Page 1 of 1

Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 2:11 pm
by trol
Hello,

I have looked this code over several times and cannot seem to find the issue.

I am trying to insert some form data into my MYSQL database and continually get the error

"Error: Query was empty"

Here is my code:

Code: Select all

<?php
ini_set("date.timezone","America/New_York");
//login to mysql
require_once('con/con.php');
 
mysql_select_db("abc_abc" , $con)
or die("Couldn't open $db: ".mysql_error());
$when =  date("D dS M,Y h:i a");
 
if ( $_SERVER["REQUEST_METHOD"]=="POST" ) {
 
$addme1 = mysql_real_escape_string ($_POST["ADD_ME"]);
$date1 = mysql_real_escape_string ($_POST["date"]);
$title1 = mysql_real_escape_string ($_POST["title"]);
$industry1 = mysql_real_escape_string ($_POST["industry"]);
$use1 = mysql_real_escape_string ($_POST["use"]);
$collection1 = mysql_real_escape_string ($_POST["collection"]);
$res1 = mysql_real_escape_string ($_POST["resolutions"]);
$upld1 = mysql_real_escape_string ($_POST["uploadedfile"]);
$url1 = mysql_real_escape_string (basename( $_FILES['uploadedfile']['name']));
}
//UPLOAD FILE
if (isset($url1))
{
$target_path = "templates/";
 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
}
//ADD INFO TO MYSQL
if (isset($_POST['ADD_ME']))
{
  mysql_query("INSERT INTO designer (DATE, RESOLUTION, TITLE, URL, INDUSTRY, USE, COLLECTION)
VALUES ('$date1', '$res1', '$title1', '$url1', '$industry1', '$use1', '$collection1')");
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
//variables below are to ensure correct posting
echo "TEMPLATE SUCESSFULLY ADDED!";
echo $addme1;
echo $date1;
echo $title1;
echo $industry1;
echo $use1;
echo $collection1;
echo $res1;
echo $upld1;
echo $url1;
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Template</title>
</head>
 
<body>
 
<h2>Please Populate ALL The Fields Below To Add A New Template:</h2>
<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">
<input type="hidden" name="ADD_ME" value="ADD_ME">
<input type="hidden" name="date" value="<? echo "$when" ?>">
Title:<br><input type="text" name="title" size="60" value=""><br>
Industry:<br>
<select name="industry">
<option selected>Choose</option>
<option value="Hotel">Hotel</option>
<option value="Resturant/Cafe">Resturant/Cafe</option>
<option value="Convention Center">Convention Center</option>
<option value="Airport">Airport</option>
<option value="Retail">Retail</option>
<option value="Government">Government</option>
<option value="Education">Education</option>
<option value="Medical">Medical</option>
<option value="Sports Entertainment">Sports Entertainment</option>
<option value="Casino">Casino</option>
<option value="Museum/Zoo">Museum/Zoo</option>
</select><br>
Use:<br><input type="text" name="use" size="15" value=""><br>
Collection:<br>
<select name="collection">
<option selected>Choose</option>
<option value="Abstract">Abstract</option>
<option value="Emergency">Emergency</option>
<option value="Frame">Frame</option>
<option value="Fresco">Fresco</option>
<option value="Misc">Misc.</option>
</select><br>
Resolution:<br>
<select name="resolutions">
<option selected>Choose</option>
<option value="800x600">800x600</option>
<option value="1024x768">1024x768</option>
<option value="1280x720">1280x720</option>
<option value="1280x768">1280x768</option>
<option value="1280x1024">1280x1024</option>
<option value="1360x768">1360x768</option>
<option value="1366x768">1366x768</option>
<option value="1440x900">1440x900</option>
<option value="1600x1200">1600x1200</option>
<option value="1920x1080">1920x1080</option>
<option value="1920x1200">1920x1200</option>
</select><br>
<h2>Template Upload</h2>
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a template to upload: <input name="uploadedfile" type="file" /><br />
</p><hr>
  <br />
<input type="submit" name="submit_btn" value="ADD TEMPLATE" />
<input type="reset" value="RESET">
</form>
 
</body>
</html>
 
Thanks in advance!!

Re: Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 2:23 pm
by AbraCadaver
Are you just copying and pasting code? You've run a query and then you run another one with a non-existent variable $sql and then check if that was successful. Do you see any problems here:

Code: Select all

mysql_query("INSERT INTO designer (DATE, RESOLUTION, TITLE, URL, INDUSTRY, USE, COLLECTION) 
VALUES ('$date1', '$res1', '$title1', '$url1', '$industry1', '$use1', '$collection1')");
 
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

Re: Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 2:31 pm
by JakeJ
Your SQL code looks correct. Is it possible you have a data type problem?

What I do when I'm having a problem like this is to step through inserting one variable at a time until I figure out which one is failing.

Re: Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 2:38 pm
by trol
AbraCadaver,

My mistake. I have been swapping variables left and right to try and get this thing working. I changed mysql_query to $sql and I am now back to my original problem which is a different error:

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 'USE, COLLECTION) VALUES ('Thu 11th Feb,2010 03:28 pm', '1920x1080', 'Mystical Ga' at line 1

Updated code is:

if (isset($_POST['ADD_ME']))
{
$sql="INSERT INTO designer (DATE, RESOLUTION, TITLE, URL, INDUSTRY, USE, COLLECTION)
VALUES ('$date1', '$res1', '$title1', '$url1', '$industry1', '$use1', '$collection1')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

Sorry for the confusion, Thanks for your advice!

Re: Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 2:47 pm
by JakeJ

Re: Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 3:24 pm
by trol
Jake,

Thanks a million!! That's it!

I took your advice and added each variable to the database one by one and USE was the one that was throwing the error. I changed the name from USE to PURPOSE and now it is working fine! I will keep the link you provided handy for future use.

Thanks again!!

Re: Help... Error: Query was empty using INSERT INTO

Posted: Thu Feb 11, 2010 3:25 pm
by JakeJ
I'm glad it worked. Usually it's the little things that trip us up the most.