MySQL Update Help

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
mlummus
Forum Newbie
Posts: 5
Joined: Tue Apr 20, 2010 8:22 pm

MySQL Update Help

Post by mlummus »

Does anyone see the error in the code below? I am receiving the program's error code "Error in placing data in database." It may be something simple - I am fairly new to this. Thanks in advance for your help.

Code: Select all

$db = mysqli_connect('server', 'user', 'password');

mysqli_select_db($db, "database");

$query = "UPDATE student SET id='$id', name_last='$name_last', name_first='$name_first', email='$email', class='$class', major='$major', living='$living WHERE id='$id'";

$result = mysqli_query($db, $query);

if ($result)
     { 
       echo "student updated in database.</h2>";
     }

   else
     {
	   echo "<h2> Error in placing data in database </h2>";
     }

mysqli_close($db);
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: MySQL Update Help

Post by flying_circus »

You missed a single quote after the value of "living"

Code: Select all

$query = "UPDATE `student` SET `id`='$id', `name_last`='$name_last', `name_first`='$name_first', `email`='$email', `class`='$class', `major`='$major', `living`='$living' WHERE `id`='$id';";
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: MySQL Update Help

Post by mikosiko »

In addition to Flying_circus's post, allow me add a small comment to your code:

a) You can write this lines

Code: Select all

$db = mysqli_connect('server', 'user', 'password');
mysqli_select_db($db, "database");
In this simplified way

Code: Select all

$db = mysqli_connect('server', 'user', 'password', 'database');
the instruction mysqli_select_db() should be used only if you want to change your active connection

b) in addition to this:

Code: Select all

$result = mysqli_query($db, $query);
you can also check the number of affected rows if you want

Code: Select all

$result = mysqli_query($db, $query);
/* And print or check for the number of affected rows */
printf("Number of UPDATED rows : %d\n", mysqli_affected_rows($db));
Post Reply