0

I'm relativity new to php and just testing out some code. The odd thing is that the code both does/doesn't work. The code should check a MySQLi database to determine the state of the check box and then apply that state to the checkbox. What the code currently does is designate the checkbox state based solely off the value of the if condition, regardless of the MySQLi database values.

Here is the code for the html page, it's the if statement near the bottom that's causing issues;

<?php
	include_once 'includes/dbh.inc.php';
?>

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php

	$sql_1 = "SELECT * FROM test2;";
	$results = mysqli_query($conn, $sql_1) or die('Error getting data.');
	
	echo(string) "<table>";
	echo "<tr><th>state</th><th>id</th></tr>";
	
	while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
		echo "<tr><td>";
		echo $row['state'];
		echo "</td><td>";
		echo $row['id'];
		echo "</td></tr>";
		}
		
	echo "</table>";
?>

<form action="includes/checkbox.inc.php" method="post">
	<input type="hidden" name="checkbox1" value="0">
	<input type="checkbox" name="checkbox1" value="1"
	
<?php
	$sql_2 = mysqli_query($conn, "SELECT state FROM test2 WHERE id = '0'") or die('Error getting data.');
	if ($sql_2 == "0") {
      echo "checked";
   } else {
      echo " ";
   }
   mysqli_close($conn);	
?>	
	> Item 1<br>
	
	
	<input type="hidden" name="checkbox2" value="0">
	<input type="checkbox" name="checkbox2" value="1" checked> Item 2<br>
	<input type="submit" name="Submit" value="Submit">
</form>
<br>
<a href="http://localhost/testsite2.com/includes/update.inc.php">Reset</a><br>


</body>
</html>

The odd thing about this code is that the if ($sql_2 == "0") results in the checkbox remaining unchecked, but changing the 0 to a 1, if ($sql_2 == "1") results in the checkbox remaining checked. Both results are regardless of what the database shows.

I know all the other bits of code work, because when I check the checkbox and submit, it updates the database and displays it correctly (the reverse is also true).

If anyone knows why if ($sql_2 == "0") is not working, please let me know. I've even checked other stack overflow postings, and as far as I can tell, everything should be coded properly.

Edit:

I should have stated that in the above question, changing the = to == or reversing the order doesn't fix the problem. The if statement still only returns the else statement.

I've done additional research and think that the issue is related to the use of mysqli_query to retrieve the data, as it should likely be mysqli_fetch_row.

1
  • 1
    if ($sql_2 = "0") is making an assignment, not testing equality. Use if ($sql_2 == "0"). Commented Feb 28, 2018 at 1:44

2 Answers 2

2

if ($sql_2 = "0") will make the value of $sql2 to '0' and this condition will be always true

change it to

if ($sql_2 == "0")

to prevent the accidental assignment you can do like below

if ("0"==$sql_2)  
Sign up to request clarification or add additional context in comments.

1 Comment

I should have stated that in the above question, changing the = to == or reversing the order doesn't fix the problem. The if statement still only returns the else statement. (Thanks for explaining the difference between = and ==.)
0

ISSUE FIXED

The solution was to add a mysqli_data_seek() to the php, below is the working code.

<?php
include_once 'includes/dbh.inc.php';
?>

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php

$sql_1 = "SELECT * FROM test2;";
$result_1 = mysqli_query($conn, $sql_1) or die('Error getting data.');

echo(string) "<table>";
echo "<tr><th>state</th><th>id</th></tr>";
while($row = mysqli_fetch_array($result_1, MYSQLI_ASSOC)) {
	echo "<tr><td>";
	echo $row['state'];
	echo "</td><td>";
	echo $row['id'];
	echo "</td></tr>";
	}	
echo "</table>";

$query_1 = "SELECT state, id FROM test2 ORDER BY id";
$sql_3 = mysqli_query($conn, $query_1) or die('Error getting data.');
if ($result_2 = $sql_3) {
	mysqli_data_seek($result_2, 0);
	$row_1 = mysqli_fetch_row($result_2);
}
if ($result_3 = $sql_3) {
	mysqli_data_seek($result_3, 1);
	$row_2 = mysqli_fetch_row($result_3);
}
?>

<form action="includes/checkbox.inc.php" method="post">
<input type="hidden" name="checkbox1" value="0">
<input type="checkbox" name="checkbox1" value="1"
<?php
if ($row_1[0]=="1") {
  echo "checked";
} else {
  echo " ";
}	
?>
> Item 1<br>


<input type="hidden" name="checkbox2" value="0">
<input type="checkbox" name="checkbox2" value="1"
<?php
if ($row_2[0]=="1") {
  echo "checked";
} else {
  echo " ";
}	
?>
> Item 2<br>
<input type="submit" name="Submit" value="Submit">
</form>
<br>
<a href="http://localhost/testsite2.com/includes/update.inc.php">Reset</a>        
<br>
<?php
mysqli_close($conn);	
?>
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.