elfeste Posted November 24, 2006 Share Posted November 24, 2006 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); }} Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/ Share on other sites More sharing options...
printf Posted November 24, 2006 Share Posted November 24, 2006 Your not passing [b]$cart_id[/b] to [code]check_cust()[/code], so it will always return 0 rowschange 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 Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-129805 Share on other sites More sharing options...
elfeste Posted November 25, 2006 Author Share Posted November 25, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-129851 Share on other sites More sharing options...
fenway Posted November 25, 2006 Share Posted November 25, 2006 I'm not sure why you don't just return the count directly -- it's much slower to get back all the data and then throw it away. Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-129892 Share on other sites More sharing options...
elfeste Posted November 25, 2006 Author Share Posted November 25, 2006 It may be slower but the routine is not going to be heavily used.It is reassuring to know that no-one else can see why it does not work when as far as I can tell, this routine should work without any problems. Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-130062 Share on other sites More sharing options...
printf Posted November 25, 2006 Share Posted November 25, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-130081 Share on other sites More sharing options...
elfeste Posted November 25, 2006 Author Share Posted November 25, 2006 I have been testing and getting the variable values from the routine$numrows is always 0$yx is always 0The 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 Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-130097 Share on other sites More sharing options...
elfeste Posted November 25, 2006 Author Share Posted November 25, 2006 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); }} Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-130148 Share on other sites More sharing options...
fenway Posted November 26, 2006 Share Posted November 26, 2006 And you're not getting back any errors from the DB? Quote Link to comment https://forums.phpfreaks.com/topic/28366-can-anyone-see-the-problem-with-this/#findComment-130321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.