-3

I have been beating my head against a wall for a few hours now trying to get this to update my DB.

<?
//edit_item_data.php
$con=mysqli_connect("localhost","root","","Inventory");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sql= "UPDATE Item 
          SET Catagory = '$_POST[Catagory]',
          Cost = '$_POST[Cost]',
          Condition = '$_POST[Condition]',
          PurchaseLot_PurchaseLotID = '$_POST[PurchaseLot]',
          Location = '$_POST[Location]',
          Desc = '$_POST[Desc]',
          Notes = '$_POST[Notes]'
          WHERE 
          ItemID = '$_POST[id]'";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>
<script type='text/javascript'>
 settimeout('self.close()',5000);
</script>

this is the error I'm getting

Error: You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 'Condition = New,     
PurchaseLot_PurchaseLotID = 1, Location = e' at line 4

I'm running mysql 5.6 and php 5.5. I'm sure its something dumb but I can't for the life of me see what it is.

DcHerrera
  • 80
  • 1
  • 11
  • 1
    Your SQL statement is invalid because you have several string variables placed unquoted directly into it, which is invalid. What you have is highly [vulnerable to tampering via SQL injection](http://en.wikipedia.org/wiki/Sql_injection). It is a good time to take a step back and read the [MySQLi documentation on prepared statements](http://php.net/manual/en/mysqli.prepare.php) to alleviate both your quoting problem and the large security problem. – Michael Berkowski Dec 14 '13 at 23:28

3 Answers3

1

Well you are hilariously vulnerable to SQL injection doing what you are doing, but the problem is that you aren't enclosing your variables in quotes, e.g:

SET Catagory = '$_POST[Catagory]',
-- etc

Use mysqli_real_escape_string to escape your variables before you put them into your SQL, like this:

SET Catagory = '" . mysqli_real_escape_string($_POST['Catagory'], $con) . "',
scrowler
  • 24,273
  • 9
  • 60
  • 92
0

xkcd

You want something like this:

$sql = "UPDATE `Item` SET
   `Catagory` = '".mysqli_real_escape_string($_POST['Catagory'],$con)."',
   `Cost` = '".mysqli_real_escape_string($_POST['Cost'],$con)."',
   ........
   WHERE `ItemID` = ".intval($_POST['id']);

Side-note, it's spelled "category".


EDIT: If you, like me, can't be arsed to type out such a long function name...

$e = function($str) use ($con) {
    return mysqli_real_escape_string($str,$con);
};

Then:

... `Catagory` = '".$e($_POST['Catagory'])."' ...
Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

The real issue was lack of grave accents

        SET `Catagory` = '$_POST[Catagory]',
DcHerrera
  • 80
  • 1
  • 11