Jump to content

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>
Link to comment
https://forums.phpfreaks.com/topic/22243-error-query-was-empty/
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]
Link to comment
https://forums.phpfreaks.com/topic/22243-error-query-was-empty/#findComment-99610
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())
Link to comment
https://forums.phpfreaks.com/topic/22243-error-query-was-empty/#findComment-99616
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.
Link to comment
https://forums.phpfreaks.com/topic/22243-error-query-was-empty/#findComment-99617
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.
Link to comment
https://forums.phpfreaks.com/topic/22243-error-query-was-empty/#findComment-99624
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.