Ce code va de paire avec le texte explicatif suivant:
This code is paired with the following explanative post:
http://indietrendsetter.blogspot.ca/2018/01/ma-proper-contribution.html
<!doctype html>
<?php
include('includes/database.php');
?>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<link href="../Style/Style.css" rel="stylesheet" type="text/css">
<link href="../Style/navbarstyle.css" rel="stylesheet" type="text/css">
<script language="javascript">
function verify(form) {
if(form.product_title.value == "") {
alert("please enter Product title");
form.product_title.focus();
return false;
}
if(form.product_price.value == "") {
alert("please enter Product price");
form.product_price.focus();
return false;
}
if(form.product_category.selectedIndex == 0) {
alert("please select a product category");
form.product_category.focus();
return false;
}
}
</script>
</head>
<body>
<div id="wrapper">
<?php
$path = $_SERVER["DOCUMENT_ROOT"];
$path = "../includes/template_header.php";
include_once($path);
?>
<div id="contentwrapper">
<?php
$path = $_SERVER["DOCUMENT_ROOT"];
$path = "../includes/template_shopnavbar.php";
include_once($path);
?>
<div align="center"
</br>
</br>
</br>
</br>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" onsubmit="return verify(this)";>
<table align="center" width="900" border="2px" bgcolor="#D8D3C9">
<input type="hidden" name="submitted" value="true" />
<tr align="center">
<td colspan="8" height="50px">
<h2> Insert new product here </h2>
</td>
</tr>
<tr>
<td width="150px" height="30px" align="right">
<b>Product title:</b>
</td>
<td width="700px">
<input type="text" name="product_title" size="100" required="true"/>
</td>
</tr>
<tr>
<td width="150px" height="30px" align="right">
<b>Product price:</b>
</td>
<td>
<input type="text" name="product_price" size="20" required="true"/>
</td>
</tr>
<tr>
<td width="150px" height="30px" align="right">
<b>Product category:</b>
</td>
<td>
<select name="product_category" required/>
<option>Select a category</option>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$Category_id = $row_cats['Category_id'];
$Category_title = $row_cats['Category_title'];
echo "<option value='$Category_id'>$Category_title</option>";
}
?>
</select>
</td>
</tr>
<tr align="right">
<td colspan="8" height="50px">
<input type="submit" name="submit" value="Insert Now"/>
</td>
</tr>
</table>
</form>
</br>
</br>
<?php
if (isset($_POST['submitted']))
$product_title = $_POST['product_title'];
$product_price = $_POST['product_price'];
$product_category = $_POST['product_category'];
if(isset($_POST['product_category']) && $_POST['product_category'] == 'Select a category')
{ echo '<h3>Please select a category</h3>'; }
else if(empty($_POST['product_title']) || empty($_POST['product_price']))
{echo "<h3>all fields are required</h3>";}
else
{
include('includes/database_admin.php');
//getting the text data from the field
$sqlinsert = " INSERT INTO product(product_title, product_price, product_category) VALUES('$product_title','$product_price', '$product_category')";
if(!mysqli_query($conn, $sqlinsert)) {
echo "Error inserting data";
}else{
echo "<h3>Record successfully added to the database</h3>";
}
}
?>
No comments:
Post a Comment