简体   繁体   English

上载数据并从两个表中选择它们,并在同一HTML表中显示它们

[英]Upload data and select them from two tables and display them in the same HTML table

I would like to ask you how to select data from two different tables and display them in the single HTML table. 我想问你如何从两个不同的表中选择数据并将它们显示在单个HTML表中。 I've tried many ways, but I can't find the right way. 我已经尝试了许多方法,但是找不到正确的方法。

First table 第一桌

Second 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 Upload code: 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");
}
?>

After clicking the Upload button is image uploaded to the "images" folder, but no data are uploaded to the database. 单击“上载”按钮后,图像被上载到“图像”文件夹,但是没有数据上载到数据库。 Tables are empty. 表是空的。

I would like to get table like this: 我想要这样的表:

********************************************
   username | age | gender | uploaded image
 1.user     | 15  | male   | image         
 2.user2    | 14  | female | image     
********************************************

Can someone help me please? 有人能帮助我吗?

I am not going to answer "the upload thing", as it is not the topic of the question. 我不会回答“上传内容”,因为这不是问题的主题。 I am not commenting on code structuring, SQL injections, use of Czech to name variables, and other issues, either. 我也没有评论代码结构,SQL注入,使用Czech来命名变量以及其他问题。 See @tadman's comments. 参见@tadman的评论。

As of JOIN, you join on IDs which are both autoincrement. 从JOIN开始,您加入都是自动递增的ID。 It does not make much sense. 这没有多大意义。 You should define a column like user_id in images table and then use something like this: 你应该这样定义列user_idimages表中,然后使用这样的:

SELECT *
FROM table1
JOIN table2 ON table1.ID = table2.user_id
ORDER BY table1.ID

I have already found a solution. 我已经找到了解决方案。

The error was in the tag. 错误出在标签中。 I used the action="upload.php" attribute, but upload.php cared only about uploading an image, not a recording of the entire form. 我使用action =“ upload.php”属性,但是upload.php只关心上传图像,而不关心整个表单的记录。

So I removed the action="upload.php" attribute and I put PHP ipmroved code from upload.php into the file that contains the function INSERT for entire form. 因此,我删除了action =“ upload.php”属性,并将来自上载PHP ipmroved的PHP代码放入了包含整个表格INSERT函数的文件中。

Thank you all for your advice and warnings :) 谢谢大家的建议和警告:)

暂无
暂无

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

相关问题 PHP从两个表中选择并加入它们? - PHP select from two tables and join them? 如何从两个 API 调用中获取数据,将它们合并并与我的其他数据一起显示在表格中? - How to get data from two API calls, merge them and display them in a table with my other data? 从2个表mysql中获取数据并显示它们 - bring data from 2 tables mysql and display them PHP / SQL-从两个数据库中的两个表中选择数据,然后按时间对其进行排序 - PHP/SQL - Select data from two tables, from two databases and sort them by time 从mysql中的两个表中选择并显示其中一个列中的所有行? - SELECT from two tables in mysql and display all the row's in the column in one of them? 从两个MYSql表中获取数据并显示为HTML表 - Fetch data from two MYSql tables and display as HTML table 从两个表中选择数据,它们之间根本没有公共字段 - Select Data from two tables where there is no common field between them at all 如何每次从SQL中的表中选择两行并在PHP中显示它们? - How to select two rows each time from a table in SQL and display them in PHP? 带有下拉菜单的表格可以从2个连接的mysql表中收集数据并将其保存到1个表中 - Form with dropdown select that collects data from 2 joined mysql tables and save them into 1 table 从两个表中获取数据并将其收集在数组中并计算一个表结果的行 - get data from two tables and collect them in array and count rows of one table result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM