Jump to content

update database records with php/mysql

Recommended Posts

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:


    $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 post
Share on other sites

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

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...