Barcode Search Thing..

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Barcode Search Thing..

Post by Dale »

Could someone please tell me how to do this:

Have a form on your webpage that when you enter in a 12 digit number and press search it use's the database to get just that number and tell you the following information:
Product Name:
Product Barcode No. :
Product Price:

And if the number that you entered ISNT in the database then it says "Sorry, But this product hasnt been put into the database"


Thanks ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Well you do a SELECT on the data from the form and then you can use mysql_num_rows() to see how many results there were. You use this info in an if statement to determine what information you show.

Mac
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

:shrug: Dont understand.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need a form with a textbox into which people can enter the data.

You have to compare the data the user entered to the data that's in the database:

Code: Select all

$sql = "SELECT name, price FROM table WHERE barcode = '{$_POSTї'barcode']}'";
@$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_assoc($result);
    echo 'Product Name: '.$rowї'name'];
    echo '<br />';
    echo 'Product Barcode No.: '.$_POSTї'barcode'];
    echo '<br />';
    echo 'Product Price: '.$rowї'price'];
} else {
    echo 'Sorry, But this product hasnt been put into the database';
}
Obviously I know nothing about the names of the relevant fields in your database so you'll have to change that, I also don't know what you've called the field on your form or what version of PHP you are running.

Mac
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

So... this is just a random think back excersise...

index.php

Code: Select all

<?php
include("header.php");
?>
<form method="POST" action="search.php">
  Barcode Number:<br>
  <input type="text" name="barcode" size="20">
    <br><br>
  <input type="submit" value="Submit" name="B1">
</form>
<?php
include("footer.php");
?>
search.php

Code: Select all

<?php

$db = mysql_connect("localhost", "root", ""); 
mysql_select_db("dale", $db); 

$sql = "SELECT name, price FROM table WHERE barcode = '{$_POSTї'barcode']}'"; 
@$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>'); 
if (mysql_num_rows($result) > 0) { 
    $row = mysql_fetch_assoc($result); 
    echo 'Product Name: '.$rowї'name']; 
    echo '<br />'; 
    echo 'Product Barcode No.: '.$_POSTї'barcode']; 
    echo '<br />'; 
    echo 'Product Price: '.$rowї'price']; 
} else { 
    echo 'Sorry, But this product hasnt been put into the database'; 
}
?>
Im going to test that now ;)
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Neado it works!!!!!!!!1111#


Thanks twigletmac for the coding bits and then i done the rest in he database ;)
Post Reply