gpt4 book ai didi

php - 使用 PHP 将数据插入 MySQL 数据库出现错误

转载 作者:行者123 更新时间:2023-11-29 11:11:50 25 4
gpt4 key购买 nike

我正在尝试将数据从 HTML 表单和 PHP 脚本插入到 MySQL 数据库。

我可以从脚本连接到 MySQL 服务器,但无法连接到数据库和表。这样我就无法插入数据。

PHP 脚本触发的错误。错误是“未选择数据库!”。但我已经正确声明了名称。

这里我上传了full solution .

下面我添加了 HTML 表单和 PHP 脚本。

如果可能的话,请尝试解决这个问题。

HTML 表单

<html>
<head>
<title>Registration Form</title>
</head>
<body>
<br />
<h2 align="center">Register a new customer</h2>
<h3 align="center">Fill all fields</h3>
<form action="insertdata.php" method="post">
<table align="center">
<tr>
<td>First Name:</td>
<td><input type="text" id="fname" name="fname" /></td>
</tr>
<tr>
<td>Middle Name:</td>
<td><input type="text" id="mname" name="mname" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" id="lname" name="lname" /></td>
</tr>
<tr>
<td>NIC / Driving License No:</td>
<td><input type="text" id="NICNo" name="nicno" /></td>
</tr>
<tr>
<td>Permanent Address Name:</td>
<td><input type="text" id="paddress" name="paddress" /></td>
</tr>
<tr>
<td>E-Mail Address:</td>
<td><input type="text" id="email" name="email" /></td>
</tr>
<tr>
<td>Telephone:</td>
<td><input type="text" id="telno" name="telno" /></td>
</tr>
<tr>
<td>Mobile No:</td>
<td><input type="text" id="mobileno" name="mobileno" /></td>
</tr>
<tr>
<td>Date of Registration:</td>
<td><input type="text" id="regdate" name="regdate" /></td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr>
<td align="center"><input type="submit" />&nbsp;&nbsp;&nbsp; <input type="reset" /></td>

</tr>
</table>
</form>

</body>

InsertData.php

    <?php

$dbserver = 'localhost';
$username = 'root';
$password = '';

$dbname = 'car_rental';

$con = mysqli_connect($dbserver, $username, $password);

if(!$con)
{
echo "Not connected to server !";
echo 'ERROR : '.mysql_error();

}else{
echo "Server connected !";
}

if(!mysqli_select_db($con, $dbname));
{
echo "Database not selected !";
echo 'ERROR : '.mysql_error();
}

$fname = $_POST["fname"];
$mname = $_POST["mname"];
$lname = $_POST["lname"];
$nicno = $_POST["nicno"];
$paddress = $_POST["paddress"];
$email = $_POST["email"];
$telno = $_POST["telno"];
$mobileno = $_POST["mobileno"];
$regdate = $_POST["regdate"];

$sql = "INSERT INTO new_customers (FirstName, MiddleName, LastName, NICNo, PermanentAddress, EmailAddress, Telephone, Mobile, RegDate) VALUES ('$fname','$mname','$lname','$nicno','$paddress','$email','$telno','$mobileno','$regdate')";

if (!mysqli_query($con,$sql))
{
echo "Data not inserted !";
echo 'ERROR : '.mysql_error();
}
else
{
echo "Data inserted !";
}

header("refresh:300; url=form.php");

?>

最佳答案

尝试

mysqli_connect($dbserver, $username, $password, $dbname);

关于php - 使用 PHP 将数据插入 MySQL 数据库出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40436324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com