Page 1 of 2
Search or select
Posted: Sun Dec 23, 2007 6:32 am
by seriousdamage
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi,
I have the below simple html page that should allow me to query my database.
could someone help me with the php script to display the records based on what I searched for?
[syntax="html"]
<html>
<head>
<title>Search</title>
</head>
<body>
<center>
<img src=../img/logo.gif><br><br>
<form name="form" action="search.php" method="post">
<input type="text" name="name" />
<input type="submit" name="Submit" value="Search" />
</form>
</body>
</html>
Many thanks
Nic
feyd | Please use[/syntax]Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sun Dec 23, 2007 7:33 am
by webspider
this may help . search.php
Code: Select all
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die(mysql_error());
$where = mysql_real_escape_string ($_POST['name']);
$query = "SELECT * FROM table_name WHERE name=\''.$where.'\'';
$sql = mysql_query($query);
while ($row = mysql_fetch_object($sql)) {
//print your data as you like
echo $row->field_name;
}
Posted: Sun Dec 23, 2007 10:00 am
by seriousdamage
thanks for the info, I made the changes to ensure connection to my database and now I get the following error:
Parse error: parse error, unexpected $ in /usr/local/psa/home/vhosts/giustinianinicola.com/httpdocs/contacts/search.php on line 16
Line 16 would be where I close the script with ?>
any idea?
I will republish the modified script, it might help
Code: Select all
<?php
$link = mysql_connect('localhost', 'username', 'password')
or die(mysql_error());
$where = mysql_real_escape_string ($_POST['name']);
$query = "SELECT * FROM contacts WHERE name=\''.$where.'\'';
$sql = mysql_query($query);
while ($row = mysql_fetch_object($sql)) {
//print your data as you like
echo $row->name;
}
?>
Posted: Sun Dec 23, 2007 10:11 am
by webspider
mysql_select_db("databsename");
Code: Select all
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die(mysql_error());
mysql_select_db("databsename");
$where = mysql_real_escape_string ($_POST['name']);
$query = "SELECT * FROM table_name WHERE name=\''.$where.'\'';
$sql = mysql_query($query);
while ($row = mysql_fetch_object($sql)) {
//print your data as you like
echo $row->field_name;
}
Posted: Sun Dec 23, 2007 10:20 am
by seriousdamage
I have added the connection to the database now, but I still get the same error.
Posted: Sun Dec 23, 2007 10:22 am
by feyd
~webspider has mistakenly placed a double quote at the beginning of the $query string assignment. Switching to a single quote will fix the issue.
Posted: Sun Dec 23, 2007 10:27 am
by markusn00b
feyd wrote:~webspider has mistakenly placed a double quote at the beginning of the $query string assignment. Switching to a single quote will fix the issue.
Or adding a double qoute to the end

Posted: Sun Dec 23, 2007 10:36 am
by seriousdamage
thanks, it workes great now.
Question:
when the outcome is posted, is it posibble that beside every record I could have a button or a link to delete that specific record?
Posted: Sun Dec 23, 2007 10:55 am
by feyd
markusn00b wrote:Or adding a double qoute to the end

No, that would create an SQL syntax issue.
Posted: Sun Dec 23, 2007 9:54 pm
by crystal ship
double quote at the end also should work.
Posted: Sun Dec 23, 2007 10:14 pm
by John Cartwright
crystal ship wrote:double quote at the end also should work.
I try to avoid saying this.. but your wrong

Posted: Sun Dec 23, 2007 10:23 pm
by crystal ship
What if we close the double quote after name= like
Posted: Sun Dec 23, 2007 10:27 pm
by John Cartwright
crystal ship wrote:What if we close the double quote after name= like
Yes that's gravy
Posted: Sun Dec 23, 2007 11:14 pm
by feyd
... and silly. If you're going to use double quote strings, use them for their features. If you're not going to use them for their features, don't use them.
Posted: Sun Dec 23, 2007 11:33 pm
by crystal ship
going to use double quote strings, use them for their features
Here could you please clarify me what do you mean by the features of double quote strings? Please clarify. I have been using the same syntax in all my projects. I may be wrong using so. So..