我正在尝试在单击提交按钮时更新“Cart_View02”表。这个学期我是php和sql的新手。谁知道我做错了什么?
<?PHP
if (isset($_GET['tab'])) $table= $_GET['tab'];
else $table="Cart_view02";
$reset = true;
$errmsg = array("","");
//1. Make a connection to the database
$dbconn = mysqli_connect("localhost","root","","mydatabase1")
or die(mysqli_connect_error());
if($_SERVER['REQUEST_METHOD']=='POST'){
$action = $_POST['action'];
$qty = $_POST['txtqty'];
$crtnum = $_POST[100];
$itemid = $_POST[$row[0]];
if($action == "select"){
$sql = "INSERT INTO cart_lineitems VALUES ($crtnum, '$itemid',
'$qty');";
$result = mysqli_query($db,$sql) or die(mysql_error($db));
}
//print_r($_POST);
header("location: cartdump.php");
}
?>
<!DOCTYPE html>
<html>
<head><title>Order Page</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
</script>
<img class="banner" src="Images/acme_banner.jpg" alt="Acme Spook Shoppe"/>
</head>
<body>
<br><br>
<a href= "home.php">Home</a>
<form name="form1" method='POST'>
<?php
if (isset($_GET['prod'])) $prod=$_GET['prod'];
else $prod = "arm01";
//echo $prod; //<------To print results
$sql = "select productid,name,imagefile, "
."shortdescription, longdescription,"
."unitprice "
."from products "
."where productid='$prod'";
echo "<br>";
//echo $sql; //<------To print results
//2. Run a query against the database
$result = mysqli_query($dbconn,$sql)
or die(mysqli_error($dbconn));
//print_r($result); //<------To print results
//3. Return the results
echo "<table class=ordertable >";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[1]<br><br>";
echo "$row[3]<br><br>";
echo "$row[4]<br><br>";
echo "Price: $row[5]<br><br>";
echo "<input type='text' name='txtqty' value='1' size=2 maxlength=2>";
echo "<input type='submit' name='btnadd' value='Add To Cart'
OnClick='SubmitForm'>";
echo "</td>";
echo "<td><img src='images/products/$row[2]' "
."height=300px width =250px></td>";
echo "</tr>";
echo "</table>";
//print_r($row);
}
//4. Release the resources
mysqli_free_result($result);
//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>
对此的任何帮助将不胜感激。
答案 0 :(得分:0)
这就是我最终的结果:
<!DOCTYPE html>
<?PHP
print_r($_POST);
if (isset($_GET['prod'])) $prod=$_GET['prod'];
else $prod = "arm01";
//if (isset($_GET['tab'])) $table= $_GET['tab'];
// else $table="Cart_view02";
//$reset = true;
//$errmsg = array("","");
//1. Make a connection to the database
$dbconn = mysqli_connect("localhost","root","","mydatabase1")
or die(mysqli_connect_error());
if($_SERVER['REQUEST_METHOD']=='POST'){
$qty = $_POST['txtqty'];
$crtnum = 100;
if (preg_match("/^[0-9]{1,2}$/",$qty)){
$sql = "INSERT INTO cart_lineitems VALUES ($crtnum, '$prod', $qty);";
$result = mysqli_query($dbconn,$sql) or die(mysqli_error($dbconn));
//echo"<br>$sql";
header("location: cartdump.php");
}
else echo "Quantity is not valid<br>";
}
?>
<html>
<head><title>Order Page</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
</script>
<img class="banner" src="Images/acme_banner.jpg" alt="Acme Spook Shoppe"/>
</head>
<body>
<br><br>
<a href= "home.php">Home</a>
<form name="form1" method='POST'>
<?php
//echo $prod; //<------To print results
$sql = "select productid,name,imagefile, "
."shortdescription, longdescription,"
."unitprice "
."from products "
."where productid='$prod'";
echo "<br>";
//echo $sql; //<------To print results
//2. Run a query against the database
$result = mysqli_query($dbconn,$sql)
or die(mysqli_error($dbconn));
//print_r($result); //<------To print results
//3. Return the results
echo "<table class=ordertable >";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[1]<br><br>";
echo "$row[3]<br><br>";
echo "$row[4]<br><br>";
echo "Price: $row[5]<br><br>";
echo "<input type='text' name='txtqty' value='1' size=2 maxlength=2>";
echo "<input type='submit' name='btnadd' value='Add To Cart' OnClick='SubmitForm'>";
echo "</td>";
echo "<td><img src='images/products/$row[2]' "
."height=300px width =250px></td>";
echo "</tr>";
echo "</table>";
//print_r($row);
}
//4. Release the resources
mysqli_free_result($result);
//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>