简体   繁体   English

如何正确地将一个PHP变量传递给另一个PHP页面?

[英]How to correctly pass a PHP variable to another PHP page?

I have an android app where it uploads the file along with other variables using PHP to a server. 我有一个Android应用程序,其中使用PHP将文件以及其他变量上传到服务器。 I want to insert the uploaded file name(from the PHP file) to the PHP file which handles the inserting of variables to the database. 我想将上传的文件名(从PHP文件)插入到处理文件到数据库的插入的PHP文件中。

I want to get file_path to the another php file. 我想获得另一个PHP文件的file_path。 UploadToServer.php UploadToServer.php

<?php
session_start();

   $file_path = "uploads/";

   $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
   if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
   $_SESSION['image'] = "$file_path";
       echo "success";
   } else{
       echo "fail";
   }

?>

create_product.php create_product.php

<?php
session_start();
/*
 * Following code will create a new report row
 * All report details are read from HTTP Post Request
 */

// array for JSON response
$response = array();
$image = $_SESSION['image'];

// check for required fields
if (isset($_POST['name']) && isset($_POST['location']) && isset($_POST['description']) && isset($_POST['type'])) {

    $type = $_POST['type'];
    $name = $_POST['name'];
    $location = $_POST['location'];
    $description = $_POST['description'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO reports(type, name, location, description, image) VALUES('$type', '$name', '$location', '$description', '$file_path')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "report successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

Please tell me what's wrong. 请告诉我怎么了。

There should be no session involved at all, you need to combine the 2 scripts: 根本不应该涉及任何会话,您需要结合以下两个脚本:

<?php
// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['location']) && isset($_POST['description']) && isset($_POST['type'])) {

    $file_path = "uploads/";

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success save to db";
        $type = $_POST['type'];
        $name = $_POST['name'];
        $location = $_POST['location'];
        $description = $_POST['description'];

        // include db connect class
        require_once __DIR__ . '/db_connect.php';

        // connecting to db
        $db = new DB_CONNECT();

        // mysql inserting a new row
        $result = mysql_query("INSERT INTO reports(type, name, location, description, image) 
                               VALUES('$type', '$name', '$location', '$description', '$file_path')");

        // check if row inserted or not
        if ($result) {
            // successfully inserted into database
            $response["success"] = 1;
            $response["message"] = "report successfully created.";

            // echoing JSON response
            echo json_encode($response);
        } else {
            // failed to insert row
            $response["success"] = 0;
            $response["message"] = "Oops! An error occurred.";

            // echoing JSON response
            echo json_encode($response);
        }
    } else{
       echo "fail to upload will not save to DB";
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

Please note: 请注意:

  • This code will insert the record in the database only, it it successfully uploaded the file. 此代码仅将记录插入数据库中,它成功上传了文件。
  • mysql is deprecated mysql已弃用
  • use mysqli or PDO, and prepare query to avoid SQL injection 使用mysqli或PDO,并准备查询以避免SQL注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM