简体   繁体   English

Mysql INSERT INTO无法正常工作 - 语法正确

[英]Mysql INSERT INTO not working - syntax correct

I have this simple insert into query that seems to be outputting an error I cannot find and it's driving me nuts:( 我有这个简单的插入查询,似乎输出一个我找不到的错误,这让我疯了:(

would someone help me. 会不会有人帮助我

error is: 错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `User_Id`='16'' at line 1

Query is: 查询是:

$insert = "INSERT INTO `pf_users` (`Task4`,`Task5`,`Task6`,`Task7`) VALUES ('$task4','$task5','$task6','$task7') WHERE `User_Id`='$GetUser'";

And here is the php code: 这是PHP代码:

if(empty($_POST) === false) {
    $task4 = sanitize($_POST['task4']);
    $task5 = sanitize($_POST['task5']);
    $task6 = sanitize($_POST['task6']);
    $task7 = sanitize($_POST['task7']);

    $GetUser = $_SESSION['User_Id'];

    //Query not inserting into database
    $insert = "INSERT INTO `pf_users` (`Task4`,`Task5`,`Task6`,`Task7`) VALUES ('$task4','$task5','$task6','$task7') WHERE `User_Id`='$GetUser'";

    echo "<pre>".$insert."</pre>";

    mysql_query($insert) or die(mysql_error()); 
}

Sanitize function is mysql_real_escape_string() and the $task values are receiving the form data. Sanitize函数是mysql_real_escape_string()$task值正在接收表单数据。

Thanks. 谢谢。

You can't use a where clause in an insert. 您不能在插入中使用where子句。

When you insert a row, it just adds everything in. 当您插入一行时,它只会添加所有内容。

If you want to update an existing record, then you use the where clause to identify which one you want to update. 如果要更新现有记录, 使用where子句标识要更新的记录。

$insert = "INSERT INTO `pf_users` (`Task4`,`Task5`,`Task6`,`Task7`) VALUES ('$task4','$task5','$task6','$task7')";

or 要么

$update = "update `pf_users` set `Task4`='$task4',`Task5`='$task5', `Task6`='$task6', `Task7`='$task7' where `User_Id`='$GetUser'";

Edit: As I have too many upvotes for such a simple answer, I better update it a touch more. 编辑:因为我有太多的赞成这么简单的答案,我更好地更新它的触摸。 There is a syntax in mySQL that lets you do both insert and update - or rather it will pick which you need to do. mySQL中有一种语法可以让你同时进行插入和更新 - 或者更确切地说它会选择你需要做的事情。 It is called "insert... on duplicate key" which looks like this: 它被称为“插入...复制键”,如下所示:

$insertOrUpdate="insert into pf_users (task4, task5, task6, task7) values ('$task4', '$task5', '$task6', '$task7')
    on duplicate key update task5='$task5', task6='$task6', task7='$task7'";

To use this, you will need to have a key defined however - this could be a primary key or a composite key. 要使用它,您需要定义一个键 - 这可以是主键或复合键。 The syntax tries to insert a row, and if that breaches the key restrictions, updates the row it would have inserted. 语法尝试插入一行,如果违反了键限制,则更新它将插入的行。 In the example above, I assumed that task4 was the primary key and therefore it was omitted from the update section of the query. 在上面的示例中,我假设task4是主键,因此它从查询的更新部分中省略。

Remove WHERE in your sql statement. 删除sql语句中的WHERE But you can use WHERE in INSERT...SELECT statement. 但是您可以在INSERT...SELECT语句中使用WHERE

Maybe you want to use UPDATE 也许你想使用UPDATE

UPDATE  `pf_users`
SET `Task4` = '$task4'
    `Task5`= '$task5'
    `Task6`= '$task6'
    `Task7` = '$task7'
WHERE `User_Id`='$GetUser'

你应该使用update而不是insert

$insert = "UPDATE `pf_users` SET `Task4`='$task4',`Task5`='$task5'.... WHERE `User_Id`='$GetUser'";

insert query never contains where clause , if you want it to use .you must be use update query. insert query永远不会包含where子句,如果你想使用它。你必须使用更新查询。 Also you can use this:- 你也可以用这个: -

$update = "UPDATE `pf_users` (`Task4`,`Task5`,`Task6`,`Task7`) VALUES ('$task4','$task5','$task6','$task7') WHERE `User_Id`='$GetUser'";
$result = mysql_query($update);

or you can use it. 或者你可以使用它。

$insert = "INSERT INTO `pf_users` SET `Task4` = '".mysql_real_escape_string($task4)."',`Task5`= '".mysql_real_escape_string($task5)."',`Task6`= '".mysql_real_escape_string($task6)."',`Task7`='".mysql_real_escape_string($task7)."'";
$result = mysql_query($insert);

you can edit it by your need. 你可以根据需要编辑它。 hope it will help you. 希望它会对你有所帮助。 i try to make it ofr you as simple as i can. 我试着让你尽可能简单。 security features may be not good in this script ,you have to do some more for that. 安全功能在这个脚本中可能不太好,你必须为此做更多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM