Jump to content

Can anyone see the problem with this


elfeste

Recommended Posts

I have this routine to add customer details to a database.Once the custome adds their details, they can go back and change these details which should be updated.
The routine checks for a session. If the session value is not in the table, it adds the details to the table.
If the session value is present, it should delete the old entry and insert the new values.
The problem is that regardless of whether the session value is present the data is always entered as a new row and the old data is not deleted.
I am unable to see the problem with this routine. Can anyone see what is wrong?


function check_cust() {
mysql_connect("localhost","database","password");
$query = "SELECT session FROM orderlog WHERE session='".$cart_id."'";
$numresults = mysql_query($query);
$numrows=mysql_num_rows($numresults);
if($numrows == 0) {
$yx=0;

}
else {$yx=1;
  }
  return $yx;
}


mysql_select_db("orderlog") or die("Unable to select database"); //select which database we're using

function add_customer($cart_id,$first_name, $last_name, $addr1, $addr2, $addr3, $addr4, $addr5, $pcode, $cntry, $email, $message, $ship) {
  $yx=check_cust();
 
if ($yx==0){
$query = "INSERT INTO orderlog (session, first_name, last_name, addr1, addr2, addr3, addr4, addr5, pcode, cntry, email, message, ship) VALUES ('$cart_id', '$first_name', '$last_name', '$addr1', '$addr2', '$addr3', '$addr4', '$addr5', '$pcode', '$cntry', '$email', '$message', '$ship') ";
mysql_query($query);

}
else {$query = "DELETE FROM orderlog WHERE session='".$this->cart_id."' ";
mysql_query($query);
$query = "INSERT INTO orderlog (session, first_name, last_name, addr1, addr2, addr3, addr4, addr5, pcode, cntry, email, message, ship) VALUES ('$cart_id', '$first_name', '$last_name', '$addr1', '$addr2', '$addr3', '$addr4', '$addr5', '$pcode', '$cntry', '$email', '$message', '$ship') ";
mysql_query($query);
}}
Link to comment
Share on other sites

Your not passing [b]$cart_id[/b] to [code]check_cust()[/code], so it will always return 0 rows

change this line...

[code]function check_cust() {[/code]

to this...

[code]function check_cust($cart_id) {[/code]


change this line...

[code]$yx=check_cust();[/code]

to this...

[code]$yx=check_cust($cart_id);[/code]


printf
Link to comment
Share on other sites

I have changed the two lines as you suggested. However, this made no difference. It still adds data as a new row instead of deleting the old and inserting the new.
The only criteria for this is the session value. The check cust function should return a 1 when the session value is present but there is some problem somewhere I just cant see it.
Link to comment
Share on other sites

It difficult for people to fix things they can't see, this where you need to take responsibility and do some real time debugging. Now I you say it should work, but I still see problems with the structure, like you have mysql_select_db(), being called before the function [b]check_cust()[/b], which has my mysql_connect() inside it. Which is backwards logic, now that is not necessarily the problem because you could have a database link already open, but you also might not, but what is problem, is that your not testing all of the [b]variables[/b] being used or all of your [b]database functions[/b], so there is no way to know what is wrong without guessing, which you shouldn't do because if you write your code to debug each action you would find out what the problem really is.


printf
Link to comment
Share on other sites

I have been testing and getting the variable values from the routine

$numrows is always 0
$yx is always 0

The values dont change even though there is data with the session value in the db table. There is no problem adding the data. The problem is that the function does not recognise the data is already there so it returns $numrows=0 which will cause $yx to be 0.
The add_customer routine then adds the customer details to a new row
Link to comment
Share on other sites

I have tried to minimize the routine in to one function but $numrows is still returned as ""

function add_customer($cart_id,$first_name, $last_name, $addr1, $addr2, $addr3, $addr4, $addr5, $pcode, $cntry, $email, $message, $ship) {
  mysql_select_db("orderlog") or die("Unable to select database");
$query = "SELECT session FROM orderlog WHERE session='".$cart_id."'";
$numresults = mysql_query($query);
$numrows=mysql_num_rows($numresults);
if ($numrows==0){
mysql_select_db("orderlog") or die("Unable to select database");
$query = "INSERT INTO orderlog (session, first_name, last_name, addr1, addr2, addr3, addr4, addr5, pcode, cntry, email, message, ship) VALUES ('$cart_id', '$first_name', '$last_name', '$addr1', '$addr2', '$addr3', '$addr4', '$addr5', '$pcode', '$cntry', '$email', '$message', '$ship') ";
mysql_query($query);
}
else {
mysql_select_db("orderlog") or die("Unable to select database");
$query = "DELETE FROM orderlog WHERE session='".$cart_id."' ";
mysql_query($query);
$query = "INSERT INTO orderlog (session, first_name, last_name, addr1, addr2, addr3, addr4, addr5, pcode, cntry, email, message, ship) VALUES ('$cart_id', '$first_name', '$last_name', '$addr1', '$addr2', '$addr3', '$addr4', '$addr5', '$pcode', '$cntry', '$email', '$message', '$ship') ";
mysql_query($query);
}}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.