...

View Full Version : Error: (1065) Query was empty



mr3army
10-29-2012, 11:16 PM
Trying to create a simple admin tool and getting this error pulling my hair out over this :mad::mad::mad::mad:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?
include "db.inc.php";//database connection
$order = "SELECT * FROM members where id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="username"
size="20" value="<? echo "$row[username]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="email" size="40"
value="<? echo "$row[email]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>

Fou-Lu
10-29-2012, 11:27 PM
That's unusual; you shouldn't be getting a query is empty from this.
You should be able to verify this by modifying this: $result = mysql_query($order); to this: $result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());. I'm not going to lie, I'm not convinced that the problem is here.


BTW, if you don't see that error string in the die, then the problem is not here. If you do, post that in its entirety.

mr3army
10-29-2012, 11:58 PM
That's unusual; you shouldn't be getting a query is empty from this.
You should be able to verify this by modifying this: $result = mysql_query($order); to this: $result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());. I'm not going to lie, I'm not convinced that the problem is here.


BTW, if you don't see that error string in the die, then the problem is not here. If you do, post that in its entirety.


Hello,
No error the script runs but the textboxes don't contain the information they are supposed to hmm :((:mad:
Perhaps the fact that i'm using ID?
The best I can get is the boxes all filled but with the first users data
That was

$order = "SELECT * FROM members where id='$id'";

To

$order = "SELECT username FROM members";

So i'm certain that the error is here.Let me know

