我已经将数据库命名为myDatabase我创建的表单已经可以插入到数据库但是当我尝试插入我的购物车项目时它没有用,请帮助我。我应该如何插入用户添加的项目?
form.php的
<?php
session_start();
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
//Add an item only if we have the threee required pices of information: name, price, qty
if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
//Adding an Item
//Store it in a Array
$ITEM = array(
//Item name
'item_name' => $_GET['add'],
//Item Price
'price' => $_GET['price'],
//Qty wanted of item
'qty' => $_GET['qty']
);
//Add this item to the shopping cart
$_SESSION['SHOPPING_CART'][] = $ITEM;
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['remove'])){
//Remove the item from the cart
unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
//Re-organize the cart
//array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
$item="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
if (isset($_POST["item_name"])) {
$item = "";
} else {
$item = test_input($_POST["item_name"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<style>
#shoppingCartDisplay {
margin:5cm 0cm 0cm 17cm;
}
#pageHeader {
margin:0cm 0cm 0cm 20m;
}
</style>
<body>
<!DOCTYPE html>
<html>
<head>
<title>Form Validation </title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="form1">
<form action="process.php" method="POST">
<h1> Order Form </h1>
<h3> Please Fill Up this Form for Reservation </h3>
<p><span class="error">Your email address will not be published Required fields are marked(*).</span></p>
<h4> First_Name: </h4>
<input type="text" name="first_name">
<span class="error">* <?php echo $nameErr;?></span>
<h4> Last_Name: </h4>
<input type="text" name="last_name">
<span class="error">* <?php echo $nameErr;?></span>
<h4> E-mail:</h4>
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<h4> Comment </h4>
<textarea name ="comment" rows="8" cols="80"> </textarea>
<br><input type="Submit" class="submit" value="Submit Form">
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>"></form>
<form action="home.html">
<input type="submit" class="Cancel" value="Cancel" />
</form>
</div>
</form>
<div id="shoppingCartDisplay">
<form action="process.php" method="POST" name="shoppingcart">
<?php
//We want to include the shopping cart in the email
ob_start();
?>
<table width="500" border="1">
<tr>
<th scope="col"> </th>
<th scope="col">Item Name</th>
<th scope="col">Unit Price</th>
<th scope="col">Qty</th>
<th scope="col">Cost</th>
</tr>
<?php
//Print all the items in the shopping cart
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
?>
<tr id="item<?php echo $itemNumber; ?>">
<td><a href="?remove=<?php echo $itemNumber; ?>">remove</a></td>
<td><?php echo $item ['item_name']; ?></td>
<td><?php echo $item['price']; ?></td>
<td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
<td><?php echo $item['qty'] * $item['price']; ?></td>
<input type="hidden" name="item_name" value="'.$item.'">
</tr>
<?php
}
?>
</table>
<?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
</body>
</html>
process.php
<?php include 'database.php'; ?>
<?php
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$comment=$_POST['comment'];
$item=$_POST['item_name'];
//Execute the query
mysqli_query($connect,"INSERT INTO employees1 (first_name,last_name,email,comment,item_name)
VALUES ('$first_name','$last_name','$email','$comment','$item')");
if(mysqli_affected_rows($connect) > 0){
echo "<p>Form Submitted</p>";
echo "<a href=\"form.php\">Go Back</a>";
} else {
echo "RESERVATION FAILED<br />";
echo mysqli_error ($connect);
}