2015-10-15 62 views
0

我試圖讓點擊「提交」按鈕時更新「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

什麼問題?它不更新? – hzq

+0

您的語句中定義了$ row [0]'$ itemid = $ _POST [$ row [0]];'? – Twisty

+0

您的代碼也容易受到SQL注入的影響。 – Twisty

回答

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> 
+1

也許你想編輯這個到你的問題,而不是發佈一個答案。 :) – hzq

相關問題