即使使用索引值访问变量行也未定义

时间:2018-10-29 14:15:12

标签: php

我正在从db获取数据并在此处更新它,并尝试发送回db。错误是未定义的变量行。我正在使用索引值而不是它们的名称进行访问。

<!DOCTYPE HTML>
<?php
    include_once('DBConnection.php');

    if( isset($_GET['edit']) )
    {
        $id = $_GET['edit'];
        $res= mysql_query("SELECT * FROM details WHERE id='$id'");
        $row= mysql_fetch_array($res);
    }

    if( !empty($_POST['newName'])&& isset($_POST['newName'])&& !empty($_POST['email'])&& isset($_POST['email'])&&!empty($_POST['phonenumber'])&& isset($_POST['phonenumber']))
    {
        $newName = $_POST['newName'];
        $EMail = $_POST['email'];
        $PhoneNumber = $_POST['phonenumber'];
        $id      = $_POST['id'];
        $sql     = "UPDATE details SET name='$newName' email='$EMail' phonenumber='$PhoneNumber' WHERE id='$id'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=random.php'>";
    }

?>
<form action="edit.php" method="POST">
PhoneNumber: <input type="text" name="phonenumber" value="<?php echo $row[3];?>"/><br />
EMail: <input type="text" name="email" value="<?php echo $row[2];?>"/><br />
Name: <input type="text" name="newName" value="<?php echo $row[1];?>"/><br />
SlNo<input type="hidden" name="SlNo" value="<?php echo $row[0];?>"/>
<input type="submit" value=" Update "/>
</form>

3 个答案:

答案 0 :(得分:0)

您尚未执行SELECT查询,而是在第二个if中执行了该查询。没有定义$row变量,但您以表格的形式谈论它。

答案 1 :(得分:0)

在这种情况下,您需要确保在使用$row之前已定义它。试试这个:

<!DOCTYPE HTML>
<?php
    include_once('DBConnection.php');

    if( isset($_GET['edit']) )
    {
        $id = $_GET['edit'];
        $res= mysql_query("SELECT * FROM details WHERE id='$id'");
        $row= mysql_fetch_array($res);
    }

    if( !empty($_POST['newName'])&& isset($_POST['newName'])&& !empty($_POST['email'])&& isset($_POST['email'])&&!empty($_POST['phonenumber'])&& isset($_POST['phonenumber']))
    {
        $newName = $_POST['newName'];
        $EMail = $_POST['email'];
        $PhoneNumber = $_POST['phonenumber'];
        $id      = $_POST['id'];
        $sql     = "UPDATE details SET name='$newName' email='$EMail' phonenumber='$PhoneNumber' WHERE id='$id'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=random.php'>";
    }

?>
<form action="edit.php" method="POST">
PhoneNumber: <input type="text" name="phonenumber" value="<?php echo (isset($row)? $row[3]: "");?>"/><br />
EMail: <input type="text" name="email" value="<?php echo (isset($row)? $row[2]: "");?>"/><br />
Name: <input type="text" name="newName" value="<?php echo (isset($row)? $row[1]: "");?>"/><br />
SlNo<input type="hidden" name="SlNo" value="<?php echo (isset($row)? $row[0]: "");?>"/>
<input type="submit" value=" Update "/>
</form>

答案 2 :(得分:0)

query()方法在失败时应返回FALSE,这是一个布尔值,没有行...

在尝试访问结果行之前,请检查它是否为数组:

$res = mysql_query($sql);
if (!is_array($res)) {
    return ('error');
}
$row = mysql_fetch_array($res);

并确保$row存在,如以利沙所说。

也可以连接字符串和变量:PHP - concatenate or directly insert variables in string(请注意,您的方法也许有效,我不知道)