这是我的原始代码
<center>
<html>
<body>
<form method="post" name="AwardList" action="<?php echo $_SERVER['PHP_SELF']?>">
<table border=1>
<tr>
<th><b>Award</b></th>
<th><b>Issue ID</b></th>
<th><b>Sort #</b></th>
<th><b>Submit</b></th>
</tr>
<?php
$userid = ($vbulletin->userinfo['userid']);
$query = ("select a.*, userdisplayorder, issue_id from award a join award_user aw on a.award_id = aw.award_id where aw.userid = '$userid'");
if (isset($_POST['submit'])){
$userdisplayorder = mysql_real_escape_string($_POST['userdisplayorder']);
echo $_POST[issueid];
echo '<br>';
echo $userdisplayorder;
$sql = mysql_query("UPDATE award_user SET userdisplayorder='$userdisplayorder' WHERE issue_id='$_POST[issueid]'");
}
$result = mysql_query($query) or die(mysql_error());
$count = 1;
while ($row = mysql_fetch_array($result)){
$awardimgurl = $row['award_img_url'];
$ID = $row['issue_id'];
// don't get it from the database since its not unique in the database itself
// $userdisplayorder = $row['userdisplayorder'];
echo '<tr>';
echo '<th>';
echo "<img src='$awardimgurl'>"; echo '</th>';
echo '<th>';
echo '<input type="text" name="issueid" readonly="readonly" value="' . $ID . '" size="5">';
echo '</th>';
echo '<th>';
echo '<input type="text" name="userdisplayorder" value="' . $count . '" size="5">';
echo '</th>';
echo '<th>';
echo "<center><input type='submit' name='submit' value='Submit'></center>";
echo '</th>';
echo '</tr>';
$count ++;
}
?>
</table>
</html></center>
我还需要使userdisplayorder字段也是唯一的。我已经有了独特的问题。我怎样才能做到这一点?请通过提供代码帮助我,我是PHP新手,只懂基础知识。我还不知道PDO,我是一个视觉学习者 谢谢!
答案 0 :(得分:1)
您最好将项目分组到数组中。
for ($i = 0; $row = mysql_fetch_array($result); ++$i) {
// I took the liberty of adding HTML-encoding to the output.
// This prevents issues if a field contains quotes, etc.
$hrow = array_map('htmlentities', $row);
extract($hrow, EXTR_PREFIX_ALL, 'html');
$item_name = "items[$i]";
echo <<<ENDHTML
<tr>
<th><img src="{$html_award_img_url}"></th>
<th><input type="text" name="{$item_name}[issue_id]" readonly size="5"
value="{$html_issue_id}"></th>
<th><input type="text" name="{$item_name}[userdisplayorder]" size="5"
value="{$html_userdisplayorder}"></th>
<th><center><input type="submit" name="submitted[{$i}]" value="Submit"></center></th>
</tr>
ENDHTML;
}
此时,由于PHP处理表单的形式,$_POST['items']
将是一个数组,它的每个条目本身就是一个包含每个项目字段的数组。这几乎就像你说$_POST['items'] = array(0 => array('issue_id' => '3', 'userdisplayorder' => '0'), 1 => array('issue_id'
...... submitted
就像array($index_of_row => 'Submit')
一样。您可以使用它来确定哪个项目单击了其提交按钮,如下所示:
if (!empty($_POST['submitted'])) {
foreach ($_POST['submitted'] as $key => $unused) {
$row = $_POST['items'][$key];
# if you don't want to manually `mysql_real_escape_string` everything...
$srow = array_map('mysql_real_escape_string', $row);
... process $srow ... which now has its own unique userdisplayorder field
}
或者,如果您只想每次都处理所有项目:
if (!empty($_POST['items'])) {
foreach ($_POST['items'] as $row) {
$srow = array_map('mysql_real_escape_string', $row);
... process $srow ...
}
}
答案 1 :(得分:0)
改变这个:
$sql = mysql_query("UPDATE award_user
SET userdisplayorder='$userdisplayorder'
WHERE issue_id='$_POST[hiddenID]'");
到此:
$sql = mysql_query("UPDATE award_user SET
userdisplayorder='$userdisplayorder'
WHERE issue_id='$_POST['issueid']'");
另外,您应该使用MySQLi / PDO代替mysql_函数。 此外,我认为您不需要JavaScript函数来提交表单。你可以直接这样做。所以你可以改变这个:
echo '<th>'; echo "<center><input type='submit'
name='submit' value='Submit' onclick=awardSort('$ID')></center>"; echo '</th>';
到此:
echo '<th>';
echo "<center><input type='submit' name='submit' value='Submit' </center>";
echo '</th>';
更新。这是OP想要使userdisplayorder字段唯一的时候。一种方法是在表单级别进行更改,如下所示:
$count = 1;
while ($row = mysql_fetch_array($result)){
$awardimgurl = $row['award_img_url'];
$ID = $row['issue_id'];
// don't get it from the database since its not unique in the database itself
// $userdisplayorder = $row['userdisplayorder'];
echo '<tr>';
echo '<th>';
echo "<img src='$awardimgurl'>"; echo '</th>';
echo '<th>';
echo '<input type="text" name="issueid" readonly="readonly" value="' . $ID . '" size="5">';
echo '</th>';
echo '<th>';
echo '<input type="text" name="userdisplayorder" value="' . $count . '" size="5">';
echo '</th>';
echo '<th>';
echo "<center><input type='submit' name='submit' value='Submit'></center>";
echo '</th>';
echo '</tr>';
$count ++;
}