PHP变量不断重置

时间:2017-03-06 21:08:43

标签: php html sql forms

我正在尝试使用PHP来使用HTML表单更新SQL表。

我希望用户能够搜索VCR名称并在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>

1 个答案:

答案 0 :(得分:0)

您的PHP代码指的是一个不存在的POST变量:

$vcrName=$_POST['name'];

在更新表单中,您需要将其作为隐藏值传递:

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

目前尚不清楚这些是两个独立的PHP脚本(或者如果是这样,为什么会这样),因此您可能需要进行数据库调用以获取该值。从用户界面的角度来看,无论如何都要在更新记录时给出一个现有值。这意味着获取数据并为每个表单元素赋予value属性。