简体   繁体   English

我有一个PHP表单,如何为输入字段提供当前page_id的值?

[英]I have a PHP form, how can I give an input field a value of current page_id?

I have a PHP form where I have a hidden input field. 我有一个PHP表单,其中有一个隐藏的输入字段。 I need it to have a value of current page_id, that goes to my SQL table in a column called page_id. 我需要它具有当前page_id的值,该值进入我的SQL表中名为page_id的列中。

My images table: images = image_id, page_id, image_text, image_file; 我的图片表: images = image_id, page_id, image_text, image_file; My pages table: pages = page_id, page_name; 我的页面表: pages = page_id, page_name;

Example of my page URL: mywebsite/page.php?id=4 我的页面网址示例: mywebsite/page.php?id=4

Form (page.php): 表格(page.php):

<form method="POST" action="page.php" enctype="multipart/form-data">
     <input type="hidden" name="page_id" value="<?php echo $page['page_id']; ?>">
        <div>
          <input type="file" name="image">
        </div>
        <div>
          <textarea 
            id="text" 
            cols="40" 
            rows="4" 
            name="image_text" 
            placeholder="Write something here.."></textarea>
        </div>
        <div> 
            <button type="submit" name="upload">POST</button>
        </div>
</form>

classes.php file: classes.php文件:

class Page {
    public function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM pages");
        $query->execute();

        return $query->fetchAll();
    }    
    public function fetch_data($page_id) {
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM pages WHERE page_id = ?");
        $query->bindValue(1, $page_id);
        $query->execute();

        return $query->fetch();
    }
}

With the code I have right now, it gives me a random page_id=6, no idea where the 6 comes from.. 使用我现在拥有的代码,它给了我一个随机的page_id = 6,不知道6的来源。

What I want: image page_id = page_id of the page where the image was posted. 我想要的是:图像的page_id =发布图像的页面的page_id。

Any help is appreciated! 任何帮助表示赞赏!

$page on your form isn't defined. 表单上的$page未定义。

Use $_GET to pull the id from the URL. 使用$_GET从URL中提取ID。

Your hidden input should change to the following: 您的隐藏输入应更改为以下内容:

<input type="hidden" name="page_id" value="<?php echo $_GET['id'] ?>" />

暂无
暂无

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

相关问题 如何获取我的页面的facebook page_id? - how can i get facebook page_id for my page? 如何在输入表单字段中插入当前日期作为值 - How can i insert current date as a value in an input form field 如何使用PHP在下拉菜单中仅显示具有相同page_id的元素? 如何在foreach循环中编写“ if”语句? - How can I show only elements with same page_id in my dropdown menu with PHP? How do I write the 'if' statement in foreach loop? 我如何确定条件表达式中要在PHP中使用的输入字段类型“ submit”的当前ID? - How can I determine the current ID of the input field type “submit” for use in a PHP else if conditional expression? 使用childthemes时如何在php中添加page_id? - How to add a page_id in a php, when using childthemes? 如果页面上有两种形式的POST方法,如何使用PHP获取输入字段的值 - how can I get the value of a input field using PHP if there are two forms on the page both of POST method 我想根据 page_id 显示不同的内容,但是在每个页面上'echo page_id'结果都是一样的 - I want to display different content based on page_id, but on every page 'echo page_id' result is the same PHP 未定义变量 $page_id - 包括 - PHP undefined variable $page_id - Includes 将输入表单的值添加到当前字段值并更新PHP中的字段 - Add value of input form to current field value and update field in PHP Woocommerce | 如果page_id = .. {} - Woocommerce | If page_id= .. { }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM