更新sql中的数据时代码中的PHP错误

时间:2017-12-07 04:38:16

标签: php mysql

我收到错误未定义的变量名称,地址和ID虽然我已经定义了它。我使用了isset函数,但它无法正常工作。此外,一些HTML代码出现在名称和地址字段中。

 <?php
$link=mysqli_connect("localhost","root","");
$select=mysqli_select_db($link,'first_db');

if(isset($_POST['update'])){
     $id=$_POST['id'];
     $query="SELECT * FROM list WHERE id='$id'";
     $run=mysqli_query($link,$query);
     $row=mysqli_fetch_array($run);
 }
if(isset($_POST['name'])&& isset($_POST['address'])){
    $name=$_POST['name'];
    $address=$_POST['address'];
    $id = $_POST['id'];
    $query="UPDATE list SET name='$name',address='$address' WHERE id='$id'";
    $result=mysqli_query($link,$query);
}
else{
    echo"The fields cannot be empty";
}

?>

HTML代码为:

  <html>
    <head>    
        <title>Edit Data</title>
    </head>

    <body>
        <a href="index.php">Home</a>
        <br/><br/>

        <form name="form1" method="post" action="update.php">
            <table border="0">
                <tr> 
                    <td>Name</td>
                    <td><input type="text" name="name" value="<?php echo $name;?>"></td>
                </tr>
                <tr> 
                    <td>address</td>
                    <td><input type="text" name="address" value="<?php echo $address;?>"></td>
                </tr>

                <tr>
                   <td><input type="hidden" name="id" value="<?php echo $_GET['id'];?>"></td>
                    <td><input type="submit" name="update" value="Update"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>

6 个答案:

答案 0 :(得分:0)

请替换以下行

<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>

通过

<td><input type="hidden" name="id" value="<?php echo $_GET['id'];?>"></td>

您在值属性中错过了“”。

修改 更正后,您的HTML将是正确的,然后请检查$id=$_GET['update'];未定义我认为您需要将其更正为$id=$_POST['id'];

答案 1 :(得分:0)

get方法中,如果您从某个地方重定向,那么如果您使用update = {{id}}在URL中传递ID,请检查URL参数,因为您从id字段获取id,以便更新您应在网址

中包含id = {{id}}参数
if(isset($_POST['name'])&& isset($_POST['address'])){
    $name=$_POST['name'];
    $address=$_POST['address'];
    $id = $_POST['id'];
    $query="UPDATE list SET name='$name',address='$address' WHERE id='$id'";
    $result=mysqli_query($link,$query);
}

你需要从post变量(隐藏id字段)获取id。

答案 2 :(得分:0)

if(isset($_POST['id']))
{
$name=$_POST['name'];
$address=$_POST['address'];
$id = $_POST['id'];
$query="UPDATE list SET name='".$name."',address='".$address."' WHERE id='".$id."'";
$result=mysqli_query($link,$query);
}

答案 3 :(得分:0)

您的解决方案如下

if(isset($_GET['update'])){
         $id=$_POST['id'];//use this insted of $id=$_GET['update'] in your form name of id parameter is id
         $query="SELECT * FROM list WHERE id='$id'";
         $run=mysqli_query($link,$query);
         $row=mysqli_fetch_array($run);
     }


if(isset($_POST['name'])&& isset($_POST['address'])){
        $id=$_POST['id'];
        $name=$_POST['name'];
        $address=$_POST['address'];
        $query="UPDATE list SET name='$name',address='$address' WHERE id='$id'";
        $result=mysqli_query($link,$query);

    }

答案 4 :(得分:0)

<?php
$link=mysqli_connect("localhost","root","");
$select=mysqli_select_db($link,'first_db');

if(isset($_POST['update'])){
     $id=$_POST['id'];
     $query="SELECT * FROM list WHERE id='$id'";
     $run=mysqli_query($link,$query);
     $row=mysqli_fetch_array($run);
 }
if(isset($_POST['name'])&& isset($_POST['address'])){
    $name=$_POST['name'];
    $address=$_POST['address'];
    $id = $_POST['id'];
    $query="UPDATE list SET name='$name',address='$address' WHERE id='$id'";
    $result=mysqli_query($link,$query);
}
else{
    echo"The fields cannot be empty";
}

?>

html代码是:

<html>
<head>    
    <title>Edit Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form name="form1" method="post" action="update.php">
        <table border="0">
            <tr> 
                <td>Name</td>
                <td><input type="text" name="name" value="<?php echo $name;?>"></td>
            </tr>
            <tr> 
                <td>address</td>
                <td><input type="text" name="address" value="<?php echo $address;?>"></td>
            </tr>

            <tr>
               <td><input type="hidden" name="id" value="<?php echo $_GET['id'];?>"></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>

答案 5 :(得分:0)

我遇到了问题,所以我再次在这里发布一个单独的答案:

您需要更新一些内容,

<?php
$link=mysqli_connect("localhost","root","");
$select=mysqli_select_db($link,'first_db');

if(isset($_GET['update'])){  // Checking if the query parameter is available.
     $id=$_GET['update'];    // Getting the query string value here.
     $query="SELECT * FROM list WHERE id='$id'";
     $run=mysqli_query($link,$query);
     $row=mysqli_fetch_array($run);
 }
if(isset($_POST['name'])&& isset($_POST['address'])){
    $name=$_POST['name'];
    $address=$_POST['address'];
    $id = $_POST['id'];
    $query="UPDATE list SET name='".$name."',address='".$address."' WHERE id='".$id."'";  // Added `""` here for all variables.
    $result=mysqli_query($link,$query);
}
else{
    echo"The fields cannot be empty";
}

?>

请注意,在上面的代码中,您将获得$ row变量中的查询结果。所以在HTML中也使用它。像这样:

<html>
<head>    
    <title>Edit Data</title>
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form name="form1" method="post" action="update.php">
        <table border="0">
            <tr> 
                <td>Name</td>
                <td><input type="text" name="name" value="<?php echo (isset($row['name']))? $row['name'] : ''?>"></td>
            </tr>
            <tr> 
                <td>address</td>
                <td><input type="text" name="address" value="<?php echo (isset($row['address']))? $row['address'] : ''?>"></td>
            </tr>

            <tr>
               <td><input type="hidden" name="id" value="<?php echo (isset($_GET['id']))? $_GET['id'] : ''?>"></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>