Jump to content

Recommended Posts

Hi

 

I am trying to update data in my database, but i keep getting an error message saying -

 

Unable to perform query

update employee set title =' Mr ', firstname =' Tarik ', surname =' Chowdary ', username =' Tarf ', password =' surxth ',where employeeID =

 

 

This is my PHP script (below) - i have been doing this for ages but cant find the problem.

 

<?php

require "connect.php";

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

$query = "insert into employee values ('','".$title."','".$firstname."','".$surname."','".$username."','".$password."')";

$result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query");

header("Location: ClientForm.php");

exit();

?>

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/84019-solved-updating-data-in-a-database/
Share on other sites

Sorry that was the code for inserting data :-S

 

The actual code is below -

 

<?php

require "connect.php";

$employeeID = $_SESSION['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

    $query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password.    " ',where employeeID = ".$employeeID;;

$result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query");

header("Location: displayEmployeeAdmin.php");

exit();

?>

 

Thanks

 

 

 

I think i found the error

 

This is your current code :

 

$query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password.    " ',where employeeID = ".$employeeID."";

 

if you will notice, there should be no comma after the where clause |--[.]-[.]--|.

 

$query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password.    " ' where employeeID = ".$employeeID."";

Hi

 

Im afraid it didnt work.

 

The code you gave me -

 

$query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password.    " ',where employeeID = ".$employeeID."";

 

Has two "" at the end, is this right ?

 

I took one off and then i got the error message

 

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\HelpDesk\EditUser.php on line 10

 

Does this mean i have solved the SQL query ? and just have to tackle another.

 

The problem is in your query. Remove the comma before the WHERE clause. That should fix the problem. Also, you might want to check that you are actually getting a value from $employeeID because from the query that you posted in the first post, it seems to be missing.

 

Updated: Someone got ahead of me, oh well.

This is the code with the amendments and still it doesnt work, i keep getting the "unable to update data ...." error msg.

 

<?php

require "connect.php";

$employeeID = $_SESSION['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

    $query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password." ' where employeeID = ".$employeeID."";

$result = mysql_query($query, $connection) or die ("Unable to perform query<br>$query");

header("Location: displayEmployeeAdmin.php");

exit();

?>

oh try this:

replace your variable $query with this:

 

$query = "update employee set title ='".$title."' && firstname ='".$firstname."' && surname ='".$surname."' && username ='".$username."' && password ='".$password."' where employeeID = '".$employeeID."'";

Hi, This is the new and updated Code

 

<?php

require "connect.php";

$employeeID = $_SESSION['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

    $query = "update employee set title ='".$title."' && firstname ='".$firstname."' && surname ='".$surname."' && username ='".$username."' && password ='".$password."' where employeeID = '".$employeeID."'";

$result = mysql_query($query, $connection) or die ('Unable to perform query: '.$query.'<br />Error: '.mysql_error());

header("Location: displayEmployeeAdmin.php");

exit();

?>

 

Thankfully the error mesages have gone now, but when i edit a text field with new data and click submit, nothing happens !!

 

The data is as it was before.

 

e.g

 

Txtfield 1 - employeeID - 1

Txtfield 2 - name - David

Txtfield 3 - surname - Beckham

Txtfield 4 - username - david

Txtfield 5 - password - david

 

 

The above is the data which i originally inserted into the DB, i want to change the password to beckham, but when i click submit nothing happens.

 

Hi Duclet

 

This is the original code with ur results amendments

 

<?php

require "connect.php";

$employeeID = $_SESSION['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

    $query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password.    " ' where employeeID = ".$employeeID;

$result = mysql_query($query, $connection) or die ('Unable to perform query: '.$query.'<br />Error: '.mysql_error());

header("Location: displayEmployeeAdmin.php");

exit();

?>

 

 

And the following error message has come up -

 

Unable to perform query: update employee set title =' Mr ', firstname =' David ', surname =' Beckham ', username =' David ', password =' Beckham ' where employeeID =

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 '' at line 1

 

Thanks

 

 

 

 

By looking at the SQL, it seems like the $employeeID does not contain any value as I had thought. Which means this line:

$employeeID = $_SESSION['employeeID'];

is not doing what it is suppose to be doing.

 

Trying added session_start() at the top of the script and see if you are getting any value for $employeeID. Also, to definitively know that $employeeID is the problem of all of this, trying giving it the value explicitly. If the script runs correctly, then it is because you are not getting the session data. Otherwise, the SQL is correct.

Hi

 

After adding session_start(); at the top, i still get the same error message, would i actually need a session around the employee

i.e -    $employeeID = $_SESSION['employeeID'];

 

This is the whole code again with the new changes -

 

<?php

session_start();

require "connect.php";

$employeeID = $_SESSION['employeeID'];

$title = $_GET['title'];

$firstname = $_GET['firstname'];

$surname = $_GET['surname'];

$username = $_GET['username'];

$password = $_GET['password'];

    $query = "update employee set title =' ".$title." ', firstname =' ".$firstname." ', surname =' ".$surname." ', username =' ".$username." ', password =' ".$password.    " ' where employeeID = ".$employeeID;

$result = mysql_query($query, $connection) or die ('Unable to perform query: '.$query.'

    Error: '.mysql_error());

header("Location: displayEmployeeAdmin.php");

exit();

?>

The problem is that $_SESSION['employeeID'] is just returning NULL and not giving you a true value. So when you add that part to the SQL query, your query ends with "WHERE employeeID =" which is an invalid SQL. If you change that line to

$employeeID = 1;

the query should work. And if you still don't believe me, trying doing a var_dump($employeeID) and see what it gives you.

Hi

 

I changed the code to show a 6 at the end, because thats the ID number of the record in the database i was changing and its worked.

 

where employeeID = ".$employeeID = 6; like you have said.

 

But obviously needing it to work without hardcoding the number :-S

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.