Jump to content

SQL Insert, no errors


TwinkiBro

Recommended Posts

Alright so Im not sure what Im doing wrong here, but Its not giving me any errors or such but its not inserting anything into the SQL either with this query.

 

function insertVehicle($vehicleid, $color1, $color2, $posX, $posY, $posZ, $vw, $int) {
global $handler; 
$query = "INSERT INTO vehicles (VehID, Color1, Color2, PosX, PosY, PosZ, VW, INT) VALUES ($vehicleid, $color1, $color2, $posX, $posY, $posZ, $vw, $int)";
mysqli_query($handler, $query);
}
 
Function usage
insertVehicle(411, 255, 255, 0.00, 0.00,0.00, 12, 10);

 
 
SQL structure
Edited by TwinkiBro
Link to comment
Share on other sites

Its not giving me any errors

 

 

that's because there is no error handling in your code for the database statements.

 

the easiest way of handling things like database statement errors are to use exceptions. to enable exceptions of the msyqli statements, add the following lines of code, preferably before you make the database connection so that connection errors will also throw an exception  - 

$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <- w/index checking; w/o index checking -> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;

then, if you have php's error_reporting set to E_ALL and display_errors set to ON, any database statement errors will throw an exception and be caught by php, which will report a fatal runtime error that contains the actual msyqli error information.

 

after you add the error handling, and find which column name is producing a sql sytnax error, because it is a reserved keyword, you need to use a prepared query to supply the data values to the sql statement, rather than putting them directly into the sql query. the PDO extension is much easier and more consistent than the msyqli extension, especially for prepared queries. if you can, switch to the PDO extension. 

Link to comment
Share on other sites

As mac_gyver states you have no error handling. You may have other problems, but the only thing I can "see" wrong in your code is the field name "INT". That is a reserved word in MySQL, so you can use it normally as a field name like that. You can either change the field name or you need to enclose the filed name in backticks to tell the MySQL engine that you are referring to the field name and not using the INT command.

 

 

$query = "INSERT INTO vehicles (VehID, Color1, Color2, PosX, PosY, PosZ, VW, `INT`) VALUES ($vehicleid, $color1, $color2, $posX, $posY, $posZ, $vw, $int)";
Link to comment
Share on other sites

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.