简体   繁体   中英

PHP - How can I use the content of a string variable as a variable in empty()?

I'm doing an upload script in PHP and I want a form to be generated for each image. So I store the images id in the url like upload-edit.php?&id[]=49&id[]=50 . Because I want this system to be dynamic, I did this :

Here is the script which generates the inputs for each image.

<div class="content upload">
    <h2>Upload Edit</h2>
    <?php
    $location = "upload-edit.php?";
    $countid = count($_GET['id']);
    for($i=0;$i<$countid;$i++){
        $location = $location. '&id[]='. $_GET['id'][$i];
    }
    ?>
    <form action="<?=$location?>" method="post" enctype="multipart/form-data">
        <?php
        $countid = count($_GET['id']);
        for($i=0;$i<$countid;$i++){
            $stmt = $con->prepare("SELECT path, folder_fk FROM some table WHERE id=?");
            $stmt->bind_param('s', $_GET['id'][$i]);
            $stmt->execute();
          $stmt->store_result();
            $stmt->bind_result($path, $folder_id);
            $stmt->fetch();
            $stmt->close();

            $stmt = $con->prepare("SELECT name FROM some table WHERE id=?");
            $stmt->bind_param('s', $folder_id);
            $stmt->execute();
          $stmt->store_result();
            $stmt->bind_result($foldername);
            $stmt->fetch();
            $stmt->close();

            echo '<img src="'. $foldername. '/'. $path. '" width="100px">
            <label for="title">Title</label>
            <input type="text" name="title-'. $_GET['id'][$i]. '" id="title">
            <label for="description">Description</label>
            <textarea name="description-'. $_GET['id'][$i]. '" id="description"></textarea>
            <label for="credit">Credit</label>
            <input type="text" name="credit-'. $_GET['id'][$i]. '" id="credit">';
        }
        ?>
       <input type="submit" value="Validate properties" name="submit">
    </form>

With this script I store in an array all the inputs dynamically created.

$formfields = array();
$countid = count($_GET['id']);
for($i=0;$i<$countid;$i++){
    $formfields[] = '$_POST['. "'title-". $_GET['id'][$i]. "'". ']';
    $formfields[] = '$_POST['. "'description-". $_GET['id'][$i]. "'". ']';
    $formfields[] = '$_POST['. "'credit-". $_GET['id'][$i]. "'". ']';
}

And I want to check if one of the inputs is empty.

$msg_temp = 'set';
for($i=0;$i<$countid;$i++){
    if (empty('$_POST['. "'title-". $_GET['id'][$i]. "'". ']')){
        $msg_temp = 'unset';
    }
}

So I would like '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' to be interpreted as a $_POST['title-49'] for example. Please someone can help me ?

Thank you Nigel Ren !

The problem may be that the whole thing is in quotes - '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' , so it's all a string, try $_POST['title-'. $_GET['id'][$i]] $_POST['title-'. $_GET['id'][$i]]

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