PHP循环与下拉选择和提交按钮?

时间:2016-01-11 15:50:27

标签: php mysql loops menu

我正在尝试创建一个循环,从sql表中获取有关产品的其他信息 - MySQL table

然后创建一个看起来像这样的页面 - Webpage

到目前为止我有这个代码确实显示它但我无法弄清楚如何更新下拉菜单的名称,所以当我按提交它写入另一个SQL表的产品的名称然后多少个这些产品是由客户选择的。

<?php

$sql = "SELECT * FROM product";
$result = $conn->query($sql);
while ( $row = mysqli_fetch_assoc($result) ) {
 $columnValues[] = $row['ProductID'];           
foreach($columnValues as $key => $value) {
$$key = $value;
while ($row = $result->fetch_assoc()) {
echo "<tr>\n". "<br>";
echo "##product-ID## ";
echo "<td>".$row['ProductID']. "</td>\n";                           
echo "  ##product-name## ";
echo "<td>".$row['ProductName']."</td>\n";                          
echo "<td>\n";                              
echo "  ##dropdown## ";
echo "<select id=$value>\n";
echo "<option value='1'>1</option>\n";
echo "<option value='2'>2</option>\n";
echo "<option value='3'>3</option>\n";
echo "<option value='4'>4</option>\n";
echo "<option value='5'>5</option>\n";
echo "<option value='6'>6</option>\n";
echo "</select>\n";
echo "</td>\n";     
echo "</tr>\n";
 } 
    } 
        }

        $conn->close();                         
?>
<html>
 <body>
<form method="POST" action="#" >
<input type="submit" name="Submit" value="Submit" /><br>
</form>                                             </body>
</html>

我知道我很可能需要第二个PHP脚本来捕获帖子,所以对此的帮助也将非常感激。

/////////////////////////////////////////////// /

所以到了这里我得到了它 - 它循环并显示所有产品。当我按下提交时,它仅添加循环中的最后一个产品,并且它不关心下拉菜单 - 只需添加&#34; 2&#34;

脚本编写的表很简单 - 4列OrderID1,productid1,ProductName1 orderedqnt1

提前致谢。

的index.php

<html>
<body>
  <form method="POST" action="insert.php" >
<?php

session_start(); // session start for Variables to add to the sql in Insert.php
include("global.php"); // Stores the session Variables

## Conection part 

$sql = "SELECT * FROM product";
$result = $conn->query($sql);

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

  $columnValues[] = $row['ProductID'];
  foreach($columnValues as $key => $value) {
    $$key = $value;

while ($row = $result->fetch_assoc()) {
  echo "<tr>\n". "<br>";
  echo "##product-ID## ";
  echo "<td>".$row['ProductID']. "</td>\n";
  echo "  ##product-name## ";
  echo "<td>".$row['ProductName']."</td>\n";
  echo "<td>\n";
  /* echo "  ##dropdown## "; */

  echo "<select id=" . $value . " name='dropdown'>\n";  

  echo "<option value=''>-</option>\n";
  echo "<option value='1'>1</option>\n";
  echo "<option value='2'>2</option>\n";
  echo "<option value='3'>3</option>\n";
  echo "<option value='4'>4</option>\n";
  echo "<option value='5'>5</option>\n";
  echo "<option value='6'>6</option>\n";
  echo "</select>\n";
  /* echo "</td>\n"; */
  /* Echo ":::::value variable = "."$value"; */
  echo "</tr>\n" . "<br>";

print_r ($value);


  $_SESSION['GrabIDses']=mysqli_real_escape_string($conn,$row['ProductID']);  //Grabs the ID of the product in Session Variable
  $_SESSION['GrabNameses']=mysqli_real_escape_string($conn,$row['ProductName']); //Grabs the Name of the product in Session Variable
  $_SESSION['GrabSKUses']=mysqli_real_escape_string($conn,$row['SKU']); //Grabs the SKU of the product in Session Variable
  $_SESSION['Ordered']=mysqli_real_escape_string($conn,$value); //Grabs the Ordered Quantity for  the product in Session Variable ????????????????

  /* $GrabID = mysqli_real_escape_string($conn,$row['ProductID']);
  $GrabName = mysqli_real_escape_string($conn,$row['ProductName']);
  $GrabSKU = mysqli_real_escape_string($conn,$row['SKU']);
  echo "----------------------"."$_SESSION['GrabSKUses']"."<br>"."$_SESSION['GrabIDses']"."<br>"."----------------------"; */

    }
  }
}
$conn->close();
echo "<br>";
?>

