I'm trying to write my first product review script where
- the products are already populated into the database by me
- the visitor browses through the products' database and can submit a review of them (haven't started on this part)
PROBLEM:
1. i'm currently having a problem with adding products to the product table which contains a drop down menu with brandnames pulled from the brand table. The form is on page named addproduct.php
the incomplete and messed up code in addproduct.php is (seem to be missing INSERT into brand table on adding a product right ?)
<?php
// addproduct.php
$dcnx = @mysql_connect("localhost", "mysqluser", "password");
if (!$dcnx) {
die('<p>Unable to connect to database</a>');
} else {
echo "Connection Ok";
}
mysql_select_db(reviewdb);
if (!mysql_select_db(reviewdb)) {
die('<p>Unable to select database:' .
mysql_error() . '</p>');
}
?>
<?php
echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
<p>Add new product to database:<br />
Product Brand: <select name=\"selectbrand\">
<option selected>select brand name</option>
";
////////////////////////////////////////////////////////////////////////
$brandsql = "SELECT brandid, brandname FROM brand ORDER BY brandname ASC";
$result = @mysql_query($brandsql);
if(!$result) {
die('<p>Unable to perform query:' .
mysql_error() . '</p>');
}
while ( $brandarray = @mysql_fetch_array($result) ) {
// while begin
$brandnamelist = "<option value=\"$brandarray[brandname]\">$brandarray[brandname]</option><br />";
echo $brandnamelist;
} // while end
////////////////////////////////////////////////////////////////////////
echo "</select><br />
Product Model: <input type=\"text\" name=\"productmodel\" /><br />
Product Size: <input type=\"text\" name=\"productsize\" /><br />
Product Colour: <input type=\"text\" name=\"productcolour\" /><br />
<input type=\"submit\" name=\"submitproduct\" value=\"SUBMIT\" />
</p></form>";
?>
<?php
if ( !isset($submitproduct) ) {
echo "Fill in form the above to add product to database: ";
} else {
$insertdb = "yes";
// check to see if product form fields are empty
// if ($_POST['brandname'] == "") {
// $brandname_error = "<br />Please enter Brandname...<br />";
// $insertdb = "no";
// }
if ($_POST['productmodel'] == "") {
$productmodel_error = "<br />Please enter Product Model...<br />";
$insertdb = "no";
}
if ($_POST['productsize'] == "") {
$productsize_error = "<br />Please enter Product Size...<br />";
$insertdb = "no";
}
if ($_POST['productcolour'] == "") {
$productcolour_error = "<br />Please enter Product Colour...<br />";
$insertdb = "no";
}
// if not empty insert product into brand database
if ($insertdb == "yes") {
$brandname = $_POST['brandname'];
$productmodel = $_POST['productmodel'];
$productsize = $_POST['productsize'];
$productcolour = $_POST['productcolour'];
$sql = "INSERT INTO product SET
productmodel = $productmodel,
productsize = $productsize,
productcolour = $productcolour,
adddate = CURDATE() ";
if (@mysql_query($sql)) {
echo "<p>Product was added successfully</p>";
echo "<p><a href=\"$_SERVER[PHP_SELF]\">Add another product</a></p>";
} else {
die('<p>Unable to add product: ' .
mysql_error() . '</p>');
}
} else { // ($insertdb == "no")
echo "$brandname_error";
echo "$productmodel_error";
echo "$productsize_error";
echo "$productcolour_error";
}
////////////////////////////////////////////////////////
} // end else of !isset($submitproduct)
?>
my database is named = reviewdb and below is the table structure
CREATE TABLE brand (
brandid INT NOT NULL AUTO_INCREMENT,
brandname VAR(255) NUT NULL
)
CREATE TABLE product (
productid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
productmodel VARCHAR(255) NOT NULL,
productsize INT NOT NULL,
productcolour VARCHAR(50) NOT NULL,
adddate DATE NOT NULL
)
I have no problems adding the brandname of the product to brand table with this below code in a page called addbrand.php
<?php
// addbrand.php page
$dcnx = @mysql_connect("localhost", "mysqluser", "password");
if (!$dcnx) {
die('<p>Unable to connect to database</a>');
} else {
echo "Connection Ok";
}
mysql_select_db(reviewdb);
if (!mysql_select_db(reviewdb)) {
die('<p>Unable to select database:' .
mysql_error() . '</p>');
}
$addbrandform = "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\">
Add Brandname: <input type=\"text\" name=\"brandname\" value\"$brandname\"><br />
<input type=\"submit\" name=\"submitbrand\" value=\"Send\">
</form>";
if ( !isset($submitbrand) ) {
echo $addbrandform;
} else {
////////////////////////////////////////////////////////
$insertdb = "yes";
// check to see if brandname field is empty
if ($_POST['brandname'] == "") {
$brandname_error = "<p>Please enter Brandname...<p>";
$insertdb = "no";
}
// if not empty insert brandname into brand database
if ($insertdb == "yes") {
$brandname = $_POST['brandname'];
$sql = "INSERT INTO brand SET brandname='$brandname' ";
if (@mysql_query($sql)) {
echo "<p>Brandname was added successfully</>";
echo "<p><a href=\"$_SERVER[PHP_SELF]\">Add another Brandname</a></p>";
} else {
die ('<p>Unable to add Brandname to database:' .
mysql_error() . '</p>');
}
} else { // ($insertdb == "no")
echo "$brandname_error";
echo "$addbrandform";
}
} // else of !isset($submitbrand)
?>
the brand table is populated with 36 entries which are
brandid. brandname
1. 3M
2. Acer
3. AG Neovo
4. AOC
5. BenQ
6. Compaq
7. Cornea
8. CTX
9. Dell
10. Eizo
11. Hercules
12. Hewlett Packard
13. Hitachi
14. Hyundai
15. IBM
16. Ipex
17. KDS
18. LG
19. Likom
20. MAG
21. Microtek
22. Mitsubishi
23. NEC
24. Olympic
25. Omni
26. Panasonic
27. Philips
28. Planar
29. Polyview
30. Princeton
31. Proview
32. Sampo
33. Samsung
34. Sony
35. Sylvania
36. ViewSonic
i want to be able to sort/browse products by brandname, productsize and productcolour as well, anyone would like to steer me in the right direction ? 🙂