WesleyA Posted January 22, 2016 Share Posted January 22, 2016 I have a piece of code where I use MYSQL to add records into a database. Here I use INSERT, but I want to work with UPDATE. The user selects records which have to be updated. The records have a unique number. I hope people can help me how to convert MYSQL code into PHP. The MYSQL code used in the console is this: UPDATE table_name SET input2='my input here ' WHERE rec_number = 2; The part of the script I use looks like this: <?php $name1 = $conn->real_escape_string($input1); $name2 = $conn->real_escape_string($input2); // $sql = "INSERT INTO $table (input1, input2 ) VALUES (' $name1 ', ' $name2 ')" ; // olde code // $sql = "UPDATE $table SET input2, input1 WHERE rec_number = $rec_number_selected VALUES (' $name1 ', ' $name2 ')" ; $sql = "UPDATE $table (input1, input2) VALUE S (' $name1 ', ' $name2 ') WHERE rec_number = $rec_number_selected " ; ?> Anyone any idea what the right code is in PHP? Link to comment Share on other sites More sharing options...
john_jack Posted January 22, 2016 Share Posted January 22, 2016 just so we are clear $sql = "UPDATE $table (input1, input2) VALUE S (' $name1 ', ' $name2 ') WHERE rec_number = $rec_number_selected " ; this is not the right syntax for update ^ $sql = "UPDATE your_table_name SET input1='".$name1."' , input2='".$name2."' WHERE rec_number=".$rec_number_selected; replace your_table_name with you table name ,i also assume you know how to connect to database and execute a query i also assume that input1 and input2 are fields on your database table. the variables should be sanitized ($name1 and $name2)or use a prepared statement check the link http://www.w3schools.com/php/php_mysql_prepared_statements.asp 1 Link to comment Share on other sites More sharing options...
WesleyA Posted January 22, 2016 Author Share Posted January 22, 2016 Thanks for the great posting it worked instantly Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now