mardi 21 juin 2016

Php + Checking stock in DB

This is my first post to bare with me. I'm doing an assignment for a course I'm taking. I have this code below and I've created a table(named inventory) with 4 columns (productid, item, cost, instock). The name in my form and the item name in the DB are the same (largestone). I'm trying to display an error message if there isn't enough stock, otherwise send to checkout page.

I have 8 in stock in the database for this item and regardless of what QTY you enter, it goes to success page. I can't figure out what I'm doing wrong. You can check the page here:

http://webapps.bcit.ca/A00993884/MDIA4325/Assign5/EXXindex.php

And here is the code I have:

if (isset($_POST["submit"])){ 

$hostname = 'localhost';
$database = 'a00993884';
$username = 'A00993884';
$password = 'password';

$conn = mysqli_connect($hostname, $username, $password, $database);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}

$largestone = $_POST["largestone"]; 
$sql = "SELECT instock FROM inventory WHERE item='largestone'";
$result = mysqli_query($conn,$sql);

if ($largestone > $result) {
$largestone_error .= '<span class="error" />Not enough stock.</span
<br />';}

else {
print '<span class="error" />Success, send them to checkout page.
</span> <br/>';}

}

and my form:

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table width="450" cellspacing="0" cellpadding="0">
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<tr >
<td>Large Stone Widgets</td>
<td>$1.50 </td>
<td>QTY: <input type="text" size="3" name="largestone" value="<?php if 
(isset($_POST['largestone'])) { print  
htmlspecialchars($_POST['largestone']); } ?>" /><?php print 
$largestone_error; ?></td>
</tr></table>

Aucun commentaire:

Enregistrer un commentaire