2017-06-30 11 views
0

温度データとタイムスタンプをデータベースに保存し、その値を表に表示するためのPHPコードを作成しようとしています。温度データはhttpポストを使用してデータベースに送られます。ここhttp postとPHPを使ってデータベースに値を保存できません

コード:

の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(); 
?> 

私はhttp投稿をしていますhttps://www.hurl.it/

ページレスポンスでは、接続が閉じられていないように、インデックスページのコードを見る代わりに常に(空)と表示されます。

データベースに新しい行を1回だけ書き込むことができました。時には他のいくつかのノー....と動作するようなものです。

header( "Location:index.php");

)が最後の行であり、

出口(続いています。

ですので、すべての手順を実行する必要があります。ここ

私はこのビデオhttps://youtu.be/sJdWuPHKRRY?t=11m1sに続くSQLインジェクションを防止するために、HTTPポストのスクリーンショット

enter image description here

enter image description here

。バインド機能の使い方が分かりません。ここで私は "uid =?" 「?」だけではない。どのように適切な方法で私のSQLクエリで動的な私の$ temp変数を含めるには?

tnx

答えて

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)" 

次に、実際の価値を実際の価値に置き換えるために必要なことは何でも行うことができます。クエリ内にあります。

+0

あなたは私に言ったように既に試みました。動作しません。しかし、この声明がどのように機能するかは私には分かりません。私は=で多くのバージョンを参照してください?または?、いくつかのもの:nameまたは:value。 –

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> 

OUTPUT

enter image description here

+0

cool!あなたはページを作ることで試しましたか、あるいはPHPエディタの一種、デバッガオンラインですか? –

+0

私のローカルマシンで行った.. :) –

+0

super tnx!あなたは素晴らしいです...とにかく私はまだ、なぜまだ良いPHPデバッガがオンラインになっていないのだろうと思います。 –

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"); 

?> 

サーバーにデータを追加するには、このスクリプトをお読みください。それが役に立てば幸い。質問があれば私に知らせてください。

関連する問題