<input type="submit" name="Submit" value="Submit" /><br>
  </form>
</body>

Insert.php

  <?php
session_start(); // session start

$getvalue = $_SESSION['GrabIDses']; // session get
$getvalue1 = $_SESSION['GrabNameses']; // session get
$getvalue2 = $_SESSION['GrabSKUses']; // session get
$ordered11 = $_SESSION['Ordered']; // session get

echo $getvalue;
echo "||";
echo $getvalue1; 
echo "||";
echo $getvalue2;
echo "||"."<br>";
print_r($_SESSION);

## Connection Part
   if(isset($_POST['dropdown'])) {

        echo("You order was completed" . "<br>");           
        $sql = "INSERT INTO testorder (productid1,ProductName1,orderedqnt1) VALUES ('$getvalue', '$getvalue1','$ordered11')";



        if (mysqli_query($conn, $sql)) 
            { echo "New record created successfully"; }     

        else 
            { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
        mysqli_close($conn);
    }

else {
echo" dhur";
}
?>

2 个答案:

答案 0 :(得分:0)

echo "<select id=$value>\n"; 

需要一个名字。所以改为

echo "<select id=" . $value . " name='dropdown'>\n";     

然后你需要制作第二页,使用

if(isset($_POST['dropdown'])) {

然后使用信息插入或更新表。我想你可以很容易地解决这个问题:)。

编辑:

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

$columnValues[] = $row['ProductID'];
foreach($columnValues as $key => $value) {
$$key = $value;

while ($row = $result->fetch_assoc()) {

你正在使用2个while循环。这是1对多。

答案 1 :(得分:0)

&lt; select&gt; -Boxes必须位于表单内部和网页内部。

在&lt;中输入名称属性选择&gt; -Tag,使保存脚本中的数据可用于$ _POST

我不确定,您想做什么,所以我不知道$ row [&#39; ProductID&#39;]是否是合理的名称。

<html>
<body>
  <form method="POST" action="#" >
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM product";
$result = $conn->query($sql);

while ( $row = mysqli_fetch_assoc($result) ) {
  $columnValues[] = $row['ProductID'];
  foreach($columnValues as $key => $value) {
    $$key = $value;
    while ($row = $result->fetch_assoc()) {
      echo "<tr>\n". "<br>";
      echo "##product-ID## ";
      echo "<td>".$row['ProductID']. "</td>\n";
      echo "  ##product-name## ";
      echo "<td>".$row['ProductName']."</td>\n";
      echo "<td>\n";
      echo "  ##dropdown## ";
      echo "<select id='$value' name='{$row['ProductID']}'>\n";
      echo "<option value='1'>1</option>\n";
      echo "<option value='2'>2</option>\n";
      echo "<option value='3'>3</option>\n";
      echo "<option value='4'>4</option>\n";
      echo "<option value='5'>5</option>\n";
      echo "<option value='6'>6</option>\n";
      echo "</select>\n";
      echo "</td>\n";
      /* Echo ":::::value variable = "."$value"; */
      echo "</tr>\n";
    }
  }
}
$conn->close();
?>

    <input type="submit" name="Submit" value="Submit" /><br>
  </form>
</body>
</html>
<body>
  <form method="POST" action="#" >
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM product";
$result = $conn->query($sql);

while ( $row = mysqli_fetch_assoc($result) ) {
  $columnValues[] = $row['ProductID'];
  foreach($columnValues as $key => $value) {
    $$key = $value;
    while ($row = $result->fetch_assoc()) {
      echo "<tr>\n". "<br>";
      echo "##product-ID## ";
      echo "<td>".$row['ProductID']. "</td>\n";
      echo "  ##product-name## ";
      echo "<td>".$row['ProductName']."</td>\n";
      echo "<td>\n";
      echo "  ##dropdown## ";
      echo "<select id='$value' name='{$row['ProductID']}'>\n";
      echo "<option value='1'>1</option>\n";
      echo "<option value='2'>2</option>\n";
      echo "<option value='3'>3</option>\n";
      echo "<option value='4'>4</option>\n";
      echo "<option value='5'>5</option>\n";
      echo "<option value='6'>6</option>\n";
      echo "</select>\n";
      echo "</td>\n";
      /* Echo ":::::value variable = "."$value"; */
      echo "</tr>\n";
    }
  }
}
$conn->close();
?>

    <input type="submit" name="Submit" value="Submit" /><br>
  </form>
</body>
</html>