Snoken Snoken - 2 years ago 165
SQL Question

PHP / MySQL. Could not update data: Query was empty

First of all I like to say that I'm super bad at this type of stuff so my code can be totally useless.

The mission is to create a system that will ask the user to scan two ID's, userID and itemID. After the scan has been successful I want these values to be transported to a PHP document.
Here I'd like to run a MySQL query which will update the value of userID where itemID match the database.

So my problem is that I get this message after running my query:
userID:202 itemID:8204 Could not update data: Query was empty. And ofc my database remains empty.

I think the problem is that the query can't read the $_GET variables. But I have no clue so please help me, Thanks!

This is my form:

<form id="checkin" name="checkin" action="test.php">
<input type="button" onclick="checkIn()" value="Check in Item">

The function:

function checkIn(){
var userID=parseInt(prompt ("Scan userid"), 10);
var itemID=parseInt(prompt ("Scan itemid"), 10);
if(userID!=null && itemID!=null){
window.location.href= "http://localhost/webapp/test.php?userID=" + userID + "&itemID=" + itemID;
alert ("working so far userID:"+ userID + " --- itemID:" + itemID);

At last the PHP:

$con = mysql_connect("localhost", "root", "", "book1");
$db = mysql_select_db('book1');

if (isset($_GET["userID"]) && isset($_GET["itemID"])) {
$userID1 = (int)$_GET["userID"];
$itemID2 = (int)$_GET["itemID"];
$test = "userID: ".$_GET["userID"]." "."itemID: ".$_GET["itemID"];
echo $test;
if (!$con) {
die('Could not connect: '.mysql_error());
$upd = mysql_query('UPDATE INTO booking SET userID ="$userID" WHERE ID ="$itemID');

$retval = mysql_query($upd, $con);
if (!$retval) {
die('Could not update data: '.mysql_error());
echo "Updated data successfully\n";

Answer Source

Invalid arguments @ mysql_connect()

$con=mysql_connect("localhost", "root", "") or die ('Connection failed' . mysql_error());

$db = mysql_select_db('book1',$con);

UPDATE INTO need to change to UPDATE ....

Also you have userID assigned to variable $userID1 and itemID assigned to $itemID2. But in your query it is wrong. Query is updated now.

Mysql Manual

Also missing quotes at WHERE ID ="$itemID'

$upd = mysql_query("UPDATE booking SET userID ='$userID1' WHERE ID ='$itemID2'", $con);

P.S. Usage of mysql_* functions is not advised, instead use mysqli_*

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download