我试图从我的数据库中显示2行,但我希望它是当我点击第1行radiobuttons时第1行更新..这不会发生...当我点击第1行第二行更新。请自己检查一下吧---> http://albsocial.us/seria.php
<?php
include("connect.php");
$query = "SELECT * FROM test ORDER BY `id` DESC LIMIT 2";
$result = mysql_query($query);
echo "<h2>Seria A</h2><hr/>";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
$win = $row['win'];
$draw = $row['draw'];
$lose = $row['lose'];
echo $home, " - ", $away,"<br/>";
echo "<form action='' method='post'>
<input type='hidden' name='id' value='".$row['id']."'>
<input type='radio' name='select' value='1'>1
<input type='radio' name='select' value='X'>X
<input type='radio' name='select' value='2'>2
<input type='submit' name='submit' value='Submit'/>
</form>
";
echo $home, " -> ", $win;
echo "<br/>Barazim -> ", $draw,"<br/>";
echo $away, " -> ", $lose,"<hr/>";
}
$id = isset($_POST['id']) && is_numeric($_POST['id']) ? $_POST['id']:false;
if (isset($_POST) && $_POST['select'] == 1){
$select = $_POST['select'];
$select = $win + $select;
mysql_query("UPDATE test SET win='$select' WHERE id='$id'");
header('Location: ../seria.php');
}else if (isset($_POST) && $_POST['select'] == 'X'){
$select = $_POST['select'];
$select = '1';
$select = $draw + $select;
mysql_query("UPDATE test SET draw='$select' WHERE id='$id'");
header('Location: ../seria.php');
}else if (isset($_POST) && $_POST['select'] == 2){
$select = $_POST['select'];
$select = '1';
$select = $lose + $select;
mysql_query("UPDATE test SET lose='$select' WHERE id='$id'");
header('Location: ../seria.php');
}
?>
答案 0 :(得分:0)
您的while循环将始终将$id
设置为数据集中最后一行的ID。
您需要某种方式为表单中的每一行提交ID。然后,在检索POST
变量时获取该值。
如果你正确地构建了东西,你就不需要进行标题重定向。
另外,我建议将isset($_POST)
测试移到它自己的if语句中,这样如果没有发布任何代码就不会执行任何代码。
以下是我将如何重做:
<?php
include("connect.php");
// if data is submitted, update database
if (!empty($_POST)) {
$id = isset($_POST['id']) && is_numeric($_POST['id']) ? $_POST['id'] : false;
$select = isset($_POST['select'])&&in_array($_POST['select'],array('win','lose','draw')) ? $_POST['select'] : false;
if ($id && $select) {
$sql="UPDATE `test` SET `$select`=`$select`+1 WHERE `id`='$id';";
mysql_query($sql) or die(mysql_error());
}
}
// get data from database
$query = "SELECT * FROM test ORDER BY `id` DESC LIMIT 2";
$result = mysql_query($query) or die(mysql_error());
// output
?><h2>Seria A</h2><hr/><?php
while($row = mysql_fetch_assoc($result)){
?><p><?=$row['home']?> - <?=$row['away']?></p>
<form action="" method="post">
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="radio" name="select" value="win">1
<input type="radio" name="select" value="draw">X
<input type="radio" name="select" value="lose">2
<input type="submit" name="submit" value="Submit">
</form>
<p><?=$row['home']?> -> <?=$row['win']?></p>
<p>Barazim -> <?=$row['draw']?></p>
<p><?=$row['away']?> -> <?=$row['lose']?></p><?php
}
?>
顺便说一下,你如何处理“主场”与“客场”比赛?
答案 1 :(得分:0)
问题在于您的按钮名称。您应该给每个按钮名称,彼此不同的名称,以便服务器知道正在提交哪个表单。您可以尝试根据ID给出您的按钮名称:
<input type='submit' name='submit<?php echo $id; ?>' value='Submit'/>
然后您可以执行条件语句以查看单击哪个按钮:
if ( isset( $_POST['submit$id'] ) ) { }