如何向$ _SERVER [“ PHP_SELF”]提交ID

时间:2018-07-23 10:16:37

标签: php sql

我想使用$_SERVER["PHP_SELF"]提交表单,但是在提交表单时,它没有获取项目的ID,有没有办法添加如下所示的ID,而不必链接到另一个文件...

<form action="process.php<?php echo"?id=$productID" ?> " method="post"> 

肯定有一种简单的方法。.我花了很多时间在Google上搜索和搜索论坛,没有任何东西可以给我一个解释。

谢谢。

编辑

include "../model/functions_updateproducts.php";

function select_productspreparedGETIDt(){


    global $conn;


        $productID = $_GET['id'];
        $nameError = "";


$sql = "SELECT * FROM product WHERE productID = :productID";
$statement = $conn->prepare($sql);
$statement->bindValue(':productID', $productID);
$statement->execute();
$result = $statement->fetchAll();
$statement->closeCursor();

    foreach($result as $row):

      ?>


            <form action="<?php echo $_SERVER["PHP_SELF"];?>"  method="post">
                    <div class="form-group">
                    <input type="hidden" id="productID" name="productID" value="<?php echo $row['productID'] ?>" />
                    <label>Product Name</label>
                    <input type="text" class="form-control" id="productName" name="productName" placeholder="Enter Product Name" value="<?php echo $row['productName'] ?>"  /><span class="error"> <?php echo $nameError;?></span>
                    </div>
                    <div class="form-group">
                    <label>Quantity</label>
                    <input type="Number" class="form-control" id="QTY" name="QTY" placeholder="Enter Quantity" value="<?php echo $row['QTY'] ?>" min='1' max='100' >
                    </div>
                    <div class="form-group">
                    <label>Price</label>
                    <input type="text" class="form-control" id="productPrice" name="productPrice" placeholder="Enter Price" value="<?php echo $row['productPrice'] ?>" required/>
                    </div>
                    <div class="form-group">
                    <label>Variable</label>
                    <input type="text" class="form-control" id="Variable" name="Variable" placeholder="Enter Variable" value="<?php echo $row['Variable'] ?>" required>
                    </div>
                    <div class="form-group">
                    <label>Description</label>
                    <textarea class="form-control" id="productDescription"  name="productDescription" /><?php echo $row['productDescription'] ?></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Update Item</button>
            </form>


            <?php

                endforeach;
      }

      $nameError ="";

      if(isset($_POST['submit'])){
   if (empty($_POST["productName"])) {
     $nameError = "Name is required";
   } else {
     $name = test_input($_POST["productName"]);
     // check name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameError = "Only letters and white space allowed";
     }
   }

   $productID = $_GET['id'];
   $productName = $_POST['productName'];
   $productDescription = $_POST['productDescription'];
   $productPrice = $_POST['productPrice'];
   $QTY = $_POST['QTY'];
   $Variable = $_POST['Variable'];

     global $conn;

     $sql = "UPDATE product SET productName = :productName, productDescription = :productDescription, productPrice = :productPrice, QTY = :QTY, Variable = :Variable WHERE productID = :productID";
     $statement = $conn->prepare($sql);
     $statement->bindValue(':productName', $productName);
     $statement->bindValue(':productDescription', $productDescription);
     $statement->bindValue(':productPrice', $productPrice);
     $statement->bindValue(':productID', $productID);
     $statement->bindValue(':QTY', $QTY);
     $statement->bindValue(':Variable', $Variable);
     $result = $statement->execute();
     $statement->closeCursor();
     header("location: ../view/success.php");
     return $result;

   function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
   }
      ?>

      <?php

}
?>

2 个答案:

答案 0 :(得分:3)

在您的表单内输入以下代码:

<input type="hidden" value="<?php echo $productID ?>" name="id" />

这将提交隐藏字段。 PHP将您的$productID写入了该文件,并将其作为$_POST[]

的一部分提交

答案 1 :(得分:1)

<form action="process.php<?php echo"?id=$productID" ?> " method="post"> 

很好。用

访问productID
$_GET['id']