1

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();
2
  • 1
    why you use like id=$'id' ? Commented Feb 3, 2016 at 5:08
  • Well look at $'id'. This is a great example of why you should stop using mysql_* functions and update to mysqli or PDO and use prepared/paramatized statements. Commented Feb 3, 2016 at 5:08

2 Answers 2

1

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'

Sign up to request clarification or add additional context in comments.

Comments

1

Replace this :

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

With this :

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

6 Comments

Thanks, but it didn't work. Any other possible changes?
Also, the ID is a number
Write your query in phpmyadmin and see what happen.
Then please provide your complete code. I mean from where do you call this script (e.g HTML).
<form action="connect.php" method="post" /> <p>Judge ID: <input type="number" name="id" /></p> <p>Certification Level: <input type="text" name="certlevel" /></p> <input type="submit" value="Submit" /> </form>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.