Home > Net >  How to Insert new value into table using where
How to Insert new value into table using where

Time:11-26

I have one table name "Grade" with this structure:

ID  Student_ID First_Name Last_Name Grade
1       1         John      Smith    60
2       2         Garry     Poul     70
3       1         John      Smith    80

And I want to add a new grade for Student_ID = 1 in the table Grade, I am using PHP with MySQL DB.

I used this but gives me error!

$sql = "INSERT INTO Grade (Grade) VALUES ('85') WHERE Student_ID=1 ";

During search I found that I can't use WHERE with INSERT in MySQL, how can solve it?

Thanks for all

CodePudding user response:

For updating existing records use UPDATE and not INSERT

UPDATE Grade 
SET Grade = 85  
WHERE Student_ID = 1 

CodePudding user response:

Insert: If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query

Update: when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!

UPDATE Grade SET Grade = 85 WHERE Student_ID=1 ;

  • Related