Fou-Lu
10-30-2012, 12:53 AM
I wouldn't expect that error from it, but $id isn't defined anywhere in the script. So you are effectively pointing at id = null (where null isn't the same as IS NULL in SQL). Since no id will match null, there is therefore no results.
Perhaps the id should be coming from $_GET? $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; for example?

mr3army
10-30-2012, 01:02 AM
I wouldn't expect that error from it, but $id isn't defined anywhere in the script. So you are effectively pointing at id = null (where null isn't the same as IS NULL in SQL). Since no id will match null, there is therefore no results.
Perhaps the id should be coming from $_GET? $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; for example?

Hello i'm uncertain as I'm following this tutorial http://www.phpeveryday.com/articles/PHP-MySQL-Editing-data-P284.html

Im guessing ID is used from the database as there is a column called ID


Sorry bit of a newbie :D

Nope you were right bingo worked thanks A++

You're so great man


Very helpful so relieved right now haha

Thanks again

mr3army
10-30-2012, 01:28 AM
Im sure I posted saying it worked but eh okay :P

Now im trying to get it to edit tables after that I will want it to be able to delete :o

Okay using the code on his site I tweaked it with your debug code and I get this result

Failed to execute query: UPDATE members SET name='', address='' WHERE id='', error: Unknown column 'name' in 'field list'

The code Im using is

<?
//edit_data.php
include "db.inc.php";
$id = $_GET["id"];
$order = "UPDATE members
SET name='$username',
address='$email'
WHERE
id='$id'";

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
header("location:edit.php");
?>

Fou-Lu
10-30-2012, 03:58 AM
This error indicates you have no field named "name".
Although you'll still have an issue. $_GET['id'] is reporting nothing on your query. That may not be a problem though; if that comes from a form its very often $_POST not get.

mr3army
10-30-2012, 09:12 PM
This error indicates you have no field named "name".
Although you'll still have an issue. $_GET['id'] is reporting nothing on your query. That may not be a problem though; if that comes from a form its very often $_POST not get.

I actually added the GET bit in Il have a test around with it.

mr3army
10-30-2012, 09:20 PM
Hello

Using this code:
<?
//edit_data.php
include "db.inc.php";
$id = $_POST["id"];
$order = "UPDATE members
SET username='$username',
email='$email'
WHERE
id='$id'";

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
header("location:edit.php");
?>


And it actually deleted the infomation username and email from the database even though information was filled in :S

Fou-Lu
10-30-2012, 09:25 PM
Where have you declared the $username and $email variables? Its not that its deleted, its that you have not provided it with valid variables.

You need to look into how to handle input from a form: $_GET or $_POST as appropriate, and how to secure that SQL from injection. You should look at prepared statements for this as the mysql library is quite old and recommended it not be used (I expect it'll be deprecated in the very near future).

mr3army
10-30-2012, 09:33 PM
Where have you declared the $username and $email variables? Its not that its deleted, its that you have not provided it with valid variables.

You need to look into how to handle input from a form: $_GET or $_POST as appropriate, and how to secure that SQL from injection. You should look at prepared statements for this as the mysql library is quite old and recommended it not be used (I expect it'll be deprecated in the very near future).

Hello it should be using the textboxes infomation
I want it to use the infomation in the two textboxes email and username
On the html they are called email and username also exactly grrrrrrr :mad:

http://screencast.com/t/hEqC9668YU

See what I mean?

Fou-Lu
10-30-2012, 09:46 PM
I know where its coming from; PHP does not know. You must pull it from a superglobal such as $_POST. The only time you can not declare them is if register_globals is enabled (disabled by default as of PHP 4.2; gone as of 5.4), so you should never rely on register_globals existing. You must pull from the appropriate input from the $_GET or $_POST whichever is providing the data.

mr3army
10-30-2012, 09:48 PM
I know where its coming from; PHP does not know. You must pull it from a superglobal such as $_POST. The only time you can not declare them is if register_globals is enabled (disabled by default as of PHP 4.2; gone as of 5.4), so you should never rely on register_globals existing. You must pull from the appropriate input from the $_GET or $_POST whichever is providing the data.

Im sorry I dont really understand what you wrote there :mad:

Is there no easy way of referencing it to the text boxes and I need this to be able to edit any user I wish

Fou-Lu
10-30-2012, 11:54 PM
Sure there is, its under $_POST['inputNameHere']. Just like the code I posted for the id, which would come from the $_GET or default to 0 if it doesn't exist.

mr3army
10-31-2012, 12:14 AM
Sure there is, its under $_POST['inputNameHere']. Just like the code I posted for the id, which would come from the $_GET or default to 0 if it doesn't exist.

So your saying

<?
//edit_data.php
include "db.inc.php";
$id = $_POST["email"]; - something like this? How would I reference everything else then
$order = "UPDATE members
SET username='$username',
email='$email'
WHERE
id='$id'";

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
header("location:edit.php");
?>

OR


<?

//edit_data.php
include "db.inc.php";

$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

$order = "UPDATE members
SET username='$username',
email='$email'
WHERE
id='$id'";

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
header("location:edit.php");

?>


OR


<?
//edit_data.php
include "db.inc.php";
$email = $_POST["email"];
$username= isset($_POST['username']) ? (int)$_POST['username'] : 0;
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

$order = "UPDATE members
SET username='$username',
email='$email'
WHERE
id='$id'";

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
//header("location:edit.php");
?>

Ahhhh so damn confused :mad::mad::mad:

Fou-Lu
10-31-2012, 01:25 AM
The last one is the closest.
isset - used to determine if a value exists and is not null
(int) is a datatype cast. It will end up as a number.
?...: is a ternary syntax shortform for if/else.

So you don't want to cast to an int with username (I wouldn't assume). But you don't want these empty either.


$email = isset($_POST['email']) ? $_POST['email'] : "";
$username = isset($_POST['username']) ? $_POST['username'] : "";
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

if (ini_get('magic_quotes_gpc'))
{
$email = stripslashes($email);
$username = stripslashes($username);
}

// do whatever validation and verification you need to here.

$email = mysql_real_escape_string($email);
$username = mysql_real_escape_string($username);

// now query.

mr3army
10-31-2012, 01:34 AM
The last one is the closest.
isset - used to determine if a value exists and is not null
(int) is a datatype cast. It will end up as a number.
?...: is a ternary syntax shortform for if/else.

So you don't want to cast to an int with username (I wouldn't assume). But you don't want these empty either.


$email = isset($_POST['email']) ? $_POST['email'] : "";
$username = isset($_POST['username']) ? $_POST['username'] : "";
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

if (ini_get('magic_quotes_gpc'))
{
$email = stripslashes($email);
$username = stripslashes($username);
}

// do whatever validation and verification you need to here.

$email = mysql_real_escape_string($email);
$username = mysql_real_escape_string($username);

// now query.


Im trying to use this now but still no success or errors
<?
//edit_data.php
include "db.inc.php";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$username = isset($_POST['username']) ? $_POST['username'] : "";
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

if (ini_get('magic_quotes_gpc'))
{
$email = stripslashes($email);
$username = stripslashes($username);
}

// do whatever validation and verification you need to here.

$email = mysql_real_escape_string($email);
$username = mysql_real_escape_string($username);

// now query.

$order = "UPDATE members
SET username='$username',
email='$email'
WHERE
id='$id'";
echo $_GET["username"];

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
header("location:edit.php");

?>

Can you please comment it also thanks :mad:

Nope it worked my FTP wasnt uploading it for some reason :S thanks buddy.

Now one last thing..

I have changed the code around now to make it suitable for my application needs but I get one error!

Failed to execute query: UPDATE members SET username='Myusernamehere', email='Myemailhere', points='13', completed_surveys=' WHERE id='4', 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 '4'' at line 5
From the error Im certain something else is up with the complete surveys bit as there is no 'number' like the points :/ and I cant see anything :mad: - never mind found a brack which was where it shouldn't of been and the error is now gone when I click edit I get a blank page now and the database does not update points and surveys completed -turns out the bracket was in the right place (Im a fool) so that error still applies

:mad:
My new code:

<?
//edit_data.php
include "db.inc.php";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$points = isset($_POST['points']) ? $_POST['points'] : "";
$completed_surveys = isset($_POST['completed_surveys']) ? $_POST['completed_surveys'] : "";
$username = isset($_POST['username']) ? $_POST['username'] : "";
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;

if (ini_get('magic_quotes_gpc'))
{
$email = stripslashes($email);
$points= stripslashes($points);
$completed_surveys = stripslashes($completed_surveys);
}

// do whatever validation and verification you need to here.

$email = mysql_real_escape_string($email);
$username = mysql_real_escape_string($username);
$points = mysql_real_escape_string($points);
$completed_surveys = mysql_real_escape_string($completed_surveys);

// now query.

$order = "UPDATE members
SET username='$username',
email='$email',
points='$points',
completed_surveys='$completed_surveys
WHERE
id='$id'";
echo $_GET["username"];

$result = mysql_query($order) or die('Failed to execute query: ' . $order . ', error: ' . mysql_error());
header("location:edit.php");

?>

mr3army
10-31-2012, 06:32 PM
any ideas?

thankss

Fou-Lu
10-31-2012, 07:42 PM
Its a SQL error. You have an incorrect string in the field for completed_surveys, so the parser takes everything between the open and close, and then finds a number and a ' where they don't belong. Simply close the string quote and it should work.

mr3army
10-31-2012, 08:16 PM
Its a SQL error. You have an incorrect string in the field for completed_surveys, so the parser takes everything between the open and close, and then finds a number and a ' where they don't belong. Simply close the string quote and it should work.

Bingo knew it was something silly

Thanks very much :thumbsup::thumbsup::thumbsup::::cool::cool::p:o:):):rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum