0

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">
</form>    

The function:

<script>
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);             
 }
}
</script>

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";
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
Snoken
  • 103
  • 1
  • 11
  • `UPDATE INTO booking` should be `UPDATE booking` – Abhik Chakraborty Jun 18 '14 at 07:39
  • Not directly related to your question but you may want to consider using PDO. Casting to `int` seems like a very ugly way to sanitize input for SQL. – tangrs Jun 18 '14 at 07:41
  • Yeah I already have consider using PDO and I think I will in the near future. I just like to get this function to work first. – Snoken Jun 18 '14 at 07:49

4 Answers4

0

Your DB connection string might looks mysqli_. In mysql_, you don't want to specify the DB Name as parameter.

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

This should be,

$con=mysql_connect("localhost", "root", "");

You add unnecessary INTO in UPDATE query..

Refer the manual Try this,

UPDATE booking SET ....

instead of,

UPDATE INTO booking SET ....

You might also want to read this: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Ranjith
  • 2,779
  • 3
  • 22
  • 41
0

Correct your connection as per manual and also correct Update syntax

Check PHP CONNECTION Manual

$con=mysql_connect("localhost", "root", "") OR die('Could not connect');
$db = mysql_select_db('book1',$con);
if(isset($_GET["userID"]) && isset($_GET["itemID"])){
$userID1= (int) $_GET["userID"];
$itemID2= (int) $_GET["itemID"];
$test = "userID: ". $_GET["userID"] . " " . "itemID: ". $_GET["itemID"];
echo $test;
}
$upd = mysql_query("UPDATE booking SET userID='".$userID."' WHERE ID=$itemID");

$retval = mysql_query( $upd);
if(! $retval){
    die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
  • Fixed all of the problems and now I get a syntax error saying: You have a error in your SQL syntax; check the manual that corresponds to your MySQL server verison for the right syntax to use near '1' at line 1. Please help me! Thanks! – Snoken Jun 18 '14 at 08:47
  • If your problem is not solved then how can you accept answer? – Sadikhasan Jun 18 '14 at 08:49
  • Well i'm new to this so I clicked on accept answer before I checked for upcoming errors. – Snoken Jun 18 '14 at 08:53
  • I putted it in an answer for this question, give me a moment – Snoken Jun 18 '14 at 09:02
0

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_*

fortune
  • 3,361
  • 1
  • 20
  • 30
  • Fixed all of the problems and now I get a syntax error saying: You have a error in your SQL syntax; check the manual that corresponds to your MySQL server verison for the right syntaxto use near '1' at line 1. Please help me! Thanks! – Snoken Jun 18 '14 at 08:42
  • @user3751216 there was a problem in ur variables. I have updated the code. Please try now. – fortune Jun 18 '14 at 09:10
0

@user3751216 the error of the "syntax" problem it's generated on this line

$upd = mysql_query("UPDATE booking SET userID='".$userID."' WHERE ID=$itemID");

$retval = mysql_query( $upd);

If you put your code like this, it should resolve the problem of the SQL syntax

$upd = ("UPDATE booking SET userID='".$userID."' WHERE ID=$itemID");

$retval = mysql_query( $upd);

Let me now if you already resolve the problem.

Alkaspm
  • 1
  • 1