在php中检索数据

时间:2016-07-12 12:57:21

标签: php

我已创建了一个用于更新附件的页面。执行此操作时,如果附加了具有相同名称,大小,扩展名的文件,则无需更新附件表。这就是场景。这就是我试图做的事情:

  else if($mode == "attachment_update") {
            $id = intval(mysqli_real_escape_string($mysqli, $_REQUEST["_id"]));
            $upload_directory = "upload/attachment/";
            $result = file_upload("attachment", "../".$upload_directory);

            $file_name = '".addslashes($result[file_name])."';
write_log($file_name);
            $file_extension = '".$result[file_extension]."';
write_log($file_extension);
            $file_size = '".$result[file_size]."';
write_log($file_size);
            $uploaded_file_name = '".$result[uploaded_file_name]."';
write_log($uploaded_file_name);
            $uploaded_file_path = '".$upload_directory.$result[uploaded_file_name]."';
write_log($uploaded_file_path);

            $query_select = "SELECT
                              file_name,
                              file_extension,
                              file_size,
                              uploaded_file_name,
                              uploaded_file_path
                            FROM
                              attachments
                            WHERE
                              id = 'id';";
            $result1 = mysqli_query($mysqli, $query_select) or throwexception(mysqli_error($mysqli));                 
            $row = mysqli_fetch_row($result1);

            write_log($row[0]);
            write_log($row[1]);
            write_log($row[2]);
            write_log($row[3]);
            write_log($row[4]);


            if($row[0] == $file_name && 
               $row[1] == $file_extension && 
               $row[2] == $file_size && 
               $row[3] == $uploaded_file_name && 
               $row[4] == $uploaded_file_path) 
            { 
                write_log("inside if"); 
            } else {
                if($result[status] == true) {
                        $query = "UPDATE
                                  attachments
                                SET
                                  file_name = '".addslashes($result[file_name])."',
                                  file_extension = '".$result[file_extension]."',
                                  file_size = '".$result[file_size]."',
                                  uploaded_file_name = '".$result[uploaded_file_name]."',
                                  uploaded_file_path = '".$upload_directory.$result[uploaded_file_name]."',
                                  recorded_by = '$recorded_by',
                                  recorded_datetime = '$recorded_datetime'
                                WHERE
                                  id = 'id';";
                        mysqli_query($mysqli, $query) or throwexception(mysqli_error($mysqli));
                    }
            }


            echo json_encode(array("message" => "Updated successfully"));
            exit;
        }

if条件做了什么。如果全部为真,则表格不会更新。如果任何一个失败,表格将会更新。

这里的问题是$ file_name,$ file_extension,$ file_size,$ uploaded_file_name都是null。我不知道如何检索它。有人可以告诉如何检索这些数据,以便if可以使用if条件检查吗?

2 个答案:

答案 0 :(得分:0)

在您的情况下,您不需要触发选择查询。只需在更新查询中添加AND条件即可。

if ($mode == "attachment_update") {
    $id = intval(mysqli_real_escape_string($mysqli, $_REQUEST["_id"]));
    $upload_directory = "upload/attachment/";
    $result = file_upload("attachment", "../" . $upload_directory);

    $file_name = '".addslashes($result[file_name])."';
    write_log($file_name);
    $file_extension = '".$result[file_extension]."';
    write_log($file_extension);
    $file_size = '".$result[file_size]."';
    write_log($file_size);
    $uploaded_file_name = '".$result[uploaded_file_name]."';
    write_log($uploaded_file_name);
    $uploaded_file_path = '".$upload_directory.$result[uploaded_file_name]."';
    write_log($uploaded_file_path);

    $query = "UPDATE
                          attachments
                        SET
                          file_name = '" . addslashes($result[file_name]) . "',
                          file_extension = '" . $result[file_extension] . "',
                          file_size = '" . $result[file_size] . "',
                          uploaded_file_name = '" . $result[uploaded_file_name] . "',
                          uploaded_file_path = '" . $upload_directory . $result[uploaded_file_name] . "',
                          recorded_by = '$recorded_by',
                          recorded_datetime = '$recorded_datetime'
                        WHERE
                          id = 'id'
                          and file_name <> '" . addslashes($result[file_name]) . "',
                          and file_extension = '" . $result[file_extension] . "',
                          and file_size = '" . $result[file_size] . "',
                          and uploaded_file_name = '" . $result[uploaded_file_name] . "',
                          and uploaded_file_path = '" . $upload_directory . $result[uploaded_file_name] . "',
;";
    mysqli_query($mysqli, $query) or throwexception(mysqli_error($mysqli));

    echo json_encode(array("message" => "Updated successfully"));
    exit;
}

答案 1 :(得分:0)

你的问题需要更清晰。

  

你能分享这个功能,   $ result = file_upload(&#34;附件&#34;,&#34; ../"。$ upload_directory);

您是否能够记录$ filename和$ row的值?

  

write_log($ FILE_NAME);

  

write_log($行[0]);