dongpingwu8378 2013-08-10 20:04
浏览 29
已采纳

如何让php post变量独一无二

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!

  • 写回答

2条回答 默认 最新

  • dshdb64088 2013-08-10 21:01
    关注

    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 ...
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值