我有以下代码并且它成功符文并且说数据已成功输入但是当我检查数据库表时,每次我插入数据(尽管操作成功)我得到一个空白行。 Inshort没有插入数据,尽管代码表示操作成功
CODE: 的 dbconnect.php
<?php
$host= "host";
$user = "user";
$password = "pass";
$db = "db";
$con = mysqli_connect($host,$user,$password,$db);
if (!$con)
{ echo "Failed to Connect"; }
else
{ echo "Connection successful";}
?>
CODE:的 addInfo.php
<?php
require "dbconnect.php";
$username = $_POST["username"];
$email = $_POST["email"];
$mobile = $_POST["mobile"];
$flag = false;
$sql = "insert into userInfo values('$username','$email','$mobile');";
$flag=mysqli_query($con,$sql);
if($flag!==false)
{
echo "Data inserted successfully!";
}
else
{
echo "Error in insertion" . mysqli_error($con);
}
?>
CODE:的的index.php
<!DOCTYPE html>
<html>
<head>
<title>Add Information</title>
</head>
<body>
<form action="addinfo.php">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"/></td>
<td>Email:</td>
<td><input type="text" name="email"/></td>
<td>Mobile:</td>
<td><input type="text" name="mobile"/></td>
</tr>
</table>
<input type ="submit" value="Submit" />
</form>
</body>
</html>
你们可以做任何类型的帮助,请继续,
答案 0 :(得分:2)
I)更改
$username = $_POST["username"];
更改为$username = $_POST["name"];
method="POST"
<form>
II)更新了代码
<强> dbconnect.php 强>
<?php
$host= "host";
$user = "user";
$password = "pass";
$db = "db";
$con = new mysqli($host, $user, $password, $db);
if ($con->connect_errno) {
echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
}
?>
<强> addInfo.php 强>
<?php
require "dbconnect.php";
$stmt = $mysqli->prepare("INSERT INTO userInfo (`username`,`email`,`mobile`) VALUES (?, ?, ?)");
$stmt->bind_param('sss', $_POST["name"], $_POST["email"], $_POST["mobile"]);
if($stmt->execute()) {
echo "Data inserted successfully!";
} else {
echo "Error in insertion" . $con->errno;
}
?>
<强>的index.php 强>
<!DOCTYPE html>
<html>
<head>
<title>Add Information</title>
</head>
<body>
<form action="addinfo.php" method="POST">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"/></td>
<td>Email:</td>
<td><input type="text" name="email"/></td>
<td>Mobile:</td>
<td><input type="text" name="mobile"/></td>
</tr>
</table>
<input type ="submit" value="Submit" />
</form>
</body>
</html>
III)快速入门
IV)看看