0

I'm trying to write a PHP code used to save temperature data and timestamp in a database and show the value in a table. The temperature data is sent to database by using an http post.

here the code:

index.php (display the table)

<?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 (connection to mysql server)

<?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 (write a new row in the database)

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

I'm doing http post with https://www.hurl.it/

In the page response I see always (empty) instead to see the code of the index page like the connection is not closed.

I was able to write a new row in the database only one time, so is like sometimes work some other no.... that's weird.

header("Location: index.php");

is the last line and is followed by

exit();

so all the procedure should works.

here the screenshot of the http post

enter image description here

enter image description here

To prevent SQL Injection I followed this video https://youtu.be/sJdWuPHKRRY?t=11m1s. Is not clear to me how to use the bind function. here I see "uid=?" and not only "?". How to include my $temp variable that is dynamic in my sql query in a proper way?

tnx so much!

3 Answers 3

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

?>

Read this script to add data into server. Hope it helps. Please let me known if any query.

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

8
  • cool! did you try by making a page or there is a kind of PHP editor, debugger online? Commented Jun 30, 2017 at 11:23
  • bro i did on my local machine .. :) Commented Jun 30, 2017 at 11:24
  • super tnx! you are great.... anyway I still wonder why there is not yet a good PHP debugger online. Commented Jun 30, 2017 at 12:29
  • there normal many site . just google it. like : http://phpio.net/ , http://phpfiddle.org/ , http://www.writephponline.com/ , https://www.codechef.com/ide , http://phptester.net/ , http://sandbox.onlinephpfunctions.com/ , http://codepad.org/ .. etc Commented Jun 30, 2017 at 12:37
  • @AndreaMancini .. if Get the Ans Then Please .. Tick right to the Question and up the vote :) happy to help any time !! Commented Jun 30, 2017 at 12:39
0

Your insert query is wrong. It looks like you might have mixed up the syntax of insert and update. With inserts you will never do something like value = ?

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

You would instead write the query like this.

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

You can then do whatever you need to do to sub a real value in to the place of the ? in the query.

1
  • already tried like you told me. doesn't works. But is not clear for me how does this statement work. I see many version with =? or only ?, some with :name or :value. Commented Jun 30, 2017 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.