Jump to content

[SOLVED] Search SQL database records


barryflood22

Recommended Posts

i cant get it to work!!!

 

search.php

 

<HTML>
<HEAD></HEAD>
<BODY>
<A href="../index.php"> <-- Back </A>
<FORM NAME="search" METHOD="post" ACTION="searchresults.php">
Enter Last Name: <input name="Array[name]" type="text" id="Array[name]"><BR>
<input type="submit" name="Submit" value="Submit">

</FORM>
</BODY>
</HTML>

 

searchresults.php

<HTML>
<HEAD></HEAD>
<BODY>
<p><a href="../index.php">< back</a></p>
<p>    <?php
$Array["name"] =trim ($Array["name"]);

$Host="localhost";
$User="root";
$Password="";
$DBName="customer";
$TableName="customer";
  
$Link = mysql_connect($Host, $User, $Password);
$Query="SELECT * from $TableName Where name = $Array[name]";
$Result= mysql_db_query ($DBName, $Query, $Link);

while ($Row = mysql_fetch_array ($Result)){



print (" Name:$Row[name], $Row[address] Phone: $Row[postcode]<BR> ");
}
mysql_close ($Link);
?>
</p>
</BODY>
</HTML>

Link to comment
Share on other sites

This is the way my code is at the minute

 

<HTML>
<HEAD></HEAD>
<BODY>
<p><a href="../index.php">< back</a></p>
<p>    <?php
$Array["name"] =trim ($Array["name"]);

$Host="localhost";
$User="root";
$Password="";
$DBName="customer";
$TableName="customer";
  
$Link = mysql_connect($Host, $User, $Password);
$Query="SELECT * from $TableName Where name = $Array[name]";
$Result= mysql_db_query ($DBName, $Query, $Link);

($Row = mysql_fetch_array($Result)){



echo "Name: " . $Row['name'] . ", " . $Row['address'] . "Phone: " . $Row['postcode'] . "<BR />";
}
mysql_close ($Link);
?>
</p>
</BODY>
</HTML>

Link to comment
Share on other sites

Parse error: syntax error, unexpected ';' in C:\wamp\www\hoff_godd\Search_Records\searchresults.php on line 19

 

i think that maybe it is trying to search the wrong type of field in the sql?

 

e.g. varchar text etc. or does it make a difference?

 

all my fields in the sql are varchar - apart from the unique id - it is an int

Link to comment
Share on other sites

$Query="SELECT * from $TableName Where name = $Array[name]";
$Result= mysql_db_query ($DBName, $Query, $Link) OR DIE(mysql_error());

 

What is the error printing out?

 

you may want to try this instead:

 

$Query="SELECT * from $TableName Where name = '".$Array['name']."'";
$Result= mysql_db_query ($DBName, $Query, $Link) OR DIE(mysql_error());

Link to comment
Share on other sites

<HTML>
<HEAD></HEAD>
<BODY>
<p><a href="../index.php">< back</a></p>
<p>    <?php
$Array["name"] =trim ($Array["name"]);

$Host="localhost";
$User="root";
$Password="";
$DBName="customer";
$TableName="customer";
  
$Link = mysql_connect($Host, $User, $Password);
$Query="SELECT * from $TableName Where name = '".$Array['name']."'";
$Result= mysql_db_query ($DBName, $Query, $Link) or DIE(mysql_error());

while ($Row = mysql_fetch_array($Result)) {
echo "Name: " . $Row['name'] . ", " . $Row['address'] . "Phone: " . $Row['postcode'] . "<BR />";
}
mysql_close ($Link);
?>
</p>
</BODY>
</HTML>

 

Try that out.

Link to comment
Share on other sites

I will now repost the scripts of boh pages

 

SEARCH.PHP

 

<HTML>
<HEAD></HEAD>
<BODY>
<A href="../index.php"> <-- Back </A>
<FORM NAME="search" METHOD="post" ACTION="searchresults.php">
Enter Last Name: <input name="Array[name]" type="text" id="Array[name]"><BR>
<input type="submit" name="Submit" value="Submit">

</FORM>
</BODY>
</HTML>

 

SEARCHRESULTS.PHP

 

<HTML>
<HEAD></HEAD>
<BODY>
<p><a href="../index.php">< back</a></p>
<p>    <?php
$Array["name"] =trim ($Array["name"]);

$Host="localhost";
$User="root";
$Password="";
$DBName="customer";
$TableName="customer";
  
$Link = mysql_connect($Host, $User, $Password);
$Query="SELECT * from $TableName Where name = '$Array[name]'";
$Result= mysql_db_query ($DBName, $Query, $Link);

if ($Row = mysql_fetch_assoc($Result)){



echo "Name: " . $Row['name'] . ", " . $Row['address'] . "Phone: " . $Row['postcode'] . "<BR />";
}
mysql_close ($Link);
?>
</p>
</BODY>
</HTML>

PHPMYADMIN SCREENSHOT

 

2005887058840536581_th.jpg

Link to comment
Share on other sites

Either way, your name is not matching up. SQL is CasESenSitiVe  and if $array['name'] has one case off than the value of name= it will not work.

 

$Query="SELECT * from $TableName Where lower(name) = '".strtolower($Array['name'])."'";

 

And also make sure that there is a value in $Array['name']

 

Reasoning I am adding single quotes around the name and not omitting them is there is a check done on name without quotes as an index of an array it looks as that as a constant. Although it does not make a huge difference it is more efficient because the script takes name without single quotes as a constant. If you did define name as a constant this script would be skewed. Always best to do stuff the proper way.

 

Anyhow try the lower out and see what happens.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.