简体   繁体   English

使用 Where 子句插入

[英]Insert using Where clause

I am having problem with inserting data with where clause.我在使用where子句插入数据时遇到问题。 Here is the query:这是查询:

    $hi= "INSERT INTO user_detail (email, looking, username, profession, experience,
         current_work, state, job_type, about, college, diploma, department)
             VALUES ('$email', '$looking', '$username', '$profession','$experience',
     '$current_work', '$state', '$job_type', '$about', '$college', '$diploma', '$department')
 WHERE s='$username'";

It shows me the error:它向我展示了错误:

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE s='aniket276'' at line 1检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“WHERE s='aniket276'”附近使用的正确语法

use MySQL update使用 MySQL 更新

like that:-像那样:-

UPDATE user_detail SET email='$email' WHERE s='$username';

You should rather use UPDATE, if you want to change the value of the field in records you select using the WHERE clause如果您想更改使用 WHERE 子句选择的记录中字段的值,您应该使用 UPDATE

WHERE clause inside insert doesn't make any sens. insert 内的WHERE子句没有任何意义。

If you want to add a new row into your database, you won't need the WHERE clause since there is no existing row on which you can refer.如果要向数据库中添加新行,则不需要WHERE子句,因为没有可以引用的现有行。

If you want to update an existing row, then you shouldn't use INSERT statement but the UPDATE statement.如果要更新现有行,则不应使用INSERT语句,而应使用UPDATE语句。

An insert does not have a WHERE clause.插入没有 WHERE 子句。

If you want to only insert a row for a particular username then probably best to do this in your calling php script.如果您只想为特定用户名插入一行,那么最好在调用 php 脚本中执行此操作。

If you want to amend an existing row for a particular username then you would use an update statement:-如果要修改特定用户名的现有行,则可以使用更新语句:-

UPDATE user_detail
SET email = '$email', 
    looking = '$looking', 
    username = '$username', 
    profession = '$profession', 
    experience = '$experience', 
    current_work = '$current_work', 
    state = '$state', 
    job_type = '$job_type', 
    about = '$about', 
    college = '$college', 
    diploma = '$diploma', 
    department = '$department'
WHERE s = '$username'

If you want to insert a row if one doesn't exist but update it if it does exist then you can do an INSERT / ON DUPLICATE KEY UPDATE (assuming that the s column for the username has a unique index on it):-如果你想在不存在的情况下插入一行,但如果它存在就更新它,那么你可以执行 INSERT / ON DUPLICATE KEY UPDATE(假设用户名的s列有一个唯一的索引):-

INSERT INTO user_detail (s,
                        email, 
                        looking, 
                        username, 
                        profession, 
                        experience, 
                        current_work, 
                        state, 
                        job_type, 
                        about, 
                        college, 
                        diploma, 
                        department) 
VALUES ('$username',
        '$email', 
        '$looking', 
        '$username', 
        '$profession', 
        '$experience', 
        '$current_work', 
        '$state', 
        '$job_type', 
        '$about', 
        '$college', 
        '$diploma', 
        '$department') 
ON DUPLICATE KEY UPDATE email = VALUES(email), 
                        looking = VALUES(looking), 
                        username = VALUES(username), 
                        profession = VALUES(profession), 
                        experience = VALUES(experience), 
                        current_work = VALUES(current_work), 
                        state = VALUES(state), 
                        job_type = VALUES(job_type), 
                        about = VALUES(about), 
                        college = VALUES(college), 
                        diploma = VALUES(diploma), 
                        department = VALUES(department)

To insert a new record , you dont use WHERE.要插入新记录,请不要使用 WHERE。 you WHERE when you want to reference to a specific record.当您想要引用特定记录时,您在 WHERE。 To do that you Use MySQL Update.为此,您可以使用 MySQL 更新。

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

http://www.w3schools.com/php/php_mysql_update.asp http://www.w3schools.com/php/php_mysql_update.asp

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

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