PHP MySQLi在表中插入的行太多
我需要使用PHP和MySQLi将HTML表单中的数据插入数据库 但是我的代码工作不正常。它将相同数据的几行插入表中 我到处寻找答案,但没有任何帮助。我是新手,所以请帮我找出问题所在。如果有更好的方法做某事,我也希望看到 多谢各位 connect.php:PHP MySQLi在表中插入的行太多,php,html,mysqli,Php,Html,Mysqli,我需要使用PHP和MySQLi将HTML表单中的数据插入数据库 但是我的代码工作不正常。它将相同数据的几行插入表中 我到处寻找答案,但没有任何帮助。我是新手,所以请帮我找出问题所在。如果有更好的方法做某事,我也希望看到 多谢各位 connect.php: $link = mysqli_connect("localhost", "root", "", "tutorial"); // Check connection if ($link === false) { die("ERROR: Co
$link = mysqli_connect("localhost", "root", "", "tutorial");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
else {
// echo "Connected. ";
}
<?php
// Connection
include("connect.php");
?>
<form action="insert.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="firstname" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="lastname" id="lastName">
</p>
<p>
<label for="Country">Country:</label>
<select name="country_id" id="Country">
<?php
$sql = mysqli_query($link, "SELECT * FROM country");
while ($row = $sql->fetch_assoc()) {
unset($country, $name);
$country = $row['country_id'];
$name = $row['name'];
echo '<option value="'.$country.'">'.$name.'</option>';
}
?>
</select>
</p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Connection
include("connect.php");
// Prepare an insert statement
$sql = "INSERT INTO user (country_id, firstname, lastname) VALUES (?,?, ?)";
if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $country, $firstname, $lastname);
// Set the parameters
$country = $_REQUEST['country_id'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
echo "Records inserted successfully.";
}
else {
echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
}
}
else {
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
?>
form.php:
$link = mysqli_connect("localhost", "root", "", "tutorial");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
else {
// echo "Connected. ";
}
<?php
// Connection
include("connect.php");
?>
<form action="insert.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="firstname" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="lastname" id="lastName">
</p>
<p>
<label for="Country">Country:</label>
<select name="country_id" id="Country">
<?php
$sql = mysqli_query($link, "SELECT * FROM country");
while ($row = $sql->fetch_assoc()) {
unset($country, $name);
$country = $row['country_id'];
$name = $row['name'];
echo '<option value="'.$country.'">'.$name.'</option>';
}
?>
</select>
</p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Connection
include("connect.php");
// Prepare an insert statement
$sql = "INSERT INTO user (country_id, firstname, lastname) VALUES (?,?, ?)";
if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $country, $firstname, $lastname);
// Set the parameters
$country = $_REQUEST['country_id'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
echo "Records inserted successfully.";
}
else {
echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
}
}
else {
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
?>
名字:
姓氏:
国家:
insert.php:
$link = mysqli_connect("localhost", "root", "", "tutorial");
// Check connection
if ($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
else {
// echo "Connected. ";
}
<?php
// Connection
include("connect.php");
?>
<form action="insert.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="firstname" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="lastname" id="lastName">
</p>
<p>
<label for="Country">Country:</label>
<select name="country_id" id="Country">
<?php
$sql = mysqli_query($link, "SELECT * FROM country");
while ($row = $sql->fetch_assoc()) {
unset($country, $name);
$country = $row['country_id'];
$name = $row['name'];
echo '<option value="'.$country.'">'.$name.'</option>';
}
?>
</select>
</p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Connection
include("connect.php");
// Prepare an insert statement
$sql = "INSERT INTO user (country_id, firstname, lastname) VALUES (?,?, ?)";
if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sss", $country, $firstname, $lastname);
// Set the parameters
$country = $_REQUEST['country_id'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
echo "Records inserted successfully.";
}
else {
echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
}
}
else {
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
?>
您正在调用mysqli\u stmt\u execute($stmt)两次不确定是否有帮助,但请尝试更改此设置:
1 - mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
2 - if(mysqli_stmt_execute($stmt)){
echo "Records inserted successfully.";
}
您可以删除数字1。
mysqli\u stmt\u执行
返回true或false,将返回的值分配给var(更多信息)
替换这个
mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
echo "Records inserted successfully.";
}
用这个
$execution = mysqli_stmt_execute($stmt);
// Attempt to execute the prepared statement
if($execution){
echo "Records inserted successfully.";
}
您正在调用mysqli_stmt_execute()2两次 下面是如何用面向对象的风格来实现它 connect.php:
<?php
$mysqli = new Mysqli("localhost", "root", "", "tutorial");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
?>
insert.php:
<?php
// Connection
require_once "connect.php";
$query = "INSERT INTO user (country_id, firstname, lastname) VALUES (?,?, ?)";
if($stmt = $mysqli->prepare($query)) {
$country = $_REQUEST['country_id'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$stmt->bind_param('sss',$country,$firstname,$lastname);
if($stmt->execute()) {
echo 'Success ! ';
$stmt->close();
$mysqli->close();
} else {
echo "Could not execute query: " . $mysqli->error();
$stmt->close();
$mysqli->close();
}
} else {
echo 'Error preparing statement : ' . $mysqli->error;
$stmt->close();
$mysqli->close();
}
欢迎来到Stackoverflow!请花点时间看看如何提出好的问题。这也将帮助你得到最好的答案。()非常感谢,使用面向对象的方式更好吗?您需要保持代码的干净性和可理解性。最好的方法是使用oop,这样您就可以将类似的任务分组到类中,并消除重复代码的需要。