1

这是我的原始代码

    <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 字段唯一。我已经拥有唯一的 issueid。我怎样才能做到这一点?请通过提供代码帮助我,我是 PHP 新手,只知道基础知识。我还不知道 PDO,我是视觉学习者,谢谢!

4

2 回答 2

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 ...
    }
}
于 2013-08-10T21:01:31.520 回答
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 ++;     
}            
于 2013-08-10T20:16:00.240 回答