Tamara
Tamara

Reputation: 39

Can't update mySql database

Please help me, I'm new to mySql. I'm trying to update the database, and it doesn't work. I've checked the code, and it seems to be correct, but nothing gets updated.

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='list_records.php'>View result</a>";
}

else {
    echo "ERROR";
}

?>

Thank you

Upvotes: 0

Views: 3577

Answers (2)

mrsmith
mrsmith

Reputation: 121

If the id is INT (and it should be) you should not use single quotes. That's not for INT fields.

Consider the following code:

// sets the variables
$id = $_GET['id']; // lets assume you get the id with GET
$name = 'Some Name';
$lastname = 'Some Last Name';
$email = '[email protected]';

$sql="UPDATE ".$tbl_name." SET name='".$name."', lastname='".$lastname."', email='".$email."' WHERE id=".$id;

Upvotes: 0

stealthyninja
stealthyninja

Reputation: 10371

@Tamara: You're not getting / setting $name, $lastname, $email or $id...

Upvotes: 1

Related Questions