MySQL INSERT Error

I have this long PHP page which is suppose to insert data into a MySQL database. Some of the fields may be null and some may have text to input into. Once the code inserts the information from the form, I want it to open up the newest record in the table(one that was just inserted) and then create a folder and move the uploaded images into it. I get the error that no index was created for each field when the PHP code runs. Here is the code:

[php]<?php

include("…/…/…/db/db_connection.php");

error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);

$img1 = $_FILES[‘img1’][‘name’];
$img2 = $_FILES[‘img2’][‘name’];
$img3 = $_FILES[‘img3’][‘name’];
$img4 = $_FILES[‘img4’][‘name’];
$img5 = $_FILES[‘img5’][‘name’];
$img6 = $_FILES[‘img6’][‘name’];
$img7 = $_FILES[‘img7’][‘name’];

$sql = “INSERT INTO Vehicles_TBL VALUES (’$_POST[category]’,’$_POST[year]’,’$_POST[make]’,’$_POST[model]’,’$_POST[series]’,’$img1’,’$img2’,’$img3’,’$img4’,’$img5’,’$img6’,’$img7’,’$_POST[price]’,’$_POST[vin]’,’$_POST[stocknumber]’,’$_POST[color]’,’$_POST[mileage]’,’$_POST[engine]’,’$_POST[transmission]’,’$_POST[airbag1]’,’$_POST[airbag2]’,’$_POST[brakes]’,’$_POST[antitheft]’,’$_POST[powersteering]’,’$_POST[powerwindows]’,’$_POST[powerlocks]’,’$_POST[powermirrors]’,’$_POST[cruisecontrol]’,’$_POST[amfmstereo]’,’$_POST[cdplayer]’,’$_POST[tintedglass]’,’$_POST[floormats1]’,’$_POST[floormats2]’,’$_POST[readinglamp1]’,’$_POST[readinglamp2]’,’$_POST[drivetrain]’,’$_POST[headlightsautoonoff]’,’$_POST[wheels]’,’$_POST[seating]’,’$_POST[powerseat1]’,’$_POST[powerseat2]’,’$_POST[rearwindow]’,’$_POST[heatedseats]’,’$_POST[featuredvehicle]’,’$_POST[sunroof]’,’$_POST[moonroof]’,’$_POST[other]’)”;
$result = mysql_query($sql);

if (!$sql)
{
die('Could not connect: ’ . mysql_error());
}
$result1 = mysql_query(“SELECT * FROM Vehicles_TBL ORDER BY VID LIMIT 0, 1”);
while($row = mysql_fetch_array($result1))
{

mkdir("…/…/…/img/" . $row[‘Category’] . “/” . $row[‘Year’] . “" . $row[‘Make’] . "” . $row[‘VID’] . “/”);

$target_path = “…/…/…/img/” . $row[‘Category’] . “/” . $row[‘Year’] . “" . $row[‘Make’] . "” . $row[‘VID’] . “/”;

$target_path1 = $target_path . basename($_FILES[‘img1’][‘name’]);
move_uploaded_file($_FILES[‘img1’][‘tmp_name’], $target_path));

$target_path2 = $target_path . basename($_FILES[‘img2’][‘name’]);
move_uploaded_file($_FILES[‘img2’][‘tmp_name’], $target_path));

$target_path3 = $target_path . basename($_FILES[‘img3’][‘name’]);
move_uploaded_file($_FILES[‘img3’][‘tmp_name’], $target_path));

$target_path4 = $target_path . basename($_FILES[‘img4’][‘name’]);
move_uploaded_file($_FILES[‘img4’][‘tmp_name’], $target_path));

$target_path5 = $target_path . basename($_FILES[‘img5’][‘name’]);
move_uploaded_file($_FILES[‘img5’][‘tmp_name’], $target_path));

$target_path6 = $target_path . basename($_FILES[‘img6’][‘name’]);
move_uploaded_file($_FILES[‘img6’][‘tmp_name’], $target_path));

$target_path7 = $target_path . basename($_FILES[‘img7’][‘name’]);
move_uploaded_file($_FILES[‘img7’][‘tmp_name’], $target_path));
}

?>[/php]

I think you will need to look in to making a timestamp or some sort of id for your database per row or does it already have one ?

Then you could call the last id or newest by time.

I have an auto increment ID (VID). This is suppose to call it by the newest VID.
[php]$result1 = mysql_query(“SELECT * FROM Vehicles_TBL ORDER BY VID LIMIT 0, 1”);
while($row = mysql_fetch_array($result1)){[/php]

Your orderby should be ORDERBY
and not ORDER BY with a space

try max()

[php]Where VID = max(VID)[/php]

or try

[php]orderby DESC limit 1[/php]

sorry could not find the edit button

Neither of them worked. Could it have anything to do with some values on the form beign NULL and I’m using an SQL INSERT statement?

add a or die() function to the sql statements and see what it comes up with.
I always try to give myself an error if it does not do what I want it to, so I can see where I am going wrong or echo a part that is not visable to me.
Then once it is working take it away or move to a new section so I can debug.

[php]insert (blah blah ) or die(mysql_error());[/php]
This might tell you something that you cannot see.
You might want to do this for the sellect all satement to.

If you are inserting nothing then depending on the structure of the fields it could be that it needs some sort of input as index is a searchable field.

$_POST[category] should be $_POST[‘category’]

I went and retyped my whole page, excluding the file system code and added the error code (or die). It looks like I wasn’t including any NULL for the VID, which is auto increment. Thanks for your help!!

-Josh

Sponsor our Newsletter | Privacy Policy | Terms of Service