[英]PHP inserting data to mySQL two times instead of one
<?php
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "test";
$name="admin";
$pass="passerino";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else {
echo nl2br("Conected \n");
}
$sqlin="INSERT INTO data_test (username, password) VALUES ('valname','valpass')";
$result=$conn->query($sqlin);
if ($conn->query($sqlin) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
$conn->close();
include_once "read.php";
?>
Hello i have this simple .php file to insert data into mySQL database,but instead of once it does it two times with one request.How can i stop this?I am using wamp server x64,chrome,php 5.5.12; 您好,我有这个简单的.php文件,可将数据插入到mySQL数据库中,而不是一次通过一个请求将其插入两次。我该如何停止呢? PS read.php displays the data table PS read.php显示数据表
change this code 更改此代码
$sqlin="INSERT INTO data_test (username, password) VALUES ('valname','valpass')";
$result=$conn->query($sqlin);
if ($conn->query($sqlin) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
$conn->close();
include_once "read.php";
to 至
$sqlin="INSERT INTO data_test (username, password) VALUES ('valname','valpass')";
$result=$conn->query($sqlin);
if ($result === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
$conn->close();
include_once "read.php";
Because if you run this line $conn->query($sqlin);
因为如果运行此行$conn->query($sqlin);
twice it will run your query twice and insert record in database two time 两次它将运行您的查询两次,并两次在数据库中插入记录
Because you are execution your query times 因为您正在执行查询时间
$result=$conn->query($sqlin);// first time
if ($conn->query($sqlin) === TRUE) {// second time
Just execute it only one time 只需执行一次
$sqlin="INSERT INTO data_test (username, password) VALUES ('valname','valpass')";
if ($conn->query($sqlin) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
Don't insert value directly better use bind statement 不要直接插入值,最好使用bind语句
http://php.net/manual/en/mysqli-stmt.bind-param.php http://php.net/manual/en/mysqli-stmt.bind-param.php
Don't store plain password into database read password hashing 不要将普通密码存储到数据库读取密码哈希中
http://php.net/manual/en/function.password-hash.php http://php.net/manual/en/function.password-hash.php
http://php.net/manual/en/faq.passwords.php http://php.net/manual/en/faq.passwords.php
Do not take result variable directly do as follows : 不要直接采用结果变量,请执行以下操作:
if ($conn->query($sqlin) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlin . "<br>" . $conn->error;
}
只需在您的if条件中使用变量
if($sqlin === TRUE)
$conn->query($sqlin) === TRUE
导致使用$result === TRUE
的双重输入。
yes, it's a great issue. 是的,这是一个很大的问题。 you have to check view file. 您必须检查查看文件。 That's means, focus on your file where you called class. 这就是说,专注于您调用class的文件。 You called 2 times. 你打了两次 If you are not using any success message. 如果您没有使用任何成功消息。 It should to ok, but if you use it, so please remove another call. 应该可以,但是如果您使用它,那么请删除另一个电话。 Suppose - 假设-
Example: 例:
$_SESSION ['success'] = "Data save successfully";
or 要么
$success_msg = "Data save successfully";
(from main public function) (来自主要公共职能)
you have to call on view file - 您必须调用查看文件-
$save_blog_info = new Blog();
$success_msg = $save_blog_info->save_blog_info($_POST);
don't use - 不要使用-
$save_blog_info = new Blog();
$save_blog_info->save_blog_info($_POST);
$success_msg = $save_blog_info->save_blog_info($_POST);
thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.