我是通过youtube频道之一完成的。我只是复制了所有代码,但仍无法正常工作。你有没有人能帮助我呢?我最后面对错误说不插入。
**index.php**
<html>
<head>
<title>Data entry practise</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="wrapper">
<form action="insert.php" method="_POST">
Name: <input type="text" name="username"><br><br>
Email Address: <input type="text" name="email"><br><br>
<input type="submit" value="insert">
</form>
</div>
</body>
</html>
现在这是inset.php代码部分。好像这里有些错误..请帮帮我
**insert.php**
<?php
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo "not connected";
}
if(!mysqli_select_db($con,'tutorial'))
{
echo 'database Note Selected';
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name,Email) VALUES ('$Name', '$Email')";
if(!mysqli_query($con, $sql))
{
echo "Not Inserted";
}
else
{
echo "Inserted";
}
?>
答案 0 :(得分:0)
问题是表单方法不正确。你必须在没有下划线的情况下编写它
**index.php**
<html>
<head>
<title>Data entry practise</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="wrapper">
<form action="insert.php" method="POST">
Name: <input type="text" name="username"><br><br>
Email Address: <input type="text" name="email"><br><br>
<input type="submit" value="insert">
</form>
</div>
</body>
</html>
修改强>
我认为问题出在你的mysql连接上。您需要在$ con变量上添加db,如下所示:
<强> insert.php 强>
$con = mysqli_connect('localhost','root','', 'tutorial');
if(!$con)
{
echo "not connected";
}
if(!mysqli_select_db($con,'tutorial'))
{
echo 'database Note Selected';
}
$Name = $_POST['username'];
$Email = $_POST['email'];
$sql = "INSERT INTO person (Name,Email) VALUES ('$Name', '$Email')";
if(!mysqli_query($con, $sql))
{
echo "Not Inserted";
}
else
{
echo "Inserted";
}
?>
我测试了你的代码并且它正在运行。
还有一件事,你需要将表单方法改为&#34; POST&#34;,没有下划线,这很重要。