PHP:记录未在SQL中更新

时间:2016-01-27 13:24:44

标签: php mysql sql-update

我的问题是更新SQL记录。它正确地将SQL数据提取到表单中(用于编辑)但是当我按下保存编辑按钮时,它会在输入字段内返回以下错误:

注意:未定义的变量:行在 C:\ xampp \ htdocs \ edit.php 46
请告诉我如何修复它

<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "zz224466";
$database = "zain";
$conn = mysqli_connect($servername,$username,$password,$database);
if($conn->connect_error) {
    die("connection failed: " . $conn->connect_error);
}


if(isset($_GET['edit'])) {
    $id = $_GET["edit"];  //Get id of sql table from other php page.
    echo $id; //It gives true result. It means that $_GET method above gets id of sql table correctly


    $res = mysqli_query($conn, "SELECT * FROM product where product_id=$id");



    if ($res == FALSE) {
        die("Error");

    }
    $row = mysqli_fetch_array($res);// Getting row from sql of specific id above selected above




    if (isset($_POST['Edit'])) {  ///Checking if Edit button has been pressed
        $product_category = $_POST['product_category'];
        $product_id = $id;

//// SQL query
        $sql_category = "UPDATE product SET product_category='$product_category' WHERE product_id=$id";
        if (mysqli_query($conn, $sql_category)) {

        }

    }
}
    ?>

////////////////////HTML FORM/////////////////////////
<form method="post" action ="edit.php" id="contact-form">


    <input type="text" name="product_category" placeholder="product_category" value="<?php echo $row['product_category'];//It prints sql record in input field which is to be updated and it prints correctly. But when I press edit button it gives above mentioned error ?>"/>


    <div class="btn-group" role="group">
        <input type="submit" class="btn btn-default" name="Edit" value="Save Edits" style="margin-top: 15px; margin-right: 15px; border-radius: 4px;">

    </div>
</form>
</body>
</html>

请告诉我如何解决它

1 个答案:

答案 0 :(得分:1)

试试这个。因为,当你按下提交按钮。它会带有POST值且没有GET参数的 edit.php (在按下Edit提交按钮后。因此,浏览器无法找到{{1}导致它,没有$id值。)

$row

例如<input type="text" name="product_category" placeholder="product_category" value="<?php if(isset($row['product_category'])) { echo $row['product_category'];}?>"/>

https:www.example.com/edit.php?edit=1按钮后,submit更改为

URL

所以,没有https:www.example.com/edit.php

更新代码

edit=1更改为

<form>

除了之前我做过的事情。

完整更新的代码(请参阅我编写的行<form method="post" action ="edit.php?edit=<?echo $_GET['edit'];?>" id="contact-form">

Change Here