Forum Moderators: coopster

Message Too Old, No Replies

Searching a SQL database

this is a simple page, what is wrong with my script?

         

dipster19

6:13 am on Mar 25, 2005 (gmt 0)

10+ Year Member



I am doing my first SQL database today, and I created the script to enter information from a form to a table. I would now like a page where I can search the table and display the entries. I cant figure out why this isnt working! The following is my form:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="query.php">
<p>First Name
<input name="fname" type="text" id="fname">
</p>
<p>Last Name
<input name="lname" type="text" id="lname">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
<input name="Reset" type="reset" id="Reset" value="Reset">
</p>
</form>
</body>
</html>

And the PHP is:
<?
$host = "localhost";
$user = "username";
$pass = "pword";
$dbname = "enusyn19_payments";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = "select * from 'credit_card_info where' 'fname'='" . $_POST['fname'] . "' and 'lname'='" . $_POST['lname'] . "'";
echo $sql;

$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) { echo "<p>",$row['fname'],": ",$row['lname']; }
?>

When I run it I am receiving the following:

select * from 'credit_card_info where' 'fname'='Matt' and 'lname'='Bair'
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/enusyn19/public_html/query.php on line 16

Line 16 is:
while ($row = mysql_fetch_array($query)) { echo "<p>",$row['fname'],": ",$row['lname']; }

dipster19

6:17 am on Mar 25, 2005 (gmt 0)

10+ Year Member



The PHP code ACTUALLY reads
$sql = "select * from 'credit_card_info where' 'fname'='" . $_POST['fname'] . "' and 'lname'='" . $_POST['lname'] . "'";
echo $sql;

But I am not even sure if my table name needs to be in quotes.

HughMungus

7:17 am on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What error are you getting?

dipster19

10:07 am on Mar 25, 2005 (gmt 0)

10+ Year Member



Its at the bottom of that post:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/enusyn19/public_html/query.php on line 16

zCat

11:07 am on Mar 25, 2005 (gmt 0)

10+ Year Member




select * from 'credit_card_info where' 'fname'='Matt' and 'lname'='Bair'

This is an error: 'credit_card_info where'

select * from 'credit_card_info' where 'fname'='Matt' and 'lname'='Bair'

but you don't need to quote table / column names anyway, except in unusual circumstances.

select * from credit_card_info where fname='Matt' and lname='Bair'

grandpa

11:07 am on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$sql = "select * from 'credit_card_info where' 'fname'='" . $_POST['fname'] . "' and 'lname'='" . $_POST['lname'] . "'";

You have a misplaced apostrophe in your query at the table name. Your code looks like it was taken from a phpadmin editor. I would try to write it a little cleaner, if possible, if using in a script.

$sql = "select * from credit_card_info where fname = $_POST['fname'] and lname = $_POST['lname']";

skunked by zCat!

btw dipster19, Welcome to WebmasterWorld

dipster19

1:51 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



Thanks for the welcome :)
I had already noted the misplaced apostrophe (which was unneeded anyway) and I have cleaned up my script as suggested.

[edited by: dipster19 at 2:03 pm (utc) on Mar. 25, 2005]

dipster19

2:02 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



Well, now I am getting a new error. It is:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/enusyn19/public_html/query.php on line 10

line 10 is:

$sql = "select * from credit_card_info where fname = $_POST['fname'] and lname = $_POST['lname']";

and the complete script I am using now is:

<?
$host = "localhost";
$user = "userid";
$pass = "p-word";
$dbname = "database name";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = "select * from credit_card_info where fname = $_POST['fname'] and lname = $_POST['lname']";
echo $sql;

$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) { echo "<p>",$row['fname'],": ",$row['lname']; }
?>

zCat

4:57 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



$sql = "select * from credit_card_info where fname = $_POST['fname'] and lname = $_POST['lname']";

You don't need to quote table / column names, but string values always need quotes:

$sql = "select * from credit_card_info where fname = '".$_POST['fname']."' and lname = '".$_POST['lname']."'";

dipster19

7:54 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



