2015-11-03 76 views
-1

如何處理來自POST請求的變量?可以說我有像this這樣的表格而且我想更新票據變量,無論具體的id是什麼。SQL和POST請求

所以對於代碼,我有這個

$vote = $_POST["votes"]; 
$sentid = $_POST["sentid"]; 
一起

像這樣的東西

UPDATE `my_exampleo202s`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = sentid 

而且在發佈請求我與一些發送sentid。 sentid = 3

雖然這不起作用。我無法提供任何錯誤或任何內容,因爲我沒有從瀏覽器中查看它。

任何想法什麼是我應該這樣做的正確方法?

(這是我現在所擁有的,如果需要這種耐心的代碼)

<?php 
    $vote = $_POST["votes"]; 
    $sentid = $_POST["sentid"]; 
    $conn = new mysqli("localhost","exampleo202s","","my_exampleo202s"); 


    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 


    $sql = "UPDATE `my_exampleo202s`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = sentid"; 

    if ($conn->query($sql) === TRUE) { 
     echo "<br>Record updated successfully <br>"; 
    } else { 
     echo "<br>Error updating record: <br>" . $conn->error; 
    } 
    $conn->close(); 
?> 

UPDATE

<?php 
    $vote = $_POST["votes"]; 
    $sentid = $_POST["sentid"]; 
    $conn = new mysqli("localhost","jusavoting10rxx9s3","","my_jusavoting10rxx9s3"); 


    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 


    $sql = "UPDATE `my_jusavoting10rxx9s3`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = $sentid"; 

    if ($conn->query($sql) === TRUE) { 
     echo "<br>Record updated successfully <br>"; 
    } else { 
     echo "<br>Error updating record: <br>" . $conn->error; 
    } 
    $conn->close(); 
?> 
+0

'\'my_exampleo202s \'。\'總統候選人\''看起來不像一個合適的表名。你的數據庫是什麼樣的?無論您是否使用瀏覽器,您仍然可以將'$ conn-> error'記錄到外部文件。 –

+0

你應該使用[mysqli_real_escape_string()](http://php.net/manual/en/mysqli.real-escape-string.php) –

+0

在你的查詢中應該是$ sentid –

回答

0

嘗試呼應你的$ _POST值,如果它不工作:

<?php 
    $vote = $_POST["votes"]; 
    $sentid = $_POST["sentid"]; 
    $conn = new mysqli("localhost","exampleo202s","","my_exampleo202s"); 


    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 


    $sql = "UPDATE `my_exampleo202s`.`President Candidates` SET votes = votes + 1 WHERE `President Candidates`.`id` = $sentid"; 

    if ($conn->query($sql) === TRUE) { 
     echo "<br>Record updated successfully <br>"; 
    } else { 
     echo "<br>Error updating record: <br>" . $conn->error; 
    } 
    $conn->close(); 
?> 
+0

唯一的問題是,我沒有從瀏覽器查看它,所以我無法看到結果 – Sploit

+0

你的意思是?你的數據庫表更新了嗎? –

+0

是啊,它沒有更新。如果您願意,我可以使用我的確切代碼更新原始問題,但不確定它是否有幫助,但 – Sploit