Page 1 of 1

MySQL Update Help

Posted: Wed May 05, 2010 11:30 am
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);

Re: MySQL Update Help

Posted: Wed May 05, 2010 12:07 pm
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';";

Re: MySQL Update Help

Posted: Wed May 05, 2010 12:14 pm
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));