Verify model with DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazy8
Forum Commoner
Posts: 29
Joined: Fri Dec 22, 2006 12:19 pm

Verify model with DB

Post by crazy8 »

Ok so here is the deal. I have this small script below which grabs certain info off the DB and gets displayed into a table/form on a page. Now on the page a customer types in a model number and a qauntity and then after "enter" is hit and form gets submitted information of that product and its quantity get displayed onto the page. Well I do know that over time products will come and products will go and changes will have to be made to the DB to account for these changes. So one thing I would like to do but not sure how to do it, is to add some sort of model number verification. here is a workflow idea.

- Customer types in a model number (along with a quantity of course)
- Customer submits the form
- OOPS there is an error
- A new "small" window pops up and tells the customer "Were sorry the Item you have entered is no longer available."

So how would this be coded? because to be honest Im not sure where to even begin on how to do this with PHP.

Here is the script I have created upto now.

Code: Select all

<?php
define("PHPINCDIR","../../phpinc/");
include ('ez_db.php');
require_once(PHPINCDIR.'ez_html_family.php');
require_once(PHPINCDIR.'ez_class_page.php');
require_once(PHPINCDIR."quotefunctions.php");

    
		
if (!empty($_POST['prod_quantity']) AND !empty($_POST['model'])){
    $_SESSION['prod_quantity'][$_POST['model']] = $_POST['prod_quantity'];

} elseif (!empty($_POST['cart_quantity'])){
    // update qty in cart session after submit
  for ($i=0; $i<count($_SESSION['prod_quantity']); $i++){
        if ($_POST['cart_quantity'][$i] >= 1){
            $_SESSION['prod_quantity'][$_POST['cart_id'][$i]] = $_POST['cart_quantity'][$i];
      } else {
             $_SESSION['prod_quantity'][$_POST['cart_id'][$i]] = '';
             unset($_SESSION['prod_quantity'][$_POST['cart_id'][$i]]);
         }
    }
}

if (is_array($_SESSION['prod_quantity'])){
    foreach ($_SESSION['prod_quantity'] AS $prod=>$qty){
    $sql = mysql_query("SELECT * FROM product WHERE model='$prod'");
    $row = mysql_fetch_array($sql);               
    echo '<a href="p.php?n='.$row['id'].'">'.$row['model'].'</a> '.$row['little_desc']."\n";
    echo "<input type=\"text\" name=\"cart_quantity[]\" value=\"$qty\" size=\"2\" maxlength=\"4\" style=\"width: 40px\" class=\"quanw\">
          <input type=\"hidden\" name=\"cart_id[]\" value=\"$prod\"><br />";
    }
}

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Pass the result of your query through mysql_num_rows() to check if any records were returned, if not display your error message.
crazy8
Forum Commoner
Posts: 29
Joined: Fri Dec 22, 2006 12:19 pm

Post by crazy8 »

Well from what I have read on mysql_num_rows() now it seems that this is used more for retriving a number of rows. It could be that maybe I havent found a way to alter this to do what I need and im far from being a pro in PHP . What I need to do is take a model number entered by a customer and match it up with all the model numbers in the DB and if it doesnt exist then return an error. I will see if I can find some sample code that uses mysql_num_rows() in the way I need, Im sure there has to be something out there :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

crazy8 wrote: What I need to do is take a model number entered by a customer and match it up with all the model numbers in the DB and if it doesnt exist then return an error.
Well you are essentially doing that exactly.

Code: Select all

$result = mysql_query("SELECT * FROM product WHERE model='$prod'");
Then your error checking is as simple as

Code: Select all

$result = mysql_query("SELECT * FROM product WHERE model='$prod'"); 

if (mysql_num_rows($result) == 0)
{
   echo 'Model not found';
}
else
{
   while ($row = mysql_fetch_assoc($result))
   {
      echo $row['product_name'] .'<br />';
   }
}
crazy8
Forum Commoner
Posts: 29
Joined: Fri Dec 22, 2006 12:19 pm

Post by crazy8 »

Now is there a way in PHP to bring up an error in a small popup window rather then displaying the error on the current page?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

in php? no. PHP is a server side language. You'll need to do this on the Client-Side using javascript (which php can output)
crazy8
Forum Commoner
Posts: 29
Joined: Fri Dec 22, 2006 12:19 pm

Post by crazy8 »

Ok so now that I have code (untested and unmodified yet) to do the actual validation then all I need is to find a way for Javascript to take the output and bring up an error if there is no match with the DB correct? Now as of now weather it is an existing model number or not, it will still get posted onto the form so is that something else I need to worry about when trying to acomplish this? should my php script be in terms like this... "check model number and if there is no match, dont post model but launch js.error message"? I know thats horable psuedo code but im sure you get what im saying if that is indeed how the code should be.
Post Reply