我是PHP的新手,但被告知要创建一个连接到Mysql数据库的两个实体表单。我尝试使用以下代码连接到数据库,但数据未插入。有什么问题吗?
我如何将表数据放入mysql表?所有文件都在下面列出。
索引文件:
<html>
<head>
<title> Insert operations </title>
</head>
<body>
<form action="" method = "post">
<table align= "center" style= "margin-top: 100px;">
<table>
<tr>
<td>Name :</td> <td><input = "text" name = "name" /><td>
</tr>
<tr>
<td>Email Id :</td> <td><input type= "email" name = "email" /><td>
</tr>
<tr>
<td colspan="2" align = "center"><input type= "submit" name = "submit" />
<td>
</tr>
</table>
</body>
</html>
PHP文件:
<?php
include_once('insert.php');
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$conn=mysqli_connect('localhost','XXX','XXX','insertdb');
$query= "INSERT INTO `insert_record`(`name`, `email`) VALUES
('$name','$email')";
// query run knre k lye function ha
$run = mysqli_query($conn,$query);
if ($run == true){
echo "Data insert sucessfully";
}
else {
echo "error";
}
}
?>
连接文件:
<?php
$host= 'localhost';
$username= 'XXX';
$password= 'XXX';
$dbname = 'insertdb';
$conn= new mysqli($host,$username,$password,$dbname);
if ($conn->connect_error)
{
die('Connection failed : '.$conn->connect_error);
}
?>
数据未插入