向 MySQL 输入数据失败


Input data to MySQL fails

我一直在尝试将一些信息发送到mysql数据库,但它不断返回输入数据失败。这是我的代码

<?php
    $Firstname='';
    $Lastname='';
    $Email='';
    $Birthcountry='';
    $Phone='';
    $Occupation='';

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "canadavisa";
    $tblname = "canadav";
    // Create connection
    mysql_connect("$servername","$username","$password");
    mysql_select_db("$dbname");

    //the example of inserting data with variable from HTML form
    //input.php
    //mysql_connect("localhost","username","");//database connection
    if(isset($_POST['submit'])){
    //mysql_select_db("canadavisa"); 
    $Firstname = $_POST['Firstname'];
    $Lastname = $_POST['Lastname'];
    $Email = $_POST['Email'];
    $Birthcountry = $_POST['Birthcountry'];
    $Phon e= $_POST['Phone'];
    $Occupation = $_POST['Occupation'];
    }
    //inserting data into mysql database

    $order="INSERT INTO canadav(Firstname,Lastname,Email,Birthcountry,Phone,Occupation)VALUES('$Firstname','$Lastname','$Email','$Birthcountry','$Phone','$Occupation')";

    //declare in the order variable
    $result=mysql_query($order);  //order executes
    if($result){
        echo("<br>Input data is successful");
        echo "<a href='insert.php'>Back to main page</a>";
    } else{
        echo("<br>Input data is fail");
    }
    ?>

这里有一个语法错误:

$Phon e= $_POST['Phone'];

有一个额外的空间,删除它。

$Phone= $_POST['Phone'];

注意

  • 不要使用 mysql 函数,它们已弃用 mysqli 或 PDO 代替。

  • 转义变量以避免 sql 注入,或使用预准备语句。