user2424747
user2424747

Reputation: 23

Update mysql database with php

I want to update my database and this code in working good on another table but here i have an error and i see this message:

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 'WHERE id='588'' at line 4

<?php
$sel_item = "SELECT * FROM `employees` where id=".$_GET['emp_id'];
$done_item = mysql_query($sel_item);
$get_item = mysql_fetch_array($done_item);

if(isset($_POST['edit']) ){
    $upd= "UPDATE `employees` SET 
    `emp_no`='".$_POST['name']."',
    WHERE `id`='".$_POST['id']."";
    $do_upd = mysql_query($upd) or die(mysql_error());
}
?>


<form action="" method="post" enctype="multipart/form-data">

         <table class="append-row" width="100%" border="0" bgcolor="#006699"  height="60px" align="left" 
        style="padding:0 30px;">
          <tr>

           <td><input type="text" name="name" id="name" placeholder="name"  value="<? php echo $get_item['emp_no'];?>"></td>
          <input type="hidden" name="id" id="id"    value="<?php echo $get_item['id'];?>" >

          <td><input type="submit" name="edit" id="edit" value="edite"></td>    

      </tr>

        </table>

     </form>

Upvotes: 1

Views: 708

Answers (4)

Michael Sole
Michael Sole

Reputation: 124

emp_no='".$_POST['name']."',

The comma is breaking your SQL

Upvotes: 0

Get Off My Lawn
Get Off My Lawn

Reputation: 36351

Drop the old school Mysql and use PDO to make things easier on you and your database, but your problem is the comma before the WHERE statement.

$sql = $pdo->prepare("UPDATE employees SET emp_no = ? WHERE id = ?");
$sql->execute(array($_POST['name'], $_POST['id']));

As a personal preference, you should NEVER use a tilde `, to surround your items, if it is so you don't use a keyword, then you probably should rename your column/table/database.

Upvotes: 1

user2406160
user2406160

Reputation: 538

$upd= "UPDATE `employees` SET `emp_no`='".$_POST['name']."', WHERE `id`='".$_POST['id'].""; 
$do_upd = mysql_query($upd) or die(mysql_error());

You've missed an ' it should be...

$upd= "UPDATE `employees` SET `emp_no`='".$_POST['name']."', WHERE `id`='".$_POST['id']."'"; 

You also don't need the comma before 'WHERE'

Upvotes: 1

Dan
Dan

Reputation: 2093

You are missing the closing single quote after the $_POST['id'] in the UPDATE statement and you also have a comma you don't need before the WHERE condition.

Try:

$upd= "UPDATE `employees` SET `emp_no`='".$_POST['name']."' WHERE `id`='".$_POST['id']."'";

Upvotes: 3

Related Questions