大家好,这是我的第一页后十分感谢所有帮助学习PHP和SQLPHP错误 - IF语句所做插入运行两次
我有3个页面,但我认为你需要将唯一页面第2页是哪里出了问题,但我添加所有3如果我错了
1 HTML表单
<form method="POST" action="profile_image_getdata.php" enctype="multipart/form-data">
<input type="file" name="myimage">
<input type="submit" name="submit_image" value="Upload">
</form>
页2 profile_image_getdata.php(严重名)
<?php
session_start();
require_once("db/db_connection.php");
require_once("functions/page_control_func.php");
require_once("error/error_set.php");
if ($_SERVER[ 'REQUEST_METHOD' ] === 'POST') {
$S_id = $_SESSION[ "id" ];
$imagename = $_FILES[ "myimage" ][ "name" ];
//Get the content of the image and then add slashes to it
$imagetmp = addslashes(file_get_contents($_FILES[ 'myimage' ][ 'tmp_name' ]));
//Insert the image name and image content in image_table
$sql_i = "INSERT INTO image_table_test (`image_ID`, `id`, `name`, `image`) VALUES (NULL, '$S_id', '$imagename', '$imagetmp');";
/*mysqli_query($insert_image);*/
$result_i = mysqli_query($conn, $sql_i);
if (mysqli_query($conn, $sql_i)) { // <----This was my bug
//if ($result_i) { // <----this is my fix
redirect_to("profile_image_fetch_image.php?id=$S_id");
} else {
echo "Error: " . $sql_i . "<br>" . mysqli_error($conn);
}
}
我的问题:如果在将复制的插入2次到数据库中的上述 。我有两行代码,一个是bug,另一个是修复。但我想明白为什么这行代码:
//if (mysqli_query($conn, $sql_i)) { // <----This was my bug
会使我的代码回路,运行两次或插入到数据库2倍。 修复是更换该行代码使用变量,以便:
if ($result_i) { // <----this is my fix
我花了一段时间来寻找,因为我一直在寻找的运行速度比一次,而不是IF语句更多的东西。
如果答案是“我不知道这是一个错误。”我完全理解,但如果在那里有答案,我想了解IF语句如何使某些事情运行多次?我认为IF语句将使用代码,如果它是TRUE,而不是如果它是FALSE。不运行循环。
所以你在这里的全貌是最后一页
第3页profile_image_fetch_image.php
<?php
header("content-type:image/jpeg");
session_start();
require_once("db/db_connection.php");
if ($_SERVER[ 'REQUEST_METHOD' ] === 'GET') {
$G_id = $_GET[ 'id' ];
$sql_S = "SELECT * FROM image_table_test WHERE ID = $G_id;";
$result_S = mysqli_query($conn, $sql_S);
if (mysqli_num_rows($result_S) > 0) {
while ($row = mysqli_fetch_assoc($result_S)) {
$image_name = $row[ "name" ];
$image_content = $row[ "image" ];
echo $image_content;
}
}
}
你简单的使用两个tim mysqli_query($ conn,$ sql_S)..每当你使用这个函数你执行/执行查询时.. – scaisEdge
我看到了,即使它在if语句中它仍然可以运行功能。所以我的错误就像在if语句中使用one =设置变量,而不是使用==或=== .... if(a = b){ –