简体   繁体   中英

Send hidden input with files in the same forms PHP

I want to send a hidden input data with a files in same form to a php script, but the problem is for some reason I cannot read the hidden data it's reading nothing:

<form id="C2" method="post" enctype="multipart/form-data" action='/phps/upload_file.php'>
    <input type="hidden" name="ID" value="C2" />
Profile picute <input type="file" name="photo01" /><br>
    Pic 2 <input type="file" name="photo02" /><br>
    Pic 3 <input type="file" name="photo03" /><br>
    <input type="submit" value="Send files" />
    </form>

The upload_file.php begining looks like this:

require_once('login.php');

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
    $ID= $_REQUEST['ID'];

    error_log("File ID:".$ID."\n\n"); // I see nothing in the log file

//*****Connect to SQL********

    $db_server = mysqli_connect($db_hostname, $db_username, $db_password);
        if(!$db_server) die("Unable to connect to MYSQL:" .mysqli_error());

    mysqli_select_db($db_server, $db_database)
        or die("Unable to select database:" . mysqli_error());

    $query = "SELECT current_pn,pic_path FROM partnum WHERE PNID = " . '$ID';
    $queried = mysqli_query($db_server, $query);

    $array = mysqli_fetch_array($queried);

When I check the log file I see nothing. Anyone has idea why?

Thanks, Zsolt

Just echo $_POST['ID'];die; and remove rest of the below codes and check are you able to get the value there, if so then the problem is in your rest of the codes. try to enable your error reporting so that you can easily check whats the real problem.

Show us what data you have in POST, and you will find your problem.

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
    var_dump($_POST);

ps isset($_POST) doesn't make sense since variable $_POST is always set, so simple if ($_POST) { is enough.

"Now I changed the variable name from ID to id_photo and it's working thanks."

As per my original comment:

"Try renaming your hidden form field name to id_photo , see if that helps. "

The use of ID as a variable is not a suggested value to use, because it could cause a conflict somewhere else, in a DB for instance.

EDIT

After going over the OP's code, the following code should have been used:

if(isset($_POST['ID']) instead of if(isset($_POST) since $_POST is an autoglobal and is always set.

It needs to check whether a value has been set and not a global POST value or as Glavić suggested in the answer given .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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