Here is my original code
<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>
I need to make the userdisplayorder field unique as well. I already have the issueid unique. How can I do this? Please help me by providing code as well, I am new to PHP and only know basics. I do not know PDO yet and I am a visual learner Thank you!
You'd do well to group your items into arrays.
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;
}
At that point, because of how PHP processes forms, $_POST['items']
will be an array, and each entry of it will itself be an array containing each item field. It'd be almost like you said $_POST['items'] = array(0 => array('issue_id' => '3', 'userdisplayorder' => '0'), 1 => array('issue_id'
... . And submitted
will be like array($index_of_row => 'Submit')
. You can use it to determine which item had its submit button clicked, like so:
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
}
Or if you just want to process all the items each time:
if (!empty($_POST['items'])) {
foreach ($_POST['items'] as $row) {
$srow = array_map('mysql_real_escape_string', $row);
... process $srow ...
}
}
Change this:
$sql = mysql_query("UPDATE award_user
SET userdisplayorder='$userdisplayorder'
WHERE issue_id='$_POST[hiddenID]'");
to this:
$sql = mysql_query("UPDATE award_user SET
userdisplayorder='$userdisplayorder'
WHERE issue_id='$_POST['issueid']'");
Plus you should use MySQLi/PDO in place of mysql_ functions. Also, I don't think you need JavaScript functions for submitting to the form. You can do that directly. So you can change this:
echo '<th>'; echo "<center><input type='submit'
name='submit' value='Submit' onclick=awardSort('$ID')></center>"; echo '</th>';
to this:
echo '<th>';
echo "<center><input type='submit' name='submit' value='Submit' </center>";
echo '</th>';
UPDATE. This is when the OP wants to make the userdisplayorder field unique. One way to do that is making changes at the form level like so:
$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 ++;
}