我得到了一个带有图片和提交按钮的表单,该表单将连接用户的user_id和他们想要保存的图片的picture_id发送到MySQL数据库中称为pin的表中。我试图固定来自不同用户的不同图片。即使我单击不同图片下面的提交,也可以插入user_id,但picture_id保持不变(图片表中的最后一张图片)。是因为表格,还是我用错误的方式提及它?
我将带有图片和按钮的php代码保存在这里。
<?php
session_start();
require_once __DIR__.'/connect.php';
$sUsername = $_SESSION['txtUsername'];
try {
$stmt = $db->prepare('SELECT * FROM pictures INNER JOIN users ON users.user_id = pictures.user_id');
$stmt->execute();
$aRows = $stmt->fetchAll();
foreach ($aRows as $aRow) {echo
"<form action='display.php' method='POST'>
<div class=item><img src=images/$aRow->path>
<span class=caption>Picture id:$aRow->picture_id</span>
<input type='submit' name='submit'>
</div>
</form>";}
if (isset($_POST['submit'])) {
try {
$stmt = $db->prepare('SELECT * FROM users WHERE username = :sUserName');
$stmt->bindValue(':sUserName', $sUsername);
$stmt->execute();
$row = $stmt->fetch();
$iUserId = $row->user_id;
$stmt = $db->prepare('INSERT INTO pins VALUES( null, :iUserId, :iPictureId )');
$stmt->bindValue(':iUserId', $iUserId);
$stmt->bindValue(':iPictureId', $aRow->picture_id);
$stmt->execute();
}catch (PDOEXception $ex){
echo $ex;
}
}
}catch (PDOEXception $ex){
echo $ex;
}
?>