0

I'm trying to pass a dynamic value, an ID of a product. Through a form so I can write in a mysql database.

My problem is that the value is not being passed on through the form. I suspect it's due to that I'm trying to pass a value from one table to another table. Should I set a temporary variable for this $item_id instead? Or is it possible to pass anyway? Please help..

My shopping cart, here I echoes out the $item_id, which shows the last $item_id visible on the browser:

<div>
<?php 
echo $item_id
?>
<form method="post" action="checkoutpage.php">
<input type="hidden" name="product_bought" value="<?php echo $item_id ?>">
<input type="submit" value="Submit">
</form>

checkoutpage.php:

<?php 
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>checkout form</title>
</head>

<body>
<form action="insert.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email"><br />
Credit Card: <input type="text" name="credit_card">
<input type="hidden" name="product_bought" value='product_bought'>
<input type="submit">
</form>
</body>
</html>

insert.php

<?php
$con=mysqli_connect("localhost","xxxx","xxxx","mystore");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO orders (name, email, credit_card, product_bought)
VALUES
('$_POST[name]','$_POST[email]','$_POST[credit_card]','$_POST[product_bought]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>
4

1 回答 1

0

on checkout page you have:

<input type="hidden" name="product_bought" value='product_bought'>

and you should have:

<input type="hidden" name="product_bought" value='<?php echo $_POST['product_bought']; ?>'>

You are passing "product_bought" as the value.

I hope it fixes it.

于 2013-10-16T19:22:56.203 回答