Forum Moderators: coopster

Message Too Old, No Replies

very basic question - finding a mysql record

how to let a user find a record

         

michael heraghty

12:45 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



Sorry for the "newbie" nature of this question. I've been designing websites since 1994 but I don't have a programming background. In recent years, however, I've been learning (from doing) some basic Linux stuff, and have regularly installed Content Management Systems, cgi scripts, etc. by following instructions.

So when a client came to me and asked if I could create a website that would allow users to search the traceability details of his products, I rose to the challenge and said yes.

Let me exlpain further: The client produces a fish product, and has a simple (one table) database, with about 5 or 6 fields, that give details about the harvesting dates, the geographic origin, the batch number, etc. for each batch of fish.

I managed to create the table in a mySql database using phpMyAdmin. I also managed to create a set of "out-of-the-box" user admin tools for the client, by installing a free script (phpMyEdit I think).

However, my remaining problem is: what is the pHp code I need to allow users, on the website to enter a batch number, so that a page containing the corresponding record will be returned?

I have looked at various tutorials but can't seem to find where I'm looking for -- and don't really know where to start. Can anyone help?

(Just to be clear: I want to create a page that says "Enter batch number", and has a box where they can enter the number that they find on the tag of the product. There is no ambiguity about the batch number -- the user will either know it exactly or they won't.)

mcibor

2:41 pm on Apr 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code would be sth like this
index.php:
<?php
if(isset($_POST["batch"]))
{
$batch = $_POST["batch"];
$query = "SELECT batch, fish, geography FROM table WHERE batch = $batch";
$conn = mysql_connect("host", "user", "passwd") or die(mysql_error());
mysql_select_db("dbname", $conn) or die(mysql_error());
$result = mysql_query($query, $conn) or die(mysql_error());
?>
<table><tr><th>All the headers</th><th></th></tr>
<?php
while($answer = mysql_fetch_array($result))
{
echo "<tr><td>".$answer["fish"]."</td><td>".$answer["geography"]."</td></tr>";
}
echo "</table>";
}
else
{
?>
<form action="index.php" method="POST">
Enter the batch number: <input type="text" name="batch">
</form>
<?php
}
?>

I think this should work
Michal Cibor

michael heraghty

4:15 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



Thanks for that Michal! It seems to be heading in the right direction...

However, when I go into the page and enter "SS81" (a sample batch number) into the input box, I get this error returned:

Unknown column 'SS81' in 'where clause'

Any ideas?

[edited by: michael_heraghty at 5:10 pm (utc) on April 15, 2005]

michael heraghty

4:23 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



Just to update you ...

I changed

WHERE batch = $batch";

to

WHERE batch = '$batch'";

and it worked fine! Thanks again Michal!