Jump to content

Archived

This topic is now archived and is closed to further replies.

Blip

Error: Query was empty

Recommended Posts

Hi folks, I'm new to PHP and have been doing ASP so please bear with me I'm getting an empty query error, but I'm not trying to query the database, I'm trying to add records. Can someone tell me what Im doing wrong? It is indeed getting the info from the form and therefore getting the variable values it needs. I know this because I have a "Hello World!" print statement inside of the first "If" and it does print out, so this condidtional woks. so what's wrong? Thanks in advance

Also, not that this script lives in a folder named admin, and the database lives in a folder called database. Both are in the same level under the root folder. Is my path for the database wrong?

Here's the code

<html>
<head>
</head>
<body>
<?php
$dayname=$_POST['dayname'];
$day=intval($_POST['day']);
$month=intval($_POST['month']);
$year=intval($_POST['year']);
$title=$_POST['title'];
$description=$_POST['description'];
$cost=$_POST['cost'];
$location=$_POST['location'];
$contact=$_POST['contact'];
$type=$_REQUEST["type"];

$con = mysql_connect("mysql","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Classes", $con);

if ($type == "enter")
{
print "Hello, World!";
$result = mysql_query("INSERT INTO classes (dayname, day, month, year, title, description, cost, location, contact) VALUES ('$dayname', '$day', '$month', '$year', '$title', '$description', '$cost', '$location', '$contact')");

if (!mysql_query($SQL,$con))
  {
  die('Error: ' . mysql_error());
  }
header ('location: enter.php');

}

if ($type == "edit")
{

}

if ($type == "delete")
{

}

?>
</body>
</html>

Share this post


Link to post
Share on other sites
Where does $SQL get set to here:
[code]$result = mysql_query("INSERT INTO classes (dayname, day, month, year, title, description, cost, location, contact) VALUES ('$dayname', '$day', '$month', '$year', '$title', '$description', '$cost', '$location', '$contact')");

if (!mysql_query($SQL,$con))
  {
  die('Error: ' . mysql_error());
  }[/code]

Share this post


Link to post
Share on other sites
It doesn't get set. I'm lifting this code from the net but I don't really understand it

What is the first argument of this (SQL) supposed to be?
if (!mysql_query($SQL,$con))

Do i use $result instead?

Thanks

Share this post


Link to post
Share on other sites
Instead of

[code]<?php
$result = mysql_query("INSERT INTO classes (dayname, day, month, year, title, description, cost, location, contact) VALUES ('$dayname', '$day', '$month', '$year', '$title', '$description', '$cost', '$location', '$contact')");

if (!mysql_query($SQL,$con))
{
die('Error: ' . mysql_error());
}
?>[/code]

... make it

[code]<?php
$result = mysql_query("INSERT INTO classes (dayname, day, month, year,
title, description, cost, location, contact) VALUES ('$dayname', '$day', '$month',
'$year', '$title', '$description', '$cost', '$location', '$contact')") or die(mysql_error());
?>[/code]

Also check out these links

[url=http://php.net/manual/en/function.mysql-query.php]http://php.net/manual/en/function.mysql-query.php[/url]
[url=http://php.net/manual/en/function.exit.php]http://php.net/manual/en/function.exit.php[/url] (aka die())

Share this post


Link to post
Share on other sites
[code]Chnage this:
[coce]$result = mysql_query("INSERT INTO classes (dayname, day, month, year, title, description, cost, location, contact) VALUES ('$dayname', '$day', '$month', '$year', '$title', '$description', '$cost', '$location', '$contact')");

if (!mysql_query($SQL,$con))
  {
  die('Error: ' . mysql_error());
  }[/code]
to this:
[code]$sql = "INSERT INTO classes (dayname, day, month, year, title, description, cost, location, contact) VALUES ('$dayname', '$day', '$month', '$year', '$title', '$description', '$cost', '$location', '$contact')";

mysql_query($sql, $con) or die('Error: ' . mysql_error());[/code]

DOnt just lift the code of the net. Learn and understand the code first before using it. Also code your scripts yourself. Dont do any of the copy 'n' paste malarky you dont learn as much. Just learning how to efficiently copy 'n' paste stuff.

Share this post


Link to post
Share on other sites
Hey thanks guys, that last change worked. Yeah, I'd like to have time to read a book, butn I don't have that luxury at the moment. Thanks for your help.

Share this post


Link to post
Share on other sites
If you dont understand a function/keyword in PHP or whatever is in the code. Imediatly go to php.net. You'll leave the site know how to use the function/keyword and when to use it etc. php.nts documentation is on of the best you'll ever come across.

Share this post


Link to post
Share on other sites

×

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.