向 MySQL 添加值

我试图在将注册表单的所有内容放入数据库之前测试对 MySQL 数据库的写入,但这是错误的。有人可以检查一下我的 PHP 代码,看看我哪里出错了。


<html>

    <body>

        <form action="testingPHP.php" method="post">

            Name: <input type="text" name="Name"><br>

            Age: <input type="text" name="Age"><br>

            Gender: <select name="Gender">

                <option value=" "> </option>

                <option value="Boy">Boy</option>

                <option value="Girl">Girl</option>    

            </select><br>

            <input type ="submit">

        </form>

    </body>

</html>

    

PHP:


$Name = $_.POST['Name'];

$Age = $_.POST['Age'];

$Gender = $_.POST['Gender'];


$servername = "localhost";

$username = "testing";

$password = "testing1";

$db = "tesingdb";


$conn new mysql($sername, $username, $password, $db);


$sql = "insert into testingtable (name,age,gender)

values('$Name','$Age','$Gender')"


$conn->close();


哔哔one
浏览 196回答 1
1回答

素胚勾勒不出你

以下是变化。已删除testingPHP.php自<form action="testingPHP.php" ...>$_.POST到$_POST$conn = new mysqli(....)代替$conn new mysql(....)添加$conn->query($sql);拼写错误$servername其余代码<form action="" method="post">&nbsp; &nbsp; Name: <input type="text" name="Name"><br>&nbsp; &nbsp; Age: <input type="text" name="Age"><br>&nbsp; &nbsp; Gender: <select name="Gender">&nbsp; &nbsp; &nbsp; <option value=" "> </option>&nbsp; &nbsp; &nbsp; <option value="Boy">Boy</option>&nbsp; &nbsp; &nbsp; <option value="Girl">Girl</option>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </select><br>&nbsp; &nbsp; <input type ="submit"></form><?php$Name = $_POST['Name'];$Age = $_POST['Age'];$Gender = $_POST['Gender'];$servername = "localhost";$username = "testing";$password = "testing1";$db = "tesingdb";$conn&nbsp; = new mysqli($servername, $username, $password, $db);$sql = "insert into testingtable (name,age,gender) values('$Name','$Age','$Gender')";$conn->query($sql);$conn->close();?>
打开App,查看更多内容
随时随地看视频慕课网APP