I'm new to PHP and PDO. I've managed to get the PDO to add the form data to a mysql database when the submit button is clicked. The problem I am having is drop down box which selects and displays the data from another database table. When this is added to the databases rather than displaying the selected option 'Top, Hat or Coat' it instead displays '0, 1, 2'.
HTML Code (with some PHP):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<?PHP include_once("addProduct.php");?>
<form method="post" action="">
Product Name: <input type="text" id="productName" name="productName" /><br />
Catagory:
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("web_scripting") or die(mysql_error());
$query = "SELECT id,category FROM catagory_table";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select type="text" id="category" name="category">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['id']."'>'".$row['category']."'</option>";
}
?> </select><br />
Stock: <input type="number" id="stock" name="stock" /><br />
Cost: <input type="number" id="cost" name="cost" /><br />
<input type="submit" value="add"> <br />
<?PHP
$query = "SELECT * FROM product_table";
$result = $odb->query($query);
if($result->rowCount() > 0) {
foreach($result as $item) {
echo($item['name'] . " , " . $item['category'] . " , " . $item['stock'] . " , " . $item['cost'] . "<br />");
}
}
?>
</form>
</div>
</body>
PHP Code:
$host = "localhost";
$user = "root";
$db = "web_scripting";
$pass = "";
$odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
if(isset($_POST['productName'])) {
$productName = $_POST['productName'];
$category = $_POST['category'];
$stock = $_POST['stock'];
$cost = $_POST['cost'];
$q = "INSERT INTO product_table(name, category, stock, cost) VALUES(:name, :category, :stock, :cost);";
$query = $odb->prepare($q);
$results = $query->execute(array(
":name" => $productName,
":category" => $category,
":stock" => $stock,
":cost" => $cost
));
}
?>