无法使用http post和PHP保存数据库中的值

时间:2017-06-30 10:04:11

标签: php mysql database

我尝试编写用于在数据库中保存温度数据和时间戳的PHP代码,并在表中显示值。温度数据通过http post发送到数据库。

这里是代码:

index.php(显示表格)

<?php

include("connect.php");     

$link=Connection();

$sql="SELECT * FROM `temptrac_temp` ORDER BY `timestamp` DESC";

$result=mysqli_query($link, $sql);

/* determine number of rows result set */
$row_cnt = $result->num_rows;

printf("Result set has %d rows.\n", $row_cnt);
?>

<html lang="en">
<head>

<meta charset="utf-8">

<title>Arduino thermometer control panel</title>

<link rel="stylesheet" href="./css/index.css?v=1.0">

</head>


<body>

<div class="container">

    <p>Temperature Arduino checker</p>

    <p>The temperature is:</p>  

    <table class="normal" border="2" cellspacing="1" cellpadding="1">

    <tr>
        <td>&nbsp;Temperature&nbsp;</td>
        <td>&nbsp;Timestamp&nbsp;</td>
        <td>&nbsp;Status&nbsp;</td>

    </tr>

    <?php 
        if($result!==FALSE){


            while($row = mysqli_fetch_array($result)) {



                if($row["temp"] <= 8){

                    printf("<tr><td>&nbsp;%s</td><td>&nbsp;%s&nbsp;</td><td>&nbsp;Ok!&nbsp;</td></tr>", 
                        $row["temp"], $row["timestamp"]);
                }

                if($row["temp"] > 8){

                    printf("<tr><td>&nbsp;%s</td><td>&nbsp;%s&nbsp;</td><td>&nbsp;Alert!&nbsp;</td></tr>", 
                        $row["temp"], $row["timestamp"]);
                }

            }
            mysqli_free_result($result);
            mysqli_close($link);
        }else{

            printf("there is an error");

        }
    ?>

    </table>

</div>

</body>
</html>

connect.php(连接到mysql服务器)

<?php

function Connection(){


    /* php site version doesn't like so much "", prefer '' */
    $mysqli = new mysqli('localhost', 'temptrac_temp', 'temptrac_temp2846', 'temptrac_temp');

    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

    /* fuck maaan! check the function name return*/
    return $mysqli;

}
?>

add.php(在数据库中写一个新行)

<?php
include("connect.php");

$link = Connection();

$temp = mysqli_real_escape_string($_POST["temp"]);
//$lat =$_POST["lat"];
//$lng =$_POST["lng"];

$sql = "INSERT INTO temptrac_temp (ID, temp, timestamp) VALUES (NULL, temp=?, CURRENT_TIMESTAMP)";

$stmt = $link->prepare($sql);

$stmt->bind_param("d", $temp);


$stmt->execute();



//mysqli_query($link, $query);


$stmt->close();
$link->close();

header("Location: index.php");
exit();
?>

我正在使用https://www.hurl.it/

进行http发布

在页面响应中,我看到总是(空)而不是看到索引页面的代码,就像连接没有关闭一样。

我只能在数据库中写一个新行,所以就像有时候工作一些其他没有....这很奇怪。

  

标题(&#34;位置:index.php&#34;);

是最后一行,后跟

  

出口();

所以所有程序都应该有效。

这里是http帖子的截图

enter image description here

enter image description here

为防止SQL注入,我遵循了此视频https://youtu.be/sJdWuPHKRRY?t=11m1s。我不清楚如何使用bind函数。在这里,我看到&#34; uid =?&#34;而且不仅仅是&#34;?&#34;。如何以正确的方式包含我的sql查询中动态的$ temp变量?

这太多了!

3 个答案:

答案 0 :(得分:0)

您的插入查询错误。看起来您可能混淆了插入和更新的语法。对于插入,你永远不会做像value =?

这样的事情
$sql = "INSERT INTO temptrac_temp (ID, temp, timestamp) VALUES (NULL, temp=?, CURRENT_TIMESTAMP)"

您可以改为编写这样的查询。

$sql = "INSERT INTO temptrac_temp (ID, temp, timestamp) VALUES (NULL, ?, CURRENT_TIMESTAMP)"

然后,您可以做任何您需要做的事情,将真正的价值分配给?在查询中。

答案 1 :(得分:0)

  

Connection.php PAGE

<?php

function Connection(){

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo";

// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

return $conn;

}
?>
  

add.php PAGE

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo";

// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";


$sql = "INSERT INTO temptrac_temp (ID, temp, timestamp) VALUES (NULL,'?',CURRENT_TIMESTAMP)";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}



header("Location: index.php");
$conn->close();

?>
  

index.php PAGE

<?php

include("connection.php");     

$link=Connection();

$sql="SELECT * FROM `temptrac_temp` ORDER BY `timestamp` DESC";

$result=mysqli_query($link, $sql);

/* determine number of rows result set */
$row_cnt = $result->num_rows;

printf("Result set has %d rows.\n", $row_cnt);
?>

<html lang="en">
<head>

<meta charset="utf-8">

<title>Arduino thermometer control panel</title>

<link rel="stylesheet" href="./css/index.css?v=1.0">

</head>


<body>

<div class="container">

    <p>Temperature Arduino checker</p>

    <p>The temperature is:</p>  

    <table class="normal" border="2" cellspacing="1" cellpadding="1">

    <tr>
        <td>&nbsp;Temperature&nbsp;</td>
        <td>&nbsp;Timestamp&nbsp;</td>
        <td>&nbsp;Status&nbsp;</td>

    </tr>

    <?php 
        if($result!==FALSE){


            while($row = mysqli_fetch_array($result)) {



                if($row["temp"] <= 8){

                    printf("<tr><td>&nbsp;%s</td><td>&nbsp;%s&nbsp;</td><td>&nbsp;Ok!&nbsp;</td></tr>", 
                        $row["temp"], $row["timestamp"]);
                }

                if($row["temp"] > 8){

                    printf("<tr><td>&nbsp;%s</td><td>&nbsp;%s&nbsp;</td><td>&nbsp;Alert!&nbsp;</td></tr>", 
                        $row["temp"], $row["timestamp"]);
                }

            }
            mysqli_free_result($result);
            mysqli_close($link);
        }else{

            printf("there is an error");

        }
    ?>

    </table>

</div>

</body>
</html>
  

<强>输出

enter image description here

答案 2 :(得分:0)

<?php
include("dbconnect.php");
$SQL= "INSERT INTO tabl1(reading,cost)VALUES(".$_GET["reading"].",".$_GET["cost"].")";
mysqli_query($dbh,$SQL);
header("Location: review_data.php");

?>

阅读此脚本以将数据添加到服务器中。希望能帮助到你。如果有任何疑问,请告诉我。