Search or select

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Search or select

Post by seriousdamage »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post 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; 
}
seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Post 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; 
} 

?>
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post 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;
}
seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Post by seriousdamage »

I have added the connection to the database now, but I still get the same error.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post 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 ;)
seriousdamage
Forum Commoner
Posts: 30
Joined: Sat Nov 27, 2004 10:18 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

markusn00b wrote:Or adding a double qoute to the end ;)
No, that would create an SQL syntax issue.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

double quote at the end also should work.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

crystal ship wrote:double quote at the end also should work.
I try to avoid saying this.. but your wrong ;)
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

What if we close the double quote after name= like

Code: Select all

...name='".$where."'";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

crystal ship wrote:What if we close the double quote after name= like

Code: Select all

...name='".$where."'";
Yes that's gravy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post 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..
Post Reply