-2

I've been experimenting using PHP and SQL to access a database to then be able to read and append to it.

My problem is that, however I set it out when I compared different ways to structure it. It doesn't update.

$sql = "UPDATE Test SET '$updateGet' = '$appendGet' WHERE id = '$idGet'" ;

What is the correct way without worrying about SQL injection? Also, any documentation on other ways to do this would be appreciated.

<?php
$servername = "localhost";
$username = "jenk3194";
$password = "wlFfn1";
$dbname = "jenk3194";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

$appendGet = ($_GET["appendSend"]); # WHAT TO APPEND
$idGet = ($_GET["idSend"]); # ID TO APPEND
$updateGet = ($_GET["updateSend"]); # WHAT TO UPDATE
#$sql = "UPDATE Test SET Projection = 999 WHERE id = 1";
$sql = "UPDATE Test SET '.$updateGet.' = '.$appendGet.' WHERE id = '.$idGet.'" ;
#$sql= sprintf("UPDATE Test SET %s = %d WHERE id = %d", $updateGet, $appendGet, $idGet);
echo $sql;
?>

2 Answers2

1

Your SQL should look like this:

$sql = "UPDATE Test SET '".$updateGet."' = '".$appendGet."' WHERE id = '".$idGet."';

You need to add the dots before and after php variable. If it the variable contains a string you also need to add the quotation marks.

Toxide82
  • 277
  • 1
  • 7
0

You wrote something about injection so first of all you can use pdo statements as 1st security, but read up on this post Are PDO prepared statements sufficient to prevent SQL injection?.

Be safe and always check user input, it's not that if you're doing let's say email validation with your JS you can skip it in your PHP code. Security comes in layers.

Community
  • 1
  • 1
Tom St
  • 908
  • 8
  • 15