1

im running pdo query to update stuff i have in my table and it was working just fine until i had to add two more values to it, and im getting syntax error everytime i execute, but when i remove those two new values it works just fine.

this is from the form with post

<div class="form-group">
                <label for="hours">Hours:</label>
                <input type="text" id="hours" name="hours" value="<?php echo $inventoryR['hours']; ?>" placeholder="hours"/>
              </div>

              <div class="form-group">
                <label for="condition">condition:</label>
                <input type="text" id="condition" name="condition" value="<?php echo $inventoryR['condition']; ?>" placeholder="condition"/>
              </div>

this is php

include "connect.php";

    $id = $_POST['machineId'];
    $tags = $_POST['tags'];
    $price = $_POST['price'];
    $status = $_POST['status'];
    $info = $_POST['info'];
    $hours = $_POST['hours'];
    $condition = $_POST['condition'];

    $specl1 = $_POST['specl1'];
    $specl2 = $_POST['specl2'];
    $specl3 = $_POST['specl3'];
    $specl4 = $_POST['specl4'];
    $specl5 = $_POST['specl5'];

    $spec1 = $_POST['spec1'];
    $spec2 = $_POST['spec2'];
    $spec3 = $_POST['spec3'];
    $spec4 = $_POST['spec4'];
    $spec5 = $_POST['spec5'];

    $carousel = "";
    $featured = "";
    $rental = "";

    foreach($status as $s)
    {
        if ($s == "carousel")
        {
            $carousel = $s;
        }
        else if ($s == "featured")
        {
            $featured = $s;
        }
        else if($s == "rental")
        {
            $rental = $s;
        }

    }

    $inventoryQ = $conn->prepare("UPDATE inventory SET tags=?, price=?, carousel=?, featured=?, rental=?, info=? WHERE id=?");

    //$query = $conn->prepare("UPDATE inventory SET hours=?, condition=? WHERE id=?");

    $inventoryQ->execute(array($tags, $price, $carousel, $featured, $rental, $info, $id));

    //$query->execute(array($hours, $condition, $id));

    $specsQ = $conn->prepare("INSERT INTO specs
                            (inventory_id, label1, spec1, label2, spec2, label3, spec3, label4, spec4, label5, spec5)
                            VALUES
                            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                            ON DUPLICATE KEY UPDATE
                            label1 = VALUES(label1),
                            spec1 = VALUES(spec1),
                            label2 = VALUES(label2),
                            spec2 = VALUES(spec2), 
                            label3 = VALUES(label3),
                            spec3 = VALUES(spec3), 
                            label4 = VALUES(label4),
                            spec4 = VALUES(spec4), 
                            label5 = VALUES(label5),
                            spec5 = VALUES(spec5)");
    $specsQ->execute(array($id, $specl1, $spec1, $specl2, $spec2, $specl3, $spec3, $specl4, $spec4, $specl5, $spec5));

and i have hours and condition as the varchar (250) in table.

when im trying to execute anything with hours and condition it fails. when i comment them out like i have right now, it works just fine with other values.

Error message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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='used' WHERE id='65'' at line 1' in /home/mlerma1/public_html/admin/include/edit.php:52 Stack trace: #0 /home/mlerma1/public_html/admin/include/edit.php(52): PDOStatement->execute(Array) #1 {main} thrown in /home/mlerma1/public_html/admin/include/edit.php on line 52

i also tried commenting query for hours and condition and just echoing the post value to see if its even getting those values from form, and it does output those values.

chris85
  • 23,846
  • 7
  • 34
  • 51
max
  • 11
  • 1

1 Answers1

1

That's because CONDITION is a reserve word and needs to be escaped using backtiques like below. See MySQL Documentation

UPDATE inventory SET `hours`=?, `condition`=? WHERE id=?";
Rahul
  • 76,197
  • 13
  • 71
  • 125