我想问你如何从两个不同的表中选择数据并在单个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
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");
}
?>
点击上传按钮后,图像上传到"images"文件夹,但没有数据上传到数据库。表是空的
我想要一张这样的表:
********************************************
username | age | gender | uploaded image
1.user | 15 | male | image
2.user2 | 14 | female | image
********************************************
有人能帮我一下吗? 我不打算回答"上传的事情",因为这不是问题的主题。我也不对代码结构、SQL注入、使用捷克语命名变量和其他问题发表评论。请看@tadman的评论
作为JOIN,您连接的id都是自动递增的。这没有多大意义。您应该在images
表中定义一个类似user_id
的列,然后使用如下命令:
SELECT *
FROM table1
JOIN table2 ON table1.ID = table2.user_id
ORDER BY table1.ID
我已经找到解决办法了。
错误在标签中。我使用了action="upload.php"属性,但是upload.php只关心上传图像,而不是整个表单的记录。
因此,我删除了action="upload.php"属性,并将PHP改进的代码从upload.php放入包含整个表单INSERT函数的文件中。
谢谢你的建议和警告:)