Forum Moderators: coopster
<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']; }
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'
$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
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']; }
?>
$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.
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
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"?
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
$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 :)
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.
[p.s. Im not even positive I will use this, its mostly just for learning experience... sql is an interesting tool :) ]