Trying to create a dynamic PHP string mysql_query to update a row and return an updated row

I have a form in which jQuery tracks the onChage event .change(), so when something changes, it fires an ajax request, and I pass in the column, id and values ​​in the url.

Here I have a PHP code that should update the data.

My question is how do I build mySQl string dynamically. and how to undo changes / updates that have just changed to db.

Here is the PHP code I'm trying to work with.

<?php require_once('Connections/connect.php'); ?>

 <?php  
    $id = $_GET['id'];
    $collumn = $_GET['collumn'];
    $val = $_GET['val'];
 ?>


<?php 
    mysql_select_db($myDB, $connection);

  // here i try to build the query string and pass in the passed in values
   $sqlUpdate = 'UPDATE `plProducts`.`allPens` SET `$collumn` = '$val' WHERE `allPens`.`prodId` = '$id' LIMIT 1;';

  // here i want to echo back the updated row (or the updated data) 
  $seeResults = mysql_query($sqlUpdate, $connection);
  echo  $seeResults
?>

is an example OK?

$sqlUpdate = 'UPDATE `plProducts`.`allPens` SET "{$collumn}" = "{$val}" WHERE `allPens`.`prodId` = "{$id}"LIMIT 1;';
+3
source share
1 answer

Use the string concatenation operator ..

$sqlUpdate = 'UPDATE `plProducts`.`allPens` SET `' . $collumn .'` = \'$val\' WHERE `allPens`.`prodId` = '. $id . ' LIMIT 1;';
mysql_query(mysql_escape_string($sqlUpdate));

, SQL injection .

+3

Source: https://habr.com/ru/post/1750678/


All Articles