"未定义的变量" PHP / mysqli代码中的错误

时间:2014-08-11 09:58:25

标签: php sql mysqli

我试图使用这个PHP将数据插入我的数据库。我认为它将数据发送到数据库,但它也显示错误消息:

Notice: undefined variable: name in ...

我试图使用MySQLi,这对我来说是新的。那么也许有更好的方法将数据插入我的数据库?

    <?php
    $con=mysqli_connect("localhost","root","","school");

    if(mysqli_connect_error()){
        echo "Failed to connect to MySQL:" . mysqli_connect_error();
    }
    if(isset($_POST['submit'])){
         $name = $_POST['firstname'];
         $last = $_POST['lastname'];
         $call = $_POST['phone'];
         $num = $_POST['roll'];
    }

    $query="insert into student(fast_name,last_name,phone,roll) values('$name','$last','$call','$num')";

    if(mysqli_query($con, $query)){
        echo "<h1>data added</h1>";
    }
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <table align="center" border="1" style="width:400px">
        <form action="index.php" method="post">
            <tr>
                <td>FastName:</td>
                <td><input type="text" name="firstname" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type="text" name="lastname" /></td>
            </tr>
            <tr>
                <td>Phone:</td>
                <td><input type="text" name="phone" /></td>
            </tr>
            <tr>
                <td>roll:</td>
                <td><input type="text" name="roll" /></td>
            </tr>
            <tr>
                <td><input type="submit" name="submit" value="submit" /></td>
            </tr>
            </form>
        </table>
            <table align="center" border="1" style="width:400px">   
        </table>
    </body>
    </html>

4 个答案:

答案 0 :(得分:1)

看看这个:

 if(isset($_POST['submit'])){
         $name = $_POST['firstname'];
         $last = $_POST['lastname'];
         $call = $_POST['phone'];
         $num = $_POST['roll'];
    }
 $query="insert into student
       (fast_name,last_name,phone,roll)
        values
        ('$name','$last','$call','$num')";

如果未设置$_POST['submit'],则未设置$name。但是您尝试在if。

之后访问$name

答案 1 :(得分:0)

尝试在isset()

中执行查询
    if(isset($_POST['submit'])){
        $name = $_POST['firstname'];
        $last = $_POST['lastname'];
        $call = $_POST['phone'];
        $num = $_POST['roll'];
        $query="insert into student(fast_name,last_name,phone,roll) values('$name','$last','$call','$num')";
        if(mysqli_query($con, $query)){
            echo "<h1>data added</h1>";
        }
    }

答案 2 :(得分:0)

因为你的if语句,PHP分析代码并通过通知,所以有一种情况是$name变量不会定义(如果未设置提交)。尽管提交了表单,但在if语句之后使用$name变量。

更改为此并将解决:

if(isset($_POST['submit'])){
     $name = $_POST['firstname'];
     $last = $_POST['lastname'];
     $call = $_POST['phone'];
     $num = $_POST['roll'];

     $query="insert into student(fast_name,last_name,phone,roll)  values('$name','$last','$call','$num')";

     if(mysqli_query($con, $query)){
         echo "<h1>data added</h1>";
     }
}

答案 3 :(得分:0)

修复错误或在代码开头使用error_reporting(E_ALL & ~E_NOTICE);

隐藏通知错误
error_reporting = E_ALL & ~E_NOTICE
你的php.ini中的

(想想重启你的apache)。