2015-11-03 147 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