Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-29-2012, 10:16 PM   PM User | #1
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Error: (1065) Query was empty

Trying to create a simple admin tool and getting this error pulling my hair out over this


Code:
<!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>
mr3army is offline   Reply With Quote
Old 10-29-2012, 10:27 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.

Edit:
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mr3army (10-29-2012)
Old 10-29-2012, 10:58 PM   PM User | #3
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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.

Edit:
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 (
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
Code:
      $order = "SELECT * FROM members where id='$id'";
To
Code:
   $order = "SELECT username FROM members";
So i'm certain that the error is here.Let me know

Last edited by mr3army; 10-29-2012 at 11:14 PM..
mr3army is offline   Reply With Quote
Old 10-29-2012, 11:53 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mr3army (10-30-2012)
Old 10-30-2012, 12:02 AM   PM User | #5
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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/...data-P284.html

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


Sorry bit of a newbie

Nope you were right bingo worked thanks A++

You're so great man


Very helpful so relieved right now haha

Thanks again

Last edited by Fou-Lu; 10-30-2012 at 12:19 AM..
mr3army is offline   Reply With Quote
Old 10-30-2012, 12:28 AM   PM User | #6
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
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

Okay using the code on his site I tweaked it with your debug code and I get this result
Code:
Failed to execute query: UPDATE members SET name='', address='' WHERE id='', error: Unknown column 'name' in 'field list'
The code Im using is
Code:
<?
//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");
?>
mr3army is offline   Reply With Quote
Old 10-30-2012, 02:58 AM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 10-30-2012, 08:12 PM   PM User | #8
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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 is offline   Reply With Quote
Old 10-30-2012, 08:20 PM   PM User | #9
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Hello

Using this code:
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
mr3army is offline   Reply With Quote
Old 10-30-2012, 08:25 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mr3army (10-30-2012)
Old 10-30-2012, 08:33 PM   PM User | #11
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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

http://screencast.com/t/hEqC9668YU

See what I mean?

Last edited by mr3army; 10-30-2012 at 08:45 PM..
mr3army is offline   Reply With Quote
Old 10-30-2012, 08:46 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 10-30-2012, 08:48 PM   PM User | #13
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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

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
mr3army is offline   Reply With Quote
Old 10-30-2012, 10:54 PM   PM User | #14
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 10-30-2012, 11:14 PM   PM User | #15
mr3army
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
mr3army is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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
Code:
<?
//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

Code:
<?

//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

Code:
<?
//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

Last edited by mr3army; 10-30-2012 at 11:28 PM..
mr3army is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:13 AM.


Advertisement
Log in to turn off these ads.