Jump to content

Update mysql not workking


anne3marie

Recommended Posts

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
Share on other sites

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
Share on other sites

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
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.