1

I've been trying to make a php form page for the users of my website. When I open the .php page I got the standard error message :

Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE emp_id = $emp_id' at line 1

Can anybody help me with the syntax of these commands ???

The Code is here :

<?php
include 'dbc.php';

$emp_id = $_POST['emp_id'];
$emp_name = $_POST['emp_name'];
$emp_address = $_POST['emp_address'];
$emp_salary = $_POST['emp_salary'];
$emp_date = $_POST['join_date'];

$sql = 'INSERT INTO employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id';

mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>

5 Answers5

1

The query syntax is wrong. You have to use UPDATE query. As you are enclosing the query in single quote, the PHP variables won't get replaced. So change

$sql = 'UPDATE employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id';

to

$sql = "UPDATE employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id";

or

$sql = 'UPDATE employee SET emp_salary = '.$emp_salary.' WHERE emp_id = '.$emp_id;
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

Hi again all you good people

I thanks very much for the amount of answers !

The right solution was found and the problem is solved with this statement:

$sql = "UPDATE `employee` SET `emp_salary` = '$emp_salary' WHERE emp_id = '$emp_id'";

Most of you was inded right about the syntax and the choice about UPDATE.

The above statement function very well, but it was a bit hard to find the way.

Thanks again for all your kindness, help and time to answer my help

John Engelsby-Hansen

-1
$sql = 'UPDATE employee SET emp_salary=$emp_salary WHERE emp_id = '.$emp_id;
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6
-1

Insert query should be

$sql = 'INSERT INTO employee SET emp_salary = $emp_salary'; // it is valid without where clause

and there is no meaning for Where clause in Insert Qqery

Actually, if You want to update record then write an update query where we have to set values for column

Like

$sql = 'Update  employee SET emp_salary= $emp_salary WHERE emp_id = $emp_id';
developerCK
  • 4,418
  • 3
  • 16
  • 35
  • [check the 2nd comment at the question](http://stackoverflow.com/questions/25485612/syntax-error-near-where-emp-id-emp-id-at-line-1/25485651?noredirect=1#comment39775890_25485612) – Prix Aug 25 '14 at 12:29
-2

Your Update Query is wrong

If its an UPDATE query then it should be

UPDATE employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id

And if you are trying to insert a row then how can you use a WHERE condition?

WHERE condition are used in cases of UPDATE QUERY, NOT INSERT QUery

Smruti Singh
  • 565
  • 1
  • 4
  • 14
  • I changed the answer, and everybody realised their fault and change the INSERT query to UPDATE query.!!! LOL!!! – Smruti Singh Aug 25 '14 at 12:14
  • 1
    Your answer is still wrong as you don't tell him he needs to use double quote as with single quote the variables will not be processed. – Prix Aug 25 '14 at 12:15
  • It doesnt need to do anything with the single or double quote. Either use single or double matter is same... He is encapsualting the whole query string in a single quote, and assuming that emp_id is numeric, you dont need to do it like this '$emp_id' – Smruti Singh Aug 25 '14 at 12:17