所以我试图让桌子显示骑车人骑着什么山(为MS150做好准备)。我的问题是,当一个人点击按钮说他们已经骑过山......它只在某些时候工作。当他们说他们已经完成了一个小山时,按钮会被禁用,并且他们不能再次推动它。但是,有些记录没有正确显示。这是我的代码:
HTML:
$get_hills = "SELECT * FROM hills ORDER BY hill_region ASC, hill_id ASC";
$run_hills = mysqli_query($con, $get_hills);
while($row_hills = mysqli_fetch_array($run_hills)) {
$hill_id = $row_hills['hill_id'];
$get_user = "SELECT * FROM users WHERE user_id = '".$_SESSION['user_id']."'";
$run_user = mysqli_query($con, $get_user);
$row_user = mysqli_fetch_array($run_user);
$get_ridden = "SELECT * FROM hills_ridden WHERE hill_id = '".$hill_id."'";
$run_ridden = mysqli_query($con, $get_ridden);
$row_ridden = mysqli_fetch_array($run_ridden);
echo "
<tr>
<td>".$row_hills['hill_name']."</td>
<td>".$row_hills['hill_region']."</td>
<td>".$row_hills['hill_rise']."</td>
<td>".$row_hills['hill_length']."</td>
<td>".$row_hills['hill_avg_grade']."</td>
<td>".$row_hills['hill_total_points']."</td>
<td><a href='".$row_hills['hill_link']."' target='_blank'><button class='btn btn-inverse btn-xs'>Link</button></a></td>
<td>";
if($row_user['user_id'] == $row_ridden['user_id']) {
echo "<button class='btn btn-default btn-xs' disabled>You Rode This</button>";
} else {
echo"
<form method='post' action=''>
<input type='hidden' name='hill_id' value='".$row_hills['hill_id']."'>
<input type='hidden' name='points_gained' value='".$row_hills['hill_total_points']."'>
<input type='hidden' name='user_id' value='".$row_user['user_id']."'>
<button type='submit' name='hill_sub' class='btn btn-inverse btn-xs'>I Rode This</button>
</form>";
} echo"</td>
</tr>";
}
php:
if(isset($_POST['hill_sub'])) {
$get_user = "SELECT * FROM users WHERE user_id = '".$_SESSION['user_id']."'";
$run_user = mysqli_query($con, $get_user);
$row_user = mysqli_fetch_array($run_user);
$insert_points = "INSERT INTO points (user_id, hill_id, points_gained, points_date) VALUES ('".$row_user['user_id']."', '".$_POST['hill_id']."', '".$_POST['points_gained']."', now())";
$insert_activity = "INSERT INTO activity (user_id, hill_id, activity_date) VALUES ('".$row_user['user_id']."', '".$_POST['hill_id']."', now())";
$insert_hill = "INSERT INTO hills_ridden (user_id, hill_id) VALUES ('".$row_user['user_id']."', '".$_POST['hill_id']."')";
$run_points = mysqli_query($con, $insert_points);
$run_activity = mysqli_query($con, $insert_activity);
$run_hills = mysqli_query($con, $insert_hill);
if($run_hills) {
echo "<script>window.open('record-hill.php?user_id=".$row_user['user_id']."', '_self')</script>";
} else {
echo $run_hills->error;
}
}
这里是sql db文件:
-- Table structure for table `hills_ridden`
--
CREATE TABLE IF NOT EXISTS `hills_ridden` (
`ridden_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(5) NOT NULL,
`hill_id` varchar(5) NOT NULL,
PRIMARY KEY (`ridden_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=33 ;
任何帮助将不胜感激...我一直在尝试不同的查询,使其工作,但过去2天一直没有成功:/请!
答案 0 :(得分:0)
首先提出建议:由于您的user_id在会话中,因此无需将其设为输入字段。在后一种情况下,可以改变形式user_id值以为不同用户创建条目。而是在插入数据的代码中,从$ _SESSION而不是$ _POST
获取user_id(编辑:我现在看到你从$ _SESSION中获取。在处理插入的代码中,不需要根据会话user_id从用户表中进行选择,因为你只使用该表中的user_id )
然后,您可以尝试进行一次查询而不是三次,跳过整个用户表,因为user_id在会话中:
SELECT
hills.*,
CASE hills_ridden.hill_id WHEN NULL THEN 0 ELSE 1 AS hill_ridden
FROM hills
LEFT JOIN hills_ridden
ON hills_ridden.hill_id = hills.hill_id
AND hills_ridden.user_id = '".$_SESSION['user_id']."'
ORDER BY hill_region ASC, hill_id ASC;
您的按钮<td>
可能会变成:
if($row_hill['hill_ridden'] == 1) {
echo "<button class='btn btn-default btn-xs' disabled>You Rode This</button>";
} else {
echo"
<form method='post' action=''>
<input type='hidden' name='hill_id' value='".$row_hills['hill_id']."'>
<input type='hidden' name='points_gained' value='".$row_hills['hill_total_points']."'>
<input type='hidden' name='user_id' value='".$row_user['user_id']."'>
<button type='submit' name='hill_sub' class='btn btn-inverse btn-xs'>I Rode This</button>
</form>";
} echo"</td>
</tr>";
此代码未经测试,只是为了提供一个想法。
答案 1 :(得分:0)
@maarten,谢谢你的想法......我试着/修补它,它给了我一些我需要的东西......但意识到最好的计划是最简单的,所以我得到了这个: / p>
$get_ridden = "SELECT * FROM hills_ridden WHERE hill_id = '".$hill_id."' AND user_id = '".$row_user['user_id']."'";
$run_ridden = mysqli_query($con, $get_ridden);
$row_ridden = mysqli_fetch_array($run_ridden);
$num_ridden = mysqli_num_rows($run_ridden);
if($num_ridden >= 1) {
echo "<button class='btn btn-default btn-xs' disabled>You Rode This</button>";
} else {
这给了我正在寻找的东西...需要稍稍离开它然后用新鲜的眼睛看待它!