I have set up a form, created from a while loop of around 20 entries with each mysqli entry it echos back to include a switch. I'm trying to get the table to recognize if the switch is on or not, if it is set to on input true into the table row and if the switch is not set input false into the table row, updating all rows with one query and php execution file.
The code below consists of first the PHP AND HTML FORM code to create all the entries with a switch wrapped in a form. The second two examples of code are the php execution attempts.
PHP AND HTML FORM CODE STARTS
<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="add_newsletter.php">
<?php
// get the info from the db
$query = 'SELECT * FROM news_content ORDER BY article_id DESC';
$results = mysqli_query($dbc, $query)or die('Error querying database');
while ($row = mysqli_fetch_array($results)){
// tileAdminBar START
echo '<div id="tileAdminBar">';
echo '<div id="tileadminBarCreateNewsletterSlider">';
echo '<label class="switch">
<input type="checkbox" name="newsletterToggle" id="newsletterToggle" value="true" >
<span class="slider round"></span>
</label>';
echo '</div>';
echo '</div>';
// tileAdminBar END
echo '<div id="fieldWrapper">';
echo '<input type="hidden" value="' . $_GET['article_id'] . '"name="article_id"/> ';
echo '</div>';
} // end while
?>
<div id="fieldWrapper">
<input class="form" type="submit" name="Submit" id="Submit" value="Create newsletter"/>
</div>
</form>
PHP EXECUTION EXAMPLE ONE START
<?php
$article_id = $_POST['article_id'];
$newsletterToggle = $_POST['newsletterToggle'];
$newsletterToggle = mysqli_real_escape_string($dbc, $newsletterToggle);
$query = 'SELECT * FROM ontent ORDER BY article_id DESC';
$results = mysqli_query($dbc, $query)or die('Error querying database');
while ($row = mysqli_fetch_array($results)){
if($newsletterToggle == "true"){
//post variables to slt_database and insert into content table
$secondQuery = "UPDATE news_content SET newsletter = 'true' WHERE '$row' = '$article_id'";
$results2 = mysqli_query($dbc, $secondQuery)or die('Error querying database');
}else{
//post variables to slt_database and insert into content table
$secondQuery = "UPDATE news_content SET newsletter = 'false' WHERE '$row' = '$article_id'";
$results2 = mysqli_query($dbc, $secondQuery)or die('Error querying database');
}
}
//End of while
//close slt_database conection
mysqli_close($dbc);
?>
PHP EXECUTION EXAMPLE TWO
<?php
$newsletterToggle = $_POST['newsletterToggle'];
$newsletterToggle = mysqli_real_escape_string($dbc, $newsletterToggle);
$query = 'SELECT * FROM content ORDER BY article_id DESC';
$results = mysqli_query($dbc, $query)or die('Error querying database');
while ($row = mysqli_fetch_array($results)){
if($newsletterToggle == "true"){
//post variables to slt_database and insert into news_content table
$secondQuery = "UPDATE news_content SET newsletter = 'true'";
$results2 = mysqli_query($dbc, $secondQuery)or die('Error querying database');
}else{
//post variables to slt_database and insert into content table
$secondQuery = "UPDATE content SET newsletter = 'false'";
$results2 = mysqli_query($dbc, $secondQuery)or die('Error querying database');
}
}
//End of while
//close slt_database conection
mysqli_close($dbc);
?>
/* PHP EXECUTION EXAMPLE TWO END*/
Using PHP EXECUTION EXAMPLE ONE I try grabbing the article_id to help updating, but when executed I don't get any mysqli or php errors, it just doesn't effect any of the rows for the table column.
Using PHP EXECUTION EXAMPLE TWO without the article_id i can switch 1,2,3 on etc out of the 20 rows and it just updates all the table rows to true. If I don't switch any of the switches on all the table rows update to false. My problem is it is not recognizing the switches that have been switch on or off and updating them accordingly