2017-03-06 146 views
0

我想用PHP來更新使用HTML表單的SQL表。PHP變量保持重置

我希望用戶能夠搜索錄像機名稱並在while循環中顯示它的詳細信息,然後會出現一個更新表單,供用戶在數據庫中更改其詳細信息。

但是每當我按下更新表單上的更新按鈕時,保留新細節的變量就會變空並變得未定義。

<?php require('connect.php'); ?> 
<?php require('headerPrivate.php'); ?> 
<?php require('session.php');?> 


<?php 
      //SEARCH PHP CODE 
      //THIS WORKS FINE AND ALL THE DETAILS APPEAR 
      if(isset($_POST["search"])) 
      { 

      //CREATE VARIABLES 
      $username=$_SESSION['username']; 
      echo "username: ".$username; 
      echo '<br>'; 

      $vcrName=$_POST['name']; 
      echo "VCR Name: ".$vcrName; 
      echo '<br>'; 
      echo '<br>'; 


      //SELECT * FROM PRODUCT 
      $sql="SELECT * 
        FROM product 
        INNER JOIN user 
        ON product.owner_ID=user.user_ID 
        WHERE username='$username' AND name='$vcrName'"; 
      echo "SQL SELECT 1: ".$sql; 
      echo '<br>'; 
      echo '<br>'; 

      //$vcrName=$_POST['name']; 


      $result = mysqli_query($con,$sql); 

       echo '<div class="row">'; 


        echo '<div class="col-xs-6 col-md-4">'; 
         while ($row_all = mysqli_fetch_assoc($result)) 
           { 
          echo '<form method="post">'; 
           echo "<u>Title: ".$row_all["name"].'</u>'; 
           echo '<br>'; 

           echo '<small>'; 
            echo " Price: ".$row_all["price"]; 
           echo '</small>'; 
           echo '<br>'; 

           echo "<p><u>Short Description:</u> ".$row_all["short_descripton"]."</p>"; 
           echo '<br>'; 

           echo "<p><u>Long Description:</u> ".$row_all["long_description"]."</p>"; 
           echo '<br>'; 

           echo '<hr>'; 
          echo '</form>'; 

        echo '<div>'; 

           } 
       echo '</div>'; 
      } 
?> 

     <content> 

      <!--SEARCH FOR VCR NAME--> 
      <form class="form" method="post"> 

       <label for="name" class="sr-only">VCR Name</label> 
       <input type="text" name="name" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off"> 

       <button name="search" type="search" class="btn btn-success btn-block">Search</button> 

      </form> 
<?php 

//This is where i run into issues. The old name in the variable $vcrName is empty and i need it for the update SQL statement. 


      //UPDATE PHP 
      if(isset($_POST["alter"])) 
      { 

       //CREATE A SESSION VARIABLE FOR THE CUSTOMER ID 
       $customer_ID=$_SESSION['customer_ID']; 
       echo "Customer ID: ".$customer_ID; 
       echo '<br>'; 


       //CREATE VARIABLES 
       $changeTitle=$_POST["titleChange"]; 

       $changesDescChange=$_POST["sDescChange"]; 

       $changelDescChange=$_POST["lDescChange"]; 

       $changepriceChange=$_POST["priceChange"]; 

       $vcrName=$_POST['name']; 

       //UPDATE SQL 
       $sql_update="UPDATE product 
          SET 
          name='$changeTitle', 
          short_descripton='$changesDescChange', 
          long_description='$changelDescChange', 
          price='$changepriceChange' 
          WHERE 
          owner_ID='$customer_ID' AND name='$vcrName'"; 

       echo "SQL Update 0: ".$sql_update; 
       echo '<br>'; 
       echo '<br>'; 


       echo "Updated Name: ".$changeTitle; 
       echo '<br>'; 
       echo '<br>'; 


       echo "SQL Update 1: ".$sql_update; 
       return $sql_update; 
       echo '<br>'; 
       echo '<br>'; 

       $result_update = mysqli_query($con,$sql_update); 

       if($result_update){ 
        echo "Update Successful!"; 
       } 

       else { 
        echo "Update Unsuccessful"; 
       } 

      }  

?> 
       <!--UPDATE FORM--> 
       <form class="form" method="post"> 

        <label for="titleChange" class="sr-only">VCR Name</label> 
        <input type="text" name="titleChange" class="form-control" placeholder="VCR Name" required="" autofocus="" autocomplete="off"> 

        <label for="sDescChange" class="sr-only">Short Description</label> 
        <input type="text" name="sDescChange" class="form-control" placeholder="Short Description" required="" autofocus="" autocomplete="off"> 

        <label for="lDescChange" class="sr-only">Long Description</label> 
        <input type="text" name="lDescChange" class="form-control" placeholder="Long Description" required="" autofocus="" autocomplete="off"> 

        <label for="priceChange" class="sr-only">Price</label> 
        <input type="text" name="priceChange" class="form-control" placeholder="Price" required="" autofocus="" autocomplete="off"> 

        <button name="alter" type="submit">Change</button> 

       </form> 
     </content> 
    </body> 
</html> 
+0

這就是Web編程的工作原理:來自瀏覽器的每個請求都是一個全新的請求,並且只包含瀏覽器發送的信息。使用隱藏的表單元素,cookie或會話(有效地,由cookie標識的文件,以便您可以找到返回的同一用戶)。 – IMSoP

+0

您的HTML「表單」中沒有任何「」元素。你不會在'$ _POST'中得到任何東西。 – miken32

+0

@ miken32咦?我在這兩個例子中都看到了很多''元素。一個在第一個,第四個在第二個。 – IMSoP

回答

0

你的PHP代碼是指一個POST變量不存在:

$vcrName=$_POST['name']; 

在你更新表單,你需要把它作爲一個隱藏的價值:

<input type="hidden" name="name" value="<?=htmlspecialchars($vcrName)?>"/> 

目前尚不清楚這些是兩個單獨的PHP腳本(或者如果是這樣,爲什麼它們),所以您可能需要進行數據庫調用才能獲得該值。從用戶界面的角度來看,無論如何,更新記錄時通常會給出現有值。這將意味着獲取數據並給每個表單元素一個value屬性。