Jump to content

LAN Web page worked PHP 5, not happy with PHP 7.


Geoff_2
Go to solution Solved by Barand,

Recommended Posts

Hi All.

I'm a web page noob that cobbled some HTML and PHP together to create a web interface for some Raspberry Pi s used for monitoring and control around the house.

The pages have worked fine a few years but I have encountered an issue since upgrading one of the RPi s to the latest software. In this instance one Pi (named RPi2) has a LAMP server and one page on that server interacts with a database on another Pi (named RPi3) that has a relay drive for hot water heating power. The web page basically allows the user to check/uncheck power to hot water and writes a new line to the database when ever there is a change. A python script reacts to change in database value to switch a relay. When I updated the software on RPi3 with Buster/PHP 7 the page on RPi2 addressing the database on RPi3 stop functioning.

 

In my pursuit of a resolution I tried directing the webpage to a db table on the localhost (RPi2) which still has PHP 5 and it worked as expected. I created a clean LAMP install for a test with PHP 7 and tested the page with a local db table and it does not function correctly. I'm thinking there is an issue with variable type assignment between PHP and the database but I really don't know. I inserted a

Var_Dump()

line as a debugging measure and get a result of 

string(1) "1"

but the usual results of Var_Dump() I see on PHP help sites do not have a character in parenthesis, maybe unrelated to my problem.

 

The page is simple checkbox with update button that should show last/current state value "onoff", stored as 0 or 1 in db table, anytime the update button it pressed then a new line is written to db table with value of "onoff" reflecting checkbox checked or unchecked.

The problem is database is not reliably updated and regardless of value in database the page always refreshes to have the checkbox checked, even if the checkbox is unchecked and update is hit the checkbox becomes checked.

 

The two relevant pages of code are below

This is hws.php

<!DOCTYPE html>
<html>
<title>HWS Control</title>
<link rel="icon" href="home-button.png">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="w3.css">
<body>

<nav class="w3-sidenav w3-collapse w3-white w3-card-2 w3-animate-left" style="width:200px;" id="nav01">
  <a href="javascript:void(0)" onclick="w3_close()" 
  class="w3-closenav w3-large w3-hide-large">Close &times;</a>
  <a class="w3-xlarge" href="index.php">Home</a>
  <a class="w3-xlarge" href="active.php">Zone Control</a>
  <a class="w3-xlarge" href="actions.php">Monitor Actions</a>
  <a class="w3-xlarge" href="table_mon.php">Monitor Log</a>
  <a class="w3-xlarge" href="table_temp.php">Hotwater</a>

</nav>

<div class="w3-main" style="margin-left:200px">
<header class="w3-container w3-teal">
  <span class="w3-opennav w3-xlarge w3-hide-large" onclick="w3_open()">&#9776;</span>
  <h2>HWS Power</h2>
</header>

<div class="w3-container">
<?php
$servername = "192.168.0.34";
$username = "xxxxxxxx";
$password = "xxxxxxx";
$dbname = "Temp";


$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$thesql = "SELECT onoff FROM state ORDER BY id DESC LIMIT 1";
$result = mysqli_query($conn, $thesql) or die(mysqli_error($conn));
$row_result = mysqli_fetch_assoc($result);

$onoff = $row_result['onoff'];


mysqli_close($conn); 

?>

<form action="hwsupdate.php" method="post" class="w3-container w3-card-4">
<p>
<input type="checkbox" class="w3-check" checked="checked"  id="onoff" name= "onoff1" value="1">
<label class="w3-validate"> HWS Power</label>
</p>


<script>
   document.getElementById("onoff").checked = <?php echo $onoff?>;
</script>

<input type="submit" value="Update" class="w3-btn w3-round-xxlarge" >
<h3></h3>
</form>

</div>

<footer class="w3-container w3-teal"id="foot01" >

</footer>
     
</div><script>
function w3_open() {
    document.getElementById("nav01").style.display = "block";
}
function w3_close() {
    document.getElementById("nav01").style.display = "none";
}
</script>
<script src="ge.js"></script>     
</body>

</html>

 

 

 

and this is hwsupdate.php

<?php
$servername = "192.168.0.34";
$username = "xxxxxx";
$password = "xxxxxxxxx";
$dbname = "Temp";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


$onoff = $_POST['onoff1'];


$sql="INSERT INTO state (onoff) 
VALUES ('$onoff')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


mysqli_close($conn);

header("Location: hws.php");
?>

 

Any guidance to resolution would be greatly appreciated.

Link to comment
Share on other sites

On 3/11/2021 at 6:45 AM, Barand said:

This reply to an earlier post may be relevant to your problem

 

 

Thanks for the prompt reply.

Interesting and appears to resolve the issue.

$onoff = $_POST['onoff1']

works as expected with PHP 5 but not with PHP 7.3 on localhost and not across 2 RPi's running different versions.

(Worked across 2 RPi's with both PHP 5.)

$onoff = $_POST['onoff1'] ?? 0;

works as expected with PHP 7.3 but not with PHP 5 on localhost and not across 2 RPi's running different versions.

Still to test across across 2 RPi's on PHP 7.3 but expecting success.

Link to comment
Share on other sites

The "null coalesce" operator was introduced in v7.0

+------------------------------------------------------+---------+---------+
| Code                                                 |  v 5.x  | v 7.0+  |
+------------------------------------------------------+---------+---------+
| $onoff = isset($_POST['onoff']) ? 1 : 0              |    Y    |    Y    |
+------------------------------------------------------+---------+---------+
| $onoff = $_POST['onoff1'] ?? 0;                      |    -    |    Y    |
+------------------------------------------------------+---------+---------+

 

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.