Award Issue ID Sort # Submit user-6ren">
gpt4 book ai didi

php - 如何使php post变量唯一

转载 作者:行者123 更新时间:2023-11-29 00:27:45 25 4
gpt4 key购买 nike

这是我的原始代码

    <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,我是视觉学习者谢谢你!

最佳答案

最好将项目分组到数组中。

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 ...
}
}

关于php - 如何使php post变量唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18165864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com