anne3marie Posted May 30, 2012 Share Posted May 30, 2012 I am very new to php. I am using the code below to process a html form to update customers preference called item1. I want to update only the loggin in customer which is where my problem is. If i write "WHERE userid=4" or no where statement at all it updates correctly. When I include the where statement, it echos "updated" but the database doesn't change. I cannot figure out the correct where statement! Please Help! <?php include 'db.php'; $item_1 = $_POST['item1']; $query = "UPDATE users SET item_1 = '$item_1' WHERE userid='$userid'"; if(mysql_query($query)){ echo "updated";} else{ echo "fail";} ?> Link to comment https://forums.phpfreaks.com/topic/263402-update-mysql-not-workking/ Share on other sites More sharing options...
TOA Posted May 30, 2012 Share Posted May 30, 2012 If that's your entire script, you don't set $userid anywhere. That's probably why. Hard coding 4 in would work since it is a legit value. If you pay close attention, if you use no where clause it will update your entire database, be careful. Link to comment https://forums.phpfreaks.com/topic/263402-update-mysql-not-workking/#findComment-1349934 Share on other sites More sharing options...
anne3marie Posted May 31, 2012 Author Share Posted May 31, 2012 Sorry, I also have $userid=$_SESSION[userid]; How do I code for it to be updated of the logged in user's item1? Link to comment https://forums.phpfreaks.com/topic/263402-update-mysql-not-workking/#findComment-1349987 Share on other sites More sharing options...
TOA Posted May 31, 2012 Share Posted May 31, 2012 Sorry, I also have $userid=$_SESSION[userid]; You need to show all your code than. Your where is fine, so it must be the session. Do you have session_start at the beginning of your script? Link to comment https://forums.phpfreaks.com/topic/263402-update-mysql-not-workking/#findComment-1349991 Share on other sites More sharing options...
anne3marie Posted May 31, 2012 Author Share Posted May 31, 2012 I thought I tried that but it seems adding session_start() did the trick. Thanks for helping me. I knew it was a simple fix! Now do you know how I would add a conditional statement that leaves the database entry alone? I don't know what goes at the end of this statement or if it is possible. something like If '$item1'== 'do not update' {} not sure Link to comment https://forums.phpfreaks.com/topic/263402-update-mysql-not-workking/#findComment-1349996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.