繁体   English   中英

PHP - 如何将字符串变量的内容用作 empty() 中的变量?

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

我正在用 PHP 做一个上传脚本,我想为每个图像生成一个表单。 所以我将图像 id 存储在 url 中,如upload-edit.php?&id[]=49&id[]=50 因为我希望这个系统是动态的,所以我这样做了:

这是为每个图像生成输入的脚本。

<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>

使用这个脚本,我将所有动态创建的输入存储在一个数组中。

$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]. "'". ']';
}

我想检查输入之一是否为空。

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

所以我想要'$_POST['。 “'标题-”。 $_GET['id'][$i]。 “'”。 ']' 被解释为 $_POST['title-49'] 例如。 请有人可以帮助我吗?

谢谢奈杰尔任!

问题可能是整个事情都在引号中 - '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' '$_POST['. "'title-". $_GET['id'][$i]. "'". ']' ,所以都是字符串,试试$_POST['title-'. $_GET['id'][$i]] $_POST['title-'. $_GET['id'][$i]]

暂无
暂无

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

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