簡體   English   中英

上載數據並從兩個表中選擇它們,並在同一HTML表中顯示它們

[英]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_idimages表中,然后使用這樣的:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM