你好,所有这些都是我的言论,所以感谢所有帮助学习PHP和SQL
我有3页,但我认为您唯一需要的页面是 Page 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次。我有2行代码,一个是错误,另一个是修复。但是我想了解为什么这条代码会:
//if ( mysqli_query( $conn, $sql_i ) ) { // <----This was my bug
将使我的代码循环,运行两次或插入数据库2次。修复程序是替换该行的代码使用该变量SO:
if ( $result_i ) { // <----this is my fix
我花了一段时间才找到,因为我正在寻找不止一次运行的东西,而不是if语句。
答案是"我不知道这是一个错误"。我完全理解,但是如果有答案,我想了解是否有任何陈述可以运行不止一次?我认为如果语句将使用代码为真,而不是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;
}
}
}
您两次调用该函数,因此插入两次是正常的。
您第一次执行插件是在$result_i = mysqli_query( $conn, $sql_i );
处,然后将其插入结果存储到变量$ result_i中。
现在,您没有检查该插入的结果(存储在$ result_i中),而是再次运行插入物,并在if ( $result_i ) {
上检查结果。
您看到,而不是检查第一个插入的结果,而是检查第二个插入的结果。
您的代码中没有循环。您只需执行INSERT
查询两次:
// query executed first time, record added
$result_i = mysqli_query( $conn, $sql_i );
// query executed second time, record added again
if ( mysqli_query( $conn, $sql_i ) ) { // <----This was my bug
所以,确实可以解决方案:
// query executed first time, record added
$result_i = mysqli_query( $conn, $sql_i );
// NO second query here
if ( $result_i ) {
redirect_to( "profile_image_fetch_image.php?id=$S_id" );
} else {
echo "Error: " . $sql_i . "<br>" . mysqli_error( $conn );
}
或:
// $result_i = mysqli_query( $conn, $sql_i ); // remove this line
// query executed first time, record added, NO more queries here
if ( mysqli_query( $conn, $sql_i ) ) {
redirect_to( "profile_image_fetch_image.php?id=$S_id" );
} else {
echo "Error: " . $sql_i . "<br>" . mysqli_error( $conn );
}