I thought the string values needed quotes, but I was givin granpa's advice a try :) Well, I reinserted quotes and periods used:

$sql = "select * from credit_card_info where fname='" . $_POST['fname'] . "'; and lname='" . $_POST['lname'] . "'";

That line is now correct, but I am back to square one. I am receiving my original error message which is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/enusyn19/public_html/query.php on line 16

Something must be wrong with line 16 which is:

while ($row = mysql_fetch_array($query)) { echo "<p>",$row['fname'],": ",$row['lname']; }

Any more input is appreciated :) thanks.

jatar_k

8:09 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



echo the query to screen and then connect via a shell to your server and execute your query in mysql itself.

failing being able to do that, change this

$query = mysql_query($sql);

to

$query = mysql_query($sql) or die ('<p>error querying: ' . mysql_error());

that will give you the true error. The error you stated above means mysql_fetch_array can't use what you gave it. If it can't use it then your query exploded and the var $query isn't able to be used for mysql_fetch_array.

look to the query itself

dipster19

9:15 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



Thanks Jatar, I made that change and now I believe I am getting the actual error (if I understood that correctly). I am now getting this when I submit my search:

select * from credit_card_info where fname='Matt'; and lname='Bair' (what I am searching for)

error querying: 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 '; and lname='Bair'' at line 1

I am at a bit of a loss now... what is it referring to when it says "at line 1"?

jatar_k

10:26 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



since you only sent a 1 line query, inevitably the error will be on line 1

you've terminated your query before where you want it to end

select * from credit_card_info where fname='Matt'; and lname='Bair'

see the semi colon after ='Matt' that shouldn't there, it should be

select * from credit_card_info where fname='Matt' and lname='Bair';

so your sql query creation should be like so

$sql = "select * from credit_card_info where fname='" . $_POST['fname'] . "' and lname='" . $_POST['lname'] . "'";

try that

dipster19

6:54 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Thanks a lot! sorry I havent replied but I was gone for the weekend. My problem was that I was missing the . before and after my variables in my query. I.E. I had:

$sql = "select * from credit_card_info where fname='"$_POST['fname']"' and lname='"$_POST['lname']"'";

Instead of:

$sql = "select * from credit_card_info where fname='" . $_POST['fname'] . "' and lname='" . $_POST['lname'] . "'";

I have a new problem now though... My goal was to have it so you could search the database for somebody's first and last name, and it would display all of the billing information from the row. When I do my search, all that is displayed to me is their first and last name. How can I fix that? Here is my current script:

<?
$host = "localhost";
$user = "userid";
$pass = "password";
$dbname = "dbname";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = "select * from credit_card_info where fname='" . $_POST['fname'] . "' and lname='" . $_POST['lname'] . "'";
echo $sql;

$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)) { echo "<p>",$row['fname'],": ",$row['lname']; }
?>

I appreciate the help, Im learning a lot from this website :)

jatar_k

7:39 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that is because that is all you are showing

echo "<p>",$row['fname'],": ",$row['lname'];

but you are getting everything

select *

so try looking at what you have available with this

while ($row = mysql_fetch_array($query)) {
echo '<p><pre>';
print_r($row);
echo '</pre>';
//echo "<p>",$row['fname'],": ",$row['lname'];
}

that is what is available in the array for you to use.

<aside>I sure hope this is an internal administration function and you take the necessary means to protect your users personal information. tables with the name 'credit_card_info' and queries using 'select *' and the thought of unencrypted credit card data make me very nervous and set off a lot of privacy law bells and security issues.

dipster19

7:57 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Thanks a ton Jatar, Its working how I had intended it to! Yes, this is an internal page. The db is hosted on a secure server, and I will be putting the forms on a password protected SSL secure site. This search is only going to be accessable to the one person who will be doing billing. My goal was just to simplify the process so they dont have to use a program like phpMyAdmin. If I am going about this the wrong way or you have suggestions, im open to hearing them! You have been very helpful, thanks a lot.

[p.s. Im not even positive I will use this, its mostly just for learning experience... sql is an interesting tool :) ]