Take the 2-minute tour ×
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. It's 100% free, no registration required.

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?

<?php
include 'connect.php';
include 'main.php';
if(!isset($_SESSION['id']))

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

else{

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

Database: http://gyazo.com/b381e32d655f615b189e4fa46b74fa46.png

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

share|improve this question
add comment

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.

1 Answer

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
show 4 more comments

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