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
https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/
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>

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

$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());

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.