MySQL Insert Query Doesn't Work With Where Clause

MySQL Insert query doesn't work with WHERE clause

MySQL INSERT Syntax does not support the WHERE clause so your query as it stands will fail. Assuming your id column is unique or primary key:

If you're trying to insert a new row with ID 1 you should be using:

INSERT INTO Users(id, weight, desiredWeight) VALUES(1, 160, 145);

If you're trying to change the weight/desiredWeight values for an existing row with ID 1 you should be using:

UPDATE Users SET weight = 160, desiredWeight = 145 WHERE id = 1;

If you want you can also use INSERT .. ON DUPLICATE KEY syntax like so:

INSERT INTO Users (id, weight, desiredWeight) VALUES(1, 160, 145) ON DUPLICATE KEY UPDATE weight=160, desiredWeight=145

OR even like so:

INSERT INTO Users SET id=1, weight=160, desiredWeight=145 ON DUPLICATE KEY UPDATE weight=160, desiredWeight=145

It's also important to note that if your id column is an autoincrement column then you might as well omit it from your INSERT all together and let mysql increment it as normal.

MySQL direct INSERT INTO with WHERE clause

INSERT syntax cannot have WHERE clause. The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.

The first syntax is already correct.

Insert query with WHERE condition in mysql

IMPORTANT WARNING: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include mysqli_query() and PDO::query()

FOR EDUCATIONAL PURPOSES: this is how your code should look if you are using mysql_query:

//you should definitely create a function that sanitizes the users input
//so that you don't get hacked via sql injection:
$value = sanitize($value);
$id= sanitize($id);

$sql = "UPDATE employee SET annual_additional = '$value'
WHERE emp_id='$id'";
if (!result = mysql_query($sql))
{
die("query error".mysql_error());
}

How to add a where clause in a MySQL Insert statement?

In an insert statement you wouldn't have an existing row to do a where claues on? You are inserting a new row, did you mean to do an update statment?

update users set username='JACK' and password='123' WHERE id='1';

Insert into query with where clause

There's no INSERT with where clause.

Take a look at this question: How to insert with where clause

Maybe, you can create an IF inside your app to verify your parameters before executing the INSERT statement.

Based on your query, I believe you need an UPDATE:

UPDATE waardes SET tag_name = @tagName,
int_value = @intValue, real_value = @doubleValue,
bool_value = @boolValue WHERE machine.machine_id LIKE %1%

Is this what you need?



Related Topics



Leave a reply



Submit