2015-12-13 69 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>