Jump to content

[SOLVED] Search SQL database records


barryflood22

Recommended Posts

Alright my last stab.

 

Enter Last Name: <input name="Array[name]" type="text" id="Array[name]"><BR>

 

That does not make any sense, why make that an array????

 

Enter Last Name: <input name="name" type="text" id="name"><BR>

 

Use that instead. as for the script

 

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

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

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

 

Fix that, you are assuming register_globals was on. This is the correct way.

 

Also as a side note, I think it was noted at least 3 or 4 times to "Make sure $Array['name'] has a value"

 

If it was printing a value, which I doubt it was, than that amazes me.

Link to comment
Share on other sites

Dear friend,

 

I don't know what could be problem with your code. You should use commands echo, print... to check your code results (step by step). But, other guys already told you all of that.

 

I decided to make real code for you. I have tested it. I made database like yours from screenshot.

 

Just copy code to some file - for example: search_customers.php. Make changes for variables ($host, $uname, $pass, $db). I appologize if this is detailed too much.

 

Well, here is code:

<? 
if($_POST['txt_search']) {
$host = 'localhost';
$db='customer';
$uname='root';
$pass='root';
$cnn = mysql_connect($host, $uname, $pass);
if (!$cnn) {
	echo "Unable to connect to DB: " . mysql_error();
	exit;
}
if (!mysql_select_db($db)) {
	echo "Unable to select $db: " . mysql_error();
	exit;
}

//your query (if it's okay; check it with echo command - echo $sql to see if it's good)
$sql = "SELECT * FROM customer WHERE customer_name= '" . $_POST['txt_search'] ."'";

$result = mysql_query($sql);

if (!$result) {
	echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	exit;
}

if (mysql_num_rows($result) == 0) {
	echo "No rows found, nothing to print so am exiting";
	exit;
}

echo 'Results of your search: <br> ';	
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
	echo "$i. ${row['customer_name']}<br>";
	$i++;
}

mysql_free_result($result);
} //if form activated
?> 

<form name="form1" method="post" action="">
  <input name="txt_search" type="text" id="txt_search" />
  <label>
  <input name="cmd_search" type="submit" id="cmd_search" value="Search">
  </label>
</form>

 

With that code you should be able to track your errors (if there is any) easier.

 

Please note this query will get result only if you put real customer_name like in db. Check PHP&MySQL manual for advanced queries (with LIKE and simillar conditions). Also I have changed field's name from name to customer_name. I don't know what version of MySQL database you are using. Maybe that could be problem because of reserved names. Think about that.

 

I wish goodluck to you.

 

Please, if this solve your problem use button Solved to confirm that.

 

Anthylon

Link to comment
Share on other sites

tried your code, still not working :-( doin my head in

 

 

search.php

-------------

 

<HTML>
<HEAD></HEAD>
<BODY>
<form name="form1" method="post" action="searchresults.php">
  <input name="txt_search" type="text" id="txt_search" />
  <label>
  <input name="cmd_search" type="submit" id="cmd_search" value="Search">
  </label>
</form>
</BODY>
</HTML>

 

searchresults.php

--------------------

<HTML>
<HEAD></HEAD>
<BODY>
<p><a href="../index.php">< back</a></p>
<p>    <? 
if($_POST['txt_search']) {
$host = 'localhost';
$db='customer';
$uname='root';
$pass='';
$cnn = mysql_connect($host, $uname, $pass);
if (!$cnn) {
	echo "Unable to connect to DB: " . mysql_error();
	exit;
}
if (!mysql_select_db($db)) {
	echo "Unable to select $db: " . mysql_error();
	exit;
}


$sql = "SELECT * FROM customer WHERE name= '" . $_POST['txt_search'] ."'";

$result = mysql_query($sql);

if (!$result) {
	echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	exit;
}

if (mysql_num_rows($result) == 0) {
	echo "No rows found, nothing to print so am exiting";
	exit;
}

echo 'Results of your search: <br> ';	
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
	echo "$i. ${row['name']}<br>";
	$i++;
}

mysql_free_result($result);
} 
?> 
</p>
</BODY>
</HTML>

 

when i try and search this is what i get:

 

'; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['name']}

"; $i++; } mysql_free_result($result); } ?>

Link to comment
Share on other sites

I really don't understand you ???. Sorry, but that's impossible. Please, export your db in file, rar/zip both files and send to me via email (anthylon@gmail.com). I'll check that. It seams your PHP parser didn't finished it job. That's crazy.

 

I'm confused now. Waiting for your email.

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.