Query was empty.

Hi Everybody,

I am trying to make a php page for my database. I connected successfully to the database but the data won’t post to the database and I got an error that my query was empty. I’ve been learning videos of php tuts on youtube. I’m definitely a newbie but I have sufficient knowledge in HTML.

Please, help me with my codes. Show me my errors so that I can learn from them.

[php]<?php

define (‘DB_NAME’,‘a1399639_idea’);
define (‘DB_USER’,‘a1399639_idea’);
define (‘DB_PASSWORD’,‘limppimp1’);
define (‘DB_HOST’,‘mysql1.000webhost.com’);

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ’ . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!db_selected) {
die('Can\t use ’ . DB_NAME . ': ’ . mysql_error());
}

echo ‘Connected successfully’;

$value1 = $_POST [‘Date1’];
$value2 = $_POST [‘Student’];
$value3 = $_POST [‘Start’];
$value4 = $_POST [‘Current’];
$value5 = $_POST [‘Grammar’];
$value6 = $_POST [‘Speaking’];
$value7 = $_POST [‘Comprehension’];
$value8 = $_POST [‘Vocabulary’];
$value9 = $_POST [‘Pronunciation’];
$value10 = $_POST [‘Comment’];
$value11 = $_POST [‘Teacher’];
$value12 = $_POST [‘Date2’];

$sql = “INSERT INTO Eval (Date1, Student, Start, Current, Grammar, Speaking, Comprehension, Vocabulary, Pronunciation, Comment, Teacher, Date2) VALUES (’$value1’, ‘$value2’, ‘$value3’, ‘$value4’, ‘$value5’, ‘$value6’, ‘$value7’, ‘$value8’, ‘$value9’, ‘$value10’, ‘$value11’ ,’$value12’)”;

if (!mysql_query($sq1)) {
die('Error: ’ . mysql_error());
}

mysql_close();

?>
[/php]

Your replies are very much appreciated.

Insert queries need to end in semi-colons. Also make sure that when inserting the values, that integers are NOT encased in single quotes.

Change the way you query to see if it works correctly or not. Do not compare a query before it is complete.
If you do that, you will know if the query worked or just passed empty recordset. So, change your code:
if (!mysql_query($sq1)) {
die('Error: ’ . mysql_error());
}

To this instead:
[php]
$result = mysql_query($sq1) or die ("Error - Query failed: " . mysql_error() );
[/php]
If the query fails, the program will DIE and display the real error. If it succeeds, then it will continue to the next line of your code.

Another debug test that might help you would be to verify the SQL query is correct!
Just before you do the query, do this:
[php]
die($sql1);
[/php]
This will display the query and stop. In this debugging trick, you can read the actual query you are attempting to send to your database.

Lastly, to get this to work correctly, you must have all of the fields in your database set up and your query must match these. If the database has not been set up correctly, then the query will not work.

Good luck, and let us know if you can solve it…

you using an L in one sql and a one sq1 in the other
you need to change one or the other

Very nice catch BearDavid! I missed that! I hate the one-character misses…

oh! thanks. Wow. This is really challenging for me. I might as well start wearing glasses. :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service