2015-12-13 34 views
0

我有这样的形式:更新单元,其中ID

<?php 
    $id = $_GET['id']; 
?> 
<form method="POST" action="send.php?<?php echo $id; ?>" 
<p><input type="text" class="result" size="80%" required placeholder="number"> 
</p> 
<p> 
<input type="hidden" name="id"> 

<div style="width:200px;"> 
<span class="a-button a-button-primary a-padding-none a-button-span12"> 
    <span class="a-button-inner"> 
    <input id="continue-top" class="a-button-text " tabindex="0" type="submit" value="Continue" > 
    </span> 
</span> 
    </div> 
</p> 
</form> 

我想更新列result其中

$id = $_GET['id']; 

如何更新 '结果' 其中id =为例26在这里?:

mysql_query("UPDATE `cc` SET `result`= 
         ('".$_POST['result']."')",$db) ; 
header('Refresh: 1; url=admin.php'); 
+0

某些元素需要名称'result'。这会打开SQL注入。 'send.php?<?php echo $ id;?>'也没有设置GET参数。你需要'id ='。 – chris85

+0

http://dev.mysql.com/doc/en/update.html –

回答

1
<?php 
$id = $_GET['id']; 
?> 
<form method="POST" action="send.php"> 
    <p><input type="text" class="result" name="result" size="80%" required placeholder="number"></p> 
    <p> 
     <div style="width:200px;"> 
      <span class="a-button a-button-primary a-padding-none a-button-span12"> 
       <span class="a-button-inner"> 
        <input id="continue-top" class="a-button-text " tabindex="0" type="submit" value="Continue" > 
       </span> 
      </span> 
     </div> 
    </p> 
    <input type="hidden" name="id" value="<?php echo $id; ?>" /> 
</form> 

而且在send.php得到这个ID并与$_POST['id']$_POST['result']结果并保护与mysql_real_escape_string

就像是:

$id = mysql_real_escape_string($_POST['id']); 
$result = mysql_real_escape_string($_POST['result']); 

要更新的数据库表cc做到这一点有:

mysql_query("UPDATE `cc` SET `result` = ' " . $result . " ' WHERE `id` = ' " . $id . " '", $db); 
+1

您在表格的开头忘记了关闭“>” – Dacaspex

+0

@ chris85我加了这个,就像一个例子。编辑.. – FilipET

+0

@Dacaspex谢谢.. – FilipET

0
$sql = "UPDATE cc SET result='Doe' WHERE id=26"; 

但这里应该是成才这样的,如果你想$ _GET [ID]到工作:

<form method="POST" action="send.php?id=<?php echo $id;?>" 

做不要忘记清理数据,避免mysql注入:

$id = mysql_real_escape_string($_GET['id']); 
+0

我重拍了代码,但是再次无效。怎么了? '<?php include('connect.php'); $ query = mysql_query(“SELECT * FROM'cc' WHERE'id' ='$ id'”,$ db); $ row = mysql_fetch_assoc($ query);我们可以使用mysql_query(“UPDATE'cc' SET'result' ='”。$ _ POST ['result']。''WHERE'id' ='“。$ _POST ['id']。”'“,$ db); header('Refresh:1; url = admin.php'); ?> ' –

0

把输入类型的值隐藏起来。例如,您要更新哪个ID记录

替换此代码。

<form method="POST" action="send.php?<?php echo $id; ?>" 
<p><input type="text" class="result" size="80%" required placeholder="number"> 
</p> 
<p> 
<input type="hidden" name="id" value='26'> 

<div style="width:200px;"> 
<span class="a-button a-button-primary a-padding-none a-button-span12"> 
<span class="a-button-inner"> 
<input id="continue-top" class="a-button-text " tabindex="0" type="submit" value="Continue" > 
</span> 
</span> 
</div> 
</p> 
</form>