Saturday, 31 August 2013

Why is my SQL "INSERT INTO" Query not working?

Why is my SQL "INSERT INTO" Query not working?

I'm making a storage log for work, and I've been fighting with this code
for the last two hours with no success. For some reason, no matter how
many times I check and recheck the code for my INSERT INTO query, it will
not work. Keep in mind that I copied this code, almost verbatim (changed
the form names, and fields, obviously) from another page that has
basically the same functionality and works 100%. Code below:
This is the page containing the form where the transaction is being
submitted:
<?php
$script = '<script>
$(document).ready(function(){
$(".datepicker").datepicker();
}); </script>' ;
$title = "View/Edit Storage - " ;
include('inc/header.php');
?>
<table>
<tr>
<form action="transadded.php" name='addnewtransaction'
method="POST">
<td><input type="text" name="moveID" size="20"
value="<?php echo $results[moveid]; ?>" readonly>
</td>
<td><select id="inoutselect" name="inorout">
<option value="Select">Select</option>
<option value="Storage In">Storage In</option>
<option value="Storage Out">Storage Out</option>
</select> </td>
<td><input type="text" name="numberofunits"
size="20"></td>
<td><input type="text" name="dateoftransaction"
size="20" class="datepicker"></td>
<td><input type="text" name="rrdt" size="20"> </td>
<td><input type="submit" value="Add" id="logsubmit"></td>
</form>
</table>
<br /><br />
<?php };?>
Here's the query itself, aka "transadded.php":
<?php
$title = "Project Added - ";
include('inc/header.php');
$query = "INSERT INTO newtransaction (moveid, inout, units, transdate,
refno)
VALUES('$_POST[moveID]','$_POST[inorout]','$_POST[numberofunits]','$_POST[dateoftransaction]','$_POST[rrdt]')";
if(!mysqli_query($con,$query))
{
die ('Error: ' . mysqli_error($con));
}
echo '<div class="transstatus">' . '1 record added' . '</div>';
mysqli_close($con);
?>
The header, obviously, contains the function for connecting to the
database, and as I said, another query works just fine with it, so I know
that that isn't the problem. Upon clicking the submit button, the error I
get on the page is as follows:
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
'inout, units, transdate, refno) VALUES ('1234567','Storage
In','81','09/11/2013'' at line 1
Here, "1234567", "Storage In", etc are the values I enter into the form.
I hope you guys can help me out. I'm so stuck!
Also, I know that I'm not protected against injection right now. I plan to
work on that later, but I'm trying to get the functionality straightened
out first.
Thanks!

No comments:

Post a Comment