Bora
Bora

Reputation: 248

SQL creates new row when I run an update command

I have this script (below), which is meant to update a value for a specific row in a table. I am searching by ID, which is set as an auto-incrementing, primary key integer. When I run this script, the row is not updated- instead, a completely new row is created.

Here's the code:

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$id = $_POST['id'];
$certLevel = $_POST['certlevel'];

$sql = "UPDATE jcontact SET certlevel='$certLevel' WHERE id=$'id'";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

mysql_close();

Upvotes: 1

Views: 210

Answers (2)

user4486345
user4486345

Reputation:

First of all, I think its time to travel from MySQL into MySQLi or PDO.

Second, you need to use ' for strings and your mistake in your WHERE clause:

id=$'id'

You can easily use like this:

id=$id

or:

id='$id'

Upvotes: 1

Mr. Engineer
Mr. Engineer

Reputation: 3515

Replace this :

$sql = "UPDATE jcontact SET certlevel='$certLevel' WHERE id=$'id'";

With this :

$sql = "UPDATE jcontact SET certlevel='$certLevel' WHERE id='$id'";

Upvotes: 1

Related Questions