使用while循环从表单更新数据库内容

时间:2014-01-12 00:54:22

标签: php sql database

我的用户拥有带字幕的个人资料照片。每张照片都以自动递增的ID存储在数据库表格中,因此为了显示单个用户的所有相关照片,我会查看与其用户编号相关的内容。

由于用户之间的照片数量不同,我不确定如何处理提交的表单。

是否可以将while循环回到数据库中以更新表?

生成表单详细信息的代码:

while($row = mysql_fetch_array($result))
                      {
                      echo '<div style="width:180px; height:210px; float:left;"><a href="../assets/uploads/resized_' . $row['photoLINK'] . '" class="fancybox-effects-d" title="' . $row['photoCAPTION'] . '"><img src="../assets/uploads/thumbnail_' . $row['photoLINK'] . '" alt="" class="profileImgsStretch"/></a>';
                      echo '<input type="text" name="caption' . $row['photoID'] . '" value="' . $row['photoCAPTION'] . '"></div>';
                      echo '<input type="hidden" name="picture" value="' . $row['photoID'] . '"></div>';

                        }

3 个答案:

答案 0 :(得分:0)

First you need to look at the structure of the database. if 1 user has many pictures you need to make a separate database table consisting of the user id and the photo id

your database structure should be like this 


myDB

table 1 -Users
table 2 -user_and_picture
            -user_id
            -picture_id
table 3 -Pictures

You insert the new picture into the pictures table
Then retrieve that picture id 
get the user id and the picture id and store it in the new table user_and_picture

When you want to retrieve the pictures that belong to lets say user 1 you run a query on table user_and_pictures

an example would be 
SELECT user_id, user_picture_id
FROM user_and_picture
WHERE user_id = 1

The result of this query will give you all the pictures associated with user #1

after that make a query that gets the filenames of those pictures 

答案 1 :(得分:0)

在你的html使用中:

<input type="text" name="caption[' . $row['photoID'] . ']" value="' . $row['photoCAPTION'] . '">

您可以删除隐藏的输入字段。 因此,在您的PHP文件中,您可以使用:

$captions = $_GET['caption'];
foreach ($captions as $picture_id => $caption){
    /*
        VERY IMPORT TIP ABOUT HACKING: For each caption, check if the picture_id belongs to the user BEFORE making the update. Otherwise a hacker can just change the picture id's and add malicious captions for other users!
        If the picture belongs to the uploading user, then continue.
    */

    echo 'caption for picture_id: '.$picture_id.' = '.$caption.'<br />'
}

答案 2 :(得分:0)

这就是数据表单的工作原理:

您的输入字段包含名为name的元素。它是您在back-end收到的输入的变量名称,即PHP页。

表单变量name可以是literal变量,也可以是array

所以例如:

登录:

<input type="text" name="username" />
<input type="text" name="password" />

to get it in PHP

$user =  $_POST["username"];
$pass =  $_POST["password"];

数组中的OR

<input type="text" name="loginData[username]" />
<input type="text" name="loginData[password]" />

to get it in PHP

$userData = $_POST["loginData"]; // $userData = Array(username, password)

OR

$user =  $_POST["loginData[username]"];
$pass =  $_POST["loginData[password]"];
相关问题