Tags
Asked 2 years ago
17 Jun 2021
Views 242
Lesly

Lesly posted

select data from table, then update the same table in PHP MySQL ?

select data from table, then update the same table in PHP MySQL?
jabber

jabber
answered Apr 28 '23 00:00

Yes, you can select data from a MySQL table, and then update the same table using PHP and MySQL. Here are the steps to do so:

Connect to the MySQL database using mysqli_connect() function and select the database.


$connection = mysqli_connect("localhost", "username", "password", "database_name");

Retrieve the data you want to update from the MySQL table using a SELECT query.


$query = "SELECT * FROM table_name WHERE id = 1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);

Store the values you want to update in variables.


$column1_value = $row['column1'];
$column2_value = $row['column2'];
// add more variables for additional columns as needed

Update the values in the MySQL table using an UPDATE query.


$query = "UPDATE table_name SET column1 = '$new_column1_value', column2 = '$new_column2_value' WHERE id = 1";
mysqli_query($connection, $query);

Be sure to replace table_name, column1 , column2 , and id with the actual names used in your MySQL database. Also, replace $ new_column1_value , $ new_column2_value , and any additional variables you create with the new values you want to update the table with.

Finally, close the database connection using the mysqli_close() function.



mysqli_close($connection);

Remember to properly sanitize and validate user input before inserting or updating data in the database to prevent SQL injection attacks. You can use the mysqli_real_escape_string () function to escape any user input used in the query
Post Answer