[英]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.