PHP MySQL update problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
silverball
Forum Newbie
Posts: 1
Joined: Mon Mar 03, 2008 1:16 pm

PHP MySQL update problem

Post by silverball »

I have 2 tables in a MySQL database which are

'users' and
'tasks'

The 'users' table has 15 coloumns named 'task_name1' to 'task_name15'.

In the below PHP code, i need to update the information in the 'users' table for each of the columns
but the sql query for update (in the below code) is not working in PHP. Updates are not being inserted to the MySQL database table.


Can anyone help please to fix this problem.

Code: Select all

 
# If the value for a task is equal to "00001", then update the relevant column in the users table to $this_id (which i get from the user)
 
if($task1 == "00001") { mysql_query("UPDATE users SET task_name1 = '$this_id' WHERE id='$id'"); }
 
else if($task2 == "00001") { mysql_query("UPDATE users SET task_name2 = '$this_id' WHERE id='$id'"); }
 
else if($task3 == "00001") { mysql_query("UPDATE users SET task_name3 = '$this_id' WHERE id='$id'"); }
 
else if($task4 == "00001") { mysql_query("UPDATE users SET task_name4 = '$this_id' WHERE id='$id'"); }
 
else if($task5 == "00001") { mysql_query("UPDATE users SET task_name5 = '$this_id' WHERE id='$id'"); }
 
else if($task6 == "00001") { mysql_query("UPDATE users SET task_name6 = '$this_id' WHERE id='$id'"); }
 
else if($task7 == "00001") { mysql_query("UPDATE users SET task_name7 = '$this_id' WHERE id='$id'"); }
 
else if($task8 == "00001") { mysql_query("UPDATE users SET task_name8 = '$this_id' WHERE id='$id'"); }
 
else if($task9 == "00001") { mysql_query("UPDATE users SET task_name9 = '$this_id' WHERE id='$id'"); }
 
else if($task10 == "00001") { mysql_query("UPDATE users SET task_name10 = '$this_id' WHERE id='$id'"); }
 
else if($task11 == "00001") { mysql_query("UPDATE users SET task_name11 = '$this_id' WHERE id='$id'"); }
 
else if($task12 == "00001") { mysql_query("UPDATE users SET task_name12 = '$this_id' WHERE id='$id'"); }
 
else if($task13 == "00001") { mysql_query("UPDATE users SET task_name13 = '$this_id' WHERE id='$id'"); }
 
else if($task14 == "00001") { mysql_query("UPDATE users SET task_name14 = '$this_id' WHERE id='$id'"); }
 
else if($task15 == "00001") { mysql_query("UPDATE users SET task_name15 = '$this_id' WHERE id='$id'"); }
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: PHP MySQL update problem

Post by kryles »

try

Code: Select all

 
 
mysql_query("your query") or die(mysql_error());
 
 
to get a description of why it failed....or if it is just a blank screen maybe print out the value of $this_id to make sure you're getting a good/expected value. Also check that $id is set properly, because it may be trying to update WHERE $id is not an existing value (so you wouldn't see any change, but get no error message)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP MySQL update problem

Post by RobertGonzalez »

How are you setting your $task variables? There might be a better way to do what you are wanting to do.
Post Reply