无法从HTML表单中检索值对于PHP

时间:2014-04-05 10:22:33

标签: php html mysql sql

好的,所以我需要从表单中检索我的变量并将它们放在insert语句中 我可以将它们输入数据库。问题似乎是从我检索它们的地方 如果我以它的形式对它们进行硬编码,那么数据库或连接就没有问题,只有检索价格和isbn如下所示。

 <?php session_start();
 session_regenerate_id();
 ($_SESSION['count']) ? $_SESSION['count']++ : $_SESSION['count'] = 1;



?>

<!-- example for PHP 5.0.0 final release -->

<html>

 <head>
  <title>Session running</title>
 </head>

 <body>
 <h1> Book Details</h1>
 <a href="catalog.php?<?php echo( SID ); ?>">Go Back To Inventory</a>
 <hr>
 PHPSESSID = <?php echo session_id(); ?>
 <br>
 You have been here <?php echo( $_SESSION['count'] ); ?> times in this session
 <hr>


<html>
<head>
<title>Count Visits</title>
</head>

<body>
<h2></h2>

</body>
</html>



<?php require_once("Include/db_connect.php");

if (isset($_REQUEST['button'])) // submit was clicked
{

display_output_page();
}

?>
<?php






    $db_link = db_connect("project");



    $self = $_SERVER['PHP_SELF'];

if (isset($_REQUEST['submit'])) // submit was clicked
{

      display_output_page();
   }


?>









<?php


    $isbn = $_REQUEST['isbn'];
    $price = isset($_REQUEST['price']) ? $_REQUEST['price'] : '';
    $quantity = isset($_REQUEST['value']) ? $_REQUEST['value'] : '';












$fields = mysql_list_fields("project", "books");
$num_columns = mysql_num_fields($fields);


$query = "SELECT * FROM books WHERE isbn=".$isbn.";";
$result = mysql_query($query) or die("SQL failed");




echo '<table border="1">';



echo "<tr>";
for ($i = 0; $i < $num_columns; $i++)
{
   echo "<th>", mysql_field_name($fields, $i), "</th>";


}
  echo "<th> Quantity </th>";
    echo "<th> Add </th>";
echo "</tr>";


while ($row = mysql_fetch_assoc($result))
{



        echo "<tr>";
        echo "<td>".$row['isbn']."</td>";
        echo "<td>".$row['title']."</td>";
        echo "<td>".$row['author']. "</td>";
        echo "<td>".$row['pub']."</td>";
        echo "<td>".$row['year']."</td>";
        echo "<td>".$row['price']."</td>";
        echo "<td><img src='images/".$row['image']."'></td>";


//IN HERE IS THE PROBLEM, RETRIEVING THE isbn and price variables wont work



        echo '<form action="" method="POST">
    <td><input type = "text" value=1 style="width: 30px;" name =   "value"/></td>
    <input type="hidden" name="isbn"  value="<php echo $isbn ?>">
    input type="hidden" name="price"  value="<?php echo $price ?>">
    <td><input type = "submit" name = "button" value = "Add"></td>

                </form>

    ';



        echo "</tr>";








}
echo "</table>";





mysql_free_result($result);
mysql_close($db_link);
?>


<?php
function display_output_page()
{


 require_once("Include/db_connect.php");
 $db_link = db_connect("project");


//This is where i need to retrieve the isbn and price variables for my SQL insert

    $sessionID = session_id();
    $isbn = $_REQUEST['isbn'];
    $price = $_REQUEST['price'];
    $quantity =$_REQUEST['value'];


 $db_link = db_connect("project");
 $sql2 = "INSERT INTO cart(sessionID,isbn,quantity,price)
 VALUES('$sessionID','$isbn','$quantity','$price')";


 $result2 = mysql_query($sql2) or die ("<br> Could not execute SQL query");

 if($result2)
{

echo "You Have Added The Item To The Cart";

}


mysql_close($db_link);

}
?>

1 个答案:

答案 0 :(得分:0)

您缺少一些基本的语法元素。

<input type="hidden" name="isbn"  value="<?php echo $isbn; ?>">
<input type="hidden" name="price"  value="<?php echo $price; ?>">

另外,我建议您阅读有关基于PHP HTML表单处理的内容,因为您的代码中存在许多小错误。

http://www.w3schools.com/php/php_forms.asp