[英]Upload data and select them from two tables and display them in the same HTML table
我想问你如何从两个不同的表中选择数据并将它们显示在单个HTML表中。 我已经尝试了许多方法,但是找不到正确的方法。
HTML:
<html>
<body>
<div id="reg-area">
<form action="upload.php" method="post" align="center" id="register" enctype="multipart/form-data">
<input type="text" name="username" minlength="5" maxlength="25" required/><br /><br />
<input type="number" name="age" max="99" required/><br /><br />
<input type="radio" name="gender" value="male" id="r1" checked="checked">
<label for="r1">Muž</label>
<input type="radio" name="gender" value="female" id="r2">
<label for="r2">Žena</label><br /><br />
<input type="file" name="file_img" /><br /><br />
<input type="submit" name="btn_upload" value="Upload"/>
</form>
</div>
<?php
require_once('Db.php');
Db::connect('localhost', '***', '***', '***');
if ($_POST)
{
$existuje = Db::querySingle('
SELECT COUNT(*)
FROM table1
WHERE username=?
LIMIT 1
', $_POST['username']);
if ($existuje)
die ('<p align="center" class="element-animation"><font color="red">Uživatel s touto přezdívkou již existuje.</font></p>');
else
/*INSERT USERNAME, age, gender into Table1 */
$datum = date("Y-m-d H:i:s");
Db::query('
INSERT INTO table1 (username, age, gender, date)
VALUES (?, ?, ?, ?)
', $_POST['username'], $_POST['age'], $_POST['gender'], $datum);
header("location: index.php");
}
/*DISPLAY CONTENT OF BOTH TABLES*/
$snapy = Db::queryAll('
SELECT *
FROM table1
JOIN table2 ON `ID` = `img_ID`
ORDER BY ID;
');
echo('<div align="center" id="content"><h1>Seznam se s:</h1><table width="100%" cellspacing="10" collspacing="10"></div>');
echo('<th>Username</th><th>Age</th><th>Gender</th><th>Uploaded image</th>');
foreach ($snapy as $s)
{
echo('<tr align="center"><td>' . htmlspecialchars($s['username']));
echo('</td><td>' . htmlspecialchars($s['age']));
echo('</td><td>' . '<img src="img/' . $s['gender'] .'.png' .'" width="25px"/>');
echo('</td><td>' . '<img src="images/' . $filename . $filetype .'" width="25px"/>');
echo('</td></tr>');
}
echo('</table>');
?>
</div>
</body>
</html>
PHP上传代码:
<?php
if(isset($_POST['btn_upload']))
{
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filepath = "images/".$filename;
move_uploaded_file($filetmp,$filepath);
$sql = "INSERT INTO snapcodes (img_name,img_path,img_type) VALUES (?, ?, ?), '$filename','$filepath','$filetype')";
header("location: index.php");
}
?>
单击“上载”按钮后,图像被上载到“图像”文件夹,但是没有数据上载到数据库。 表是空的。
我想要这样的表:
********************************************
username | age | gender | uploaded image
1.user | 15 | male | image
2.user2 | 14 | female | image
********************************************
有人能帮助我吗?
我不会回答“上传内容”,因为这不是问题的主题。 我也没有评论代码结构,SQL注入,使用Czech来命名变量以及其他问题。 参见@tadman的评论。
从JOIN开始,您加入都是自动递增的ID。 这没有多大意义。 你应该这样定义列user_id
在images
表中,然后使用这样的:
SELECT *
FROM table1
JOIN table2 ON table1.ID = table2.user_id
ORDER BY table1.ID
我已经找到了解决方案。
错误出在标签中。 我使用action =“ upload.php”属性,但是upload.php只关心上传图像,而不关心整个表单的记录。
因此,我删除了action =“ upload.php”属性,并将来自上载PHP ipmroved的PHP代码放入了包含整个表格INSERT函数的文件中。
谢谢大家的建议和警告:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.