views:

257

answers:

1

I have a form that jQuery tracks the onChage .change() event so when something is changed it runs a ajax request and i pass in the column, id, and the values in the url.

Here i have the PHP code that should update the data.

My question is now how do i build the mySQl string dynamically. and how do i echo back the changes/updates that where just changed on the db.

Here is the PHP code i am 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 this example OK?

$sqlUpdate = 'UPDATE `plProducts`.`allPens` SET "{$collumn}" = "{$val}" WHERE `allPens`.`prodId` = "{$id}"LIMIT 1;';
+2  A: 

Use the string concatenation operator ..

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

Of course, this presents a whole plethora of SQL injection loopholes.

Jacob Relkin
Damn beat me too it :D
MANCHUCK
@adardesign just echo back the $val variable. You dont need to query the DB for it since you have it already
MANCHUCK
@Jacob can you please explain what you mean by saying "a whole plethora of SQL injection loopholes". thanks
adardesign
@adardesign, There are many ways a guy like me or you can manipulate this code from the outside. Because you are not running `mysql_escape_string` or a similar function on your SQL, there are ways that your query could return results that the hacker would want.
Jacob Relkin
I'll edit the answer to use `mysql_escape_string`.
Jacob Relkin
is this the same kind of security problem as `eval()` in javaScript?
adardesign
@adardesign, Sort of.
Jacob Relkin
Start by reading everything tagged [sql-injection]: http://stackoverflow.com/questions/tagged/sql-injection
Craig Trader
@Craig +1 thanks!
adardesign
@Jacob may i ask you? why is /$val/ wrapped? inside the slashes?
adardesign
@adardesign, the backslashes are to escape the inner quotes in order so that they don't close out it's enclosing string.
Jacob Relkin
Thanks, is there an option to have the `sql string` for example "SET {$collumn} and {$val}"? instead of these concatenation? why am i asking? ill tell you the truth, currently it doesn't work. so maybe there is an extra ', i was thinking if we can have a cleaner string. Thanks again.
adardesign
Yes, you can do it that way so long as the quotes are double quotes.
Jacob Relkin
@jacob I posted inside the question the last piece of code, Please let me know if this is fine. Thanks tons.
adardesign
No, this will work: `"UPDATE plProducts.allPens SET {$collumn} = {$val} WHERE allPens.prodId = {$id} LIMIT 1;"`
Jacob Relkin
@jacob Thanks tons!
adardesign