Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is just a basic script for updating my database from form data.

I don't have much experience with databases and this might be a bad question but I have read online that you should use UPDATE because I've only ever inserted stuff to tables.

Any idea whats wrong here?

include 'connect.php';
include 'main.php';

echo "You need to login to view this page";


$id = $_SESSION['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$motto = $_POST['motto'];
$bio = $_POST['bio'];

if(empty($firstname) || empty($lastname) || empty($motto) || empty($bio)){

echo "You didn't fill out any fields.";

} else if (strlen($motto) < 5) {
echo "Your motto must be more than 5 characters.";

$sql="UPDATE users SET firstname='$firstname', lastname='$lastname', bio='$bio',              motto='$motto' WHERE id='$id'"; 

There are no errors that show up when its loaded because I fixed some of the other ones but obviously I didn't put for there to be text saying like "Update complete!" or whatever. But I wasn't sure if I should do and "else" around the $sql function. It looks like it should work in my eyes but it doesn't update them.


I do have it connected properly as I already have a register system, profile page, etc...

share|improve this question

closed as off-topic by Mat, ypercubeᵀᴹ, Kin, Jon Seigel, JNK Sep 20 '13 at 20:04

If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 0 down vote accepted

You need to query your update:

 mysqli_query($connection, $sql);

That is just an example. You can use PDO or MySQLi; whichever you have in place.

share|improve this answer
Thanks man! But where do the variables $connection and $sql come into? In my connect.php file I have no connect variable and its even showing an error saying sql and connection are undefined variables. – Kevin Harrison Sep 20 '13 at 14:50
What does your connect.php look like? That will give me a better idea of how to help. You don't have to include your database info; just add DBNAME, DBUSER, DBPASS. – Ryan Sep 20 '13 at 14:51
mysql_connect("localhost", "root", ""); //connects to the server mysql_select_db("users"); //selects the database – Kevin Harrison Sep 20 '13 at 14:52
In that case use: mysql_query($sql); right below your update statement. – Ryan Sep 20 '13 at 14:53
Thankyou so much! You have helped me a lot. That would've taken me all day xD Thankyou – Kevin Harrison Sep 20 '13 at 14:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.