I can't find what is wrong with the code. It is suppose to allow inserting of products to my SQL table, but the html won't show up in my browser. I ran a similar code and it ran, but my didn't. So I'm just a little confused. Which problems are still remaining in my code?
<html>
<head>
<title>Create New Product</title>
</head>
<body>
<?php
if (isset($_POST["CreateSubmit"]))
{
validate_form();
} else
{
$messages = array();
show_form($messages);
}
?>
<?php
function show_form($messages)
{
//Assign post values if exist
$newproduct = "";
$productprice = "";
$productID = "";
if (isset($_POST["newproduct"]))
$newproduct = $_POST["newproduct"];
if (isset($_POST["productprice"]))
$productprice = $_POST["productprice"];
if (isset($_POST["productID"])) ;
$productID = $_POST["productID"];
echo "<p></p>";
echo "<h2> Enter New Product</h2>";
echo "<p></p>";
?>
<h5>Fill out New Product Information and click Submit to create.</h5>
<form name="createproduct" method="POST" action="InsertEcom.php">
<table border="1" width="100%" cellpadding="0">
<tr>
<td width="157">Product Name:</td>
<td><input type="text" name="newproduct" value='<?php echo $newproduct ?
>' size="30"></td>
</tr>
<tr>
<td width="157">Price:</td>
<td><input type="text" name="productprice" value=' <?php echo $productprice ?
>' size="30"></td>
</tr>
<tr>
<td width="157">Product ID:</td>
<td><input type="text" name="productID" value=' <?php echo $productID ?>'
size="30"></td>
</tr>
<tr>
<td width="157"><input type="submit" value="Submit" name="CreateSubmit">
</td>
<td> </td>
</tr>
</table>
</form>
<?php
} //End Show Form
?>
<?php
function validate_form()
{
$messages = array();
$redisplay = false;
//Assign values
$newproduct = $_POST["newproduct"];
$productprice = $_POST["productprice"];
$productID = $_POST["productID"];
$product = new ProductClass($newproduct, $productprice, $productID);
$count = countProduct($product);
//Check for products that already exist and Do insert
if ($count == 0)
{
$res = insertProduct($product);
echo "<h3>New Product Inserted!</h3> ";
} else
{
echo "<h3>A product with that ID already exist.</h3>";
}
}//End Validate Form
function countProduct($product)
{
//Connect to the database
$mysqli = connectdb();
$newproduct = $product->getNewproduct();
$productprice = $product->getProductprice();
$productID = $product->getProductID();
//Connect to the database
$mysqli = connectdb();
//Define the Query
//For Windows MYSQL String is case insentisitive
$Myquery = "SELECT count(*) AS count FROM Products WHERE productID='$productID'";
if ($result = $mysqli->query($Myquery))
{
/*Fetch the results of the query*/
while ($row = $results->fetch_assoc())
{
$count = $row["count"];
}
/*Destroy the result set and free the memory used for it */
$result->close();
}
$mysqli->close();
return $count;
}//End count Product
function insertProduct($product)
{
//Connect to the database
$mysqli = connectdb();
$newproduct = $product->getNewproduct();
$productprice = $product->getProductprice();
$productID = $product->getProductID();
//Now we can insert
$Query = "
INSERT INTO Products
(newProduct, productPrice, productID)
VALUES
('$newproduct', '$productprice', '$productID')
";
$Success = false;
if ($result = $mysqli->query($Query))
{
$Success = true;
}
$mysqli->close();
return $Success;
}//End Insert Product
function getDbparms();
{
$trimmed = file('parms/dbparms.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPPTY_LINES);
$key = array();
$vals = array();
foreach ($trimmed as $line)
{
$pairs = explode("=", $line);
$key[] = $pairs[0];
$vals[] = $pairs[1];
}//foreach
//Combine Key and Values into an array
$mypairs = array_combine($key, $vals);
//Assign values to ParametersClass
$myDbparms = new DbparmsClass($mypairs['username'], $mypairs['password'],
$mypairs['host'], $mypairs['db']);
//Display the Parameters values
return $myDbparms;
}//End getDbparms
function connectdb()
{
//Get the DBParameters
$mydbparms = getDbparms();
//Try to connectdb
$mysqli = new mysqli($mydbparms->getHost(), $mydbparms->getUsername(),
$mydbparms->getPassword(), $mydbparms->getDb());
if ($mysqli->connect_error)
{
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
return $mysqli;
}
class DbparmsClass
{
//property declaration
private $username = "";
private $password = "";
private $host = "";
private $db = "";
//constructor
public function __construct($myusername, $mypassword, $myhost, $mydb)
{
$this->username = $myusername;
$this->password = $mypassword;
$this->host = $myhost;
$this->db = $mydb;
}
//Get methods
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function getHost()
{
return $this->host;
}
public function getDb()
{
return $this->db;
}
//Set methods
public function setUsername($myusername)
{
$this->username = $myusername;
}
public function setPassword($mypassword)
{
$this->password = $mypassword;
}
public function setHost($myhost)
{
$this->host = $myhost;
}
public function setDb($mydb)
{
$this->db = $mydb;
}
} //End DBparms class
//Class to construct Products with getters/settes
class ProductClass
{
//property declaration
private $newproduct = "";
private $productprice = "";
private $productID = "";
//Constructor
public function __construct($newproduct, $productprice, $productID)
{
$this->newproduct = $newproduct;
$this->productprice = $productprice;
$this->productID = $productID;
}
//Get Methods
public function getNewproduct()
{
return $this->newproduct;
}
public function getProductprice()
{
return $this->productprice;
}
public function getProductID()
{
return $this->productID;
}
//Set Methods
public function setNewproduct($value)
{
$this->newproduct = $value;
}
public function setProductprice($value)
{
$this->productprice = $value;
}
public function setProductID($value)
{
$this->productID = $value;
}
}//End ProductClass
?>
</body>
</html>