While Loop inside While Loop - php

I have a problem with this code in that the second while loop only runs the first time through the code, and then also it makes it so that the $row['id'] does not get a value. Can you help me figure out where I went wrong with this?
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
$result2 = mysqli_query($con,"SELECT * FROM sections ORDER BY `order`");
while($row = mysqli_fetch_array($result))
{
echo '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
echo '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
echo '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
echo '<select name="section">';
$section = $row['section'];
while($row = mysqli_fetch_array($result2)) {
$sectionname = $row['sectionname'];
if ($sectionname == $section) {
echo '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
} else {
echo '<option value="' . $sectionname . '">' . $sectionname . '</option>';
}
}
echo '</select>';
echo '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
echo '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
echo '<input type="submit" name="submission" value="Update">';
echo '<input type="submit" name="submission" value="Delete">';
echo '</form>';
}

Another issue I see with your code is that you will only be able fetch the result set of the query once. So instead get the values beforehand, and store them in a variable like so.
$result2 = mysqli_query($con, "SELECT * FROM sections ORDER BY `order`");
$sectionnames = array();
while($row = mysqli_fetch_array($result2)) {
$sectionnames[] = $row['sectionname'];
}
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
while($row = mysqli_fetch_array($result))
{
echo '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
echo '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
echo '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
echo '<select name="section">';
$section = $row['section'];
foreach ($sectionnames as $sectionname) {
if ($sectionname == $section) {
echo '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
} else {
echo '<option value="' . $sectionname . '">' . $sectionname . '</option>';
}
}
echo '</select>';
echo '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
echo '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
echo '<input type="submit" name="submission" value="Update">';
echo '<input type="submit" name="submission" value="Delete">';
echo '</form>';
}

It's because you are redeclaring $row, try:
$result = mysqli_query($con,"SELECT * FROM faq ORDER BY `order`");
$result2 = mysqli_query($con,"SELECT * FROM sections ORDER BY `order`");
while($row = mysqli_fetch_array($result))
{
echo '<form action="../../includes/faqupdate.php" method="post" style="margin:40px;">';
echo '<input type="text" name="order" style="width:20px;text-align:center;" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['order'] . '\':this.value;" value="' . $row['order'] . '">';
echo '<input type="text" name="heading" onclick="this.value=\'\';" onfocus="this.select()" onblur="this.value=!this.value?\'' . $row['heading'] . '\':this.value;" value="' . $row['heading'] . '">';
echo '<select name="section">';
$section = $row['section'];
while($row2 = mysqli_fetch_array($result2)) {
$sectionname = $row2['sectionname'];
if ($sectionname == $section) {
echo '<option value="' . $sectionname . '" selected="selected">' . $sectionname . '</option>';
} else {
echo '<option value="' . $sectionname . '">' . $sectionname . '</option>';
}
}
echo '</select>';
echo '<input type="text" name="id" style="width:20px;background-color:#CCC;text-align:center;" value="' . $row['id'] . '" readonly>';
echo '<textarea name="content" cols="98" rows="10">' . $row['content'] . '</textarea>';
echo '<input type="submit" name="submission" value="Update">';
echo '<input type="submit" name="submission" value="Delete">';
echo '</form>';
}

Rename your $row in your second while condition for $row2

Related

Can't show another checkbox by clicking a checkbox dynamically & individually

I have a first checkbox with a list of several schemas.
Want I want to do is simple:
When I check a schema, I want to make appear his individual div "list-right" right below, which is ANOTHER checkbox.
Here is my code :
<div>
<div>
<h4>Select your schema(s) :</h4>
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
</div>
<h4>Privileges on tables by selected schemas :</h4>
<div id="div-privileges">
<?php
foreach ($schemas as $elt) {
echo '<div class="list-right" id="' . $elt->getSchema() . '">';
echo '<label for="list-right">' . $elt->getSchema() . ' :</label><br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="REVOKE"/> REVOKE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="ALL"/> ALL PRIVILEGES ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="SELECT"/> SELECT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="INSERT"/> INSERT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="UPDATE"/> UPDATE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="DELETE"/> DELETE ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="CREATE"/>CREATE ? <br />';
echo '</div>';
}
?>
</div>
</div>
I managed to display = 'none' ALL THE div "list-right".
As you can see :
var listRight=document.getElementsByClassName("list-right");
for (var i = 0; i < listRight.length; i ++) {
listRight[i].style.display = 'none';
}
What I have without the JavaScript function :
https://image.noelshack.com/fichiers/2019/38/3/1568790896-capture2.png
What I have with the JavaScript function :
https://image.noelshack.com/fichiers/2019/38/3/1568790893-capture.png
What I want :
https://image.noelshack.com/fichiers/2019/38/3/1568790898-capture3.png
But I can't make them appear individually, & dynamically...
Can anybody help me..?
If you want to this this dynamically you will have to use a client-side script, like javascript. Add onclick functions to your checkboxes like this:
echo '<input onclick="show_checkboxes(\''.$elt->getSchema().'\');" type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
Also, change your list-right div to this:
echo '<div class="list-right" id="list_right_'.$elt->getSchema().'" style="display:none;">';
Then add the show_checkboxes function like this:
echo'
<script>
function show_checkboxes(schema){
if(this.checked == true){
document.getElementById("list_right_"+schema).style.display = "block";
}
else{
document.getElementById("list_right_"+schema).style.display = "none";
}
}
</script>';
Finally it's correct, here's the code :
PhP:
<div>
<div>
<h4>Select your schema(s) :</h4>
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input id="' . $elt->getSchema() . '" onclick="show_checkboxes(\''.$elt->getSchema().'\');" type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
</div>
<h4>Privileges on tables by selected schemas :</h4>
<div id="div-privileges">
<?php
foreach ($schemas as $elt) {
echo '<div class="list-right" id="list_right_'.$elt->getSchema().'">';
echo '<label for="list-right">' . $elt->getSchema() . ' :</label><br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="REVOKE"/> REVOKE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="ALL"/> ALL PRIVILEGES ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="SELECT"/> SELECT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="INSERT"/> INSERT ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="UPDATE"/> UPDATE ? <br />';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="DELETE"/> DELETE ? <br />';
echo '<hr>';
echo '<input type="checkbox" name="privileges[' . $elt->getSchema() . '][]" value="CREATE"/>CREATE ? <br />';
echo '</div>';
}
?>
</div>
</div>
Javascript :
/*Way to display none all the list-right divs*/
var listRight=document.getElementsByClassName("list-right");
for (var i = 0; i < listRight.length; i ++) {
listRight[i].style.display = 'none';
}
/*function to display block when we click on the right schema*/
function show_checkboxes(schema){
if(document.getElementById(schema).checked){
document.getElementById("list_right_"+schema).style.display = "block";
}
else{
document.getElementById("list_right_"+schema).style.display = "none";
}
}
Thank you for the help.
Use this to display the correspondent div
<div id="list-schemas">
<?php
foreach ($schemas as $elt) {
echo '<input type="checkbox" name="schemas[]" value="' .$elt->getSchema() . '" class="schema"/>' . $elt->getSchema() . '<br />';
}
?>
</div>
and this in your javascript
<script>
var schmas=document.getElementsByClassName("schema");
for (var i = 0; i < schema.length; i++) {
schema[i].onChange = function(e) {
var schema = e.value;
if (e.checked) {
showMe(schema);
} else {
hideMe(schema);
}
}
}
function showMe(schema) {
document.getElementById(schema).style.display= block;
}
function hideMe(schema) {
document.getElementById(schema).style.display= = none;
}
</script>
But remeber to make all list-right divs hidden by using display:none in css

Data not showing when changing Input to textarea

This is my code to display code out of my database:
<?php
while($row = mysqli_fetch_array($myData)){
echo '<form action="details.php?ID='.$row['ID'].'" method="post">';
echo '<tr>';
echo '<input type="hidden" name="ID" value="'.$row['ID'].'">';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . '<input name="title" value="' . $row['Title'] . '">' .'</td>' ;
echo '<td>' . '<input size=85 name="detail" value="' . $row['Detail'] . '" cols="85" rows="2">'.'</td>';
echo '<td>' . $row['eventDate'] . '</td>';
echo '<td>' . $row['dateAdded'] . '</td>';
echo '<td>' . '<input type="submit" name="update" value="update" class="btn btn-default"> ' . '</td>';
echo '<td>' . '<input type="submit" name="delete" value="delete" class="btn btn-default"> ' . '</td>';
echo '</tr>';
echo '</form>';
}
?>
When I try to change the Inputs to <textarea>'s it stops showing the data from the database but only the text field. When I check Page Source it shows the data. How to format the fields like this:
echo '<td>' . '<textarea name="title" value="' . $row['Title'] . '">'. '</textarea>' .'</td>' ;
echo '<td>' . '<textarea size=85 name="detail" value="' . $row['Detail'] . '" cols="85" rows="2">'. '</textarea>' .'</td>';
textareas don't have a value attribute. Place the value between the opening and closing tags instead:
echo '<td><textarea name="title">'. $row['Title'] . '</textarea></td>' ;
echo '<td><textarea size=85 name="detail" cols="85" rows="2">'. $row['Detail'] .'</textarea></td>';
Try to insert the text between textarea tags instead of adding value attribute
echo '<td>' . '<textarea name="title">'. $row['Title'] .'</textarea>' .'</td>' ;
The general format for a text are field is <textarea name="name" cols="width" rows="height" wrap="type"> </textarea>
So there shouldn't be a value attribute. Something like this:
echo '<td>' . '<textarea name="title">'. '</textarea>' .'</td>' ;

PHP displays tags in different order

I have following problem, I have list of products in a database and want to display them in table unfortunately it plays some tricks for me because it displays one td before table even begins.
Here are the PHP functions:
<?php
function displayProduct($code,$url)
{
echo '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
echo '<input type="hidden" name="return_url" value="' . $url . '" />';
echo '<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
}
function displayItem($obj,$url)
{
echo '<tr>';
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>' . displayProduct($obj->code,$url) .'</td>';
echo '</tr>';
if(strlen($obj->description) > 2)
{
echo '<tr><td colspan="4" style="font-size: 10px;">' . $obj->description . '</td></tr>';
}
}
?>
And here is the HTML output that I get:
Could someone help me ?
The echo call from displayProduct happens before the echo call of displayItem occurs.
I can see two solutions.
1: displayProduct should return the things to write and not echo them.
2:
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>';
displayProduct($obj->code,$url);
echo '</td>';
displayProduct($code,$url) should return the string instead of printing it out:
function displayProduct($code,$url)
{
$result = '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
$result .='<input type="hidden" name="return_url" value="' . $url . '" />';
$result .='<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
return $result
}
[Edit] I should read better the questions...
But this still applies:
Also as Adrian stated, you should not echo the lines in "displayProducts", but return a string.

How to echo multiple rows with data based on checkbox input?

Got stuck trying to echo out multiple rows with data based on checkbox input. As of current code it processes data from only one checkbox, no matter how many checkboxes are ticked. Please help!
while ($row = mysql_fetch_assoc($r)) {
$pals .= '<input type="checkbox" name="pal_num[]" value="'
. $row['pal_num'] . '">' . $row['pal_num'] . '<br>';
}
if ($pal == '') {
echo '';
} else {
echo '<form name="get_pal" action="post2.php" method="POST">';
echo $pals;
echo '<input type="submit" name="post" value="Go!">';
echo '</form>';
}
post2.php:
$w = $_POST['pal_num'];
$rrr = mysql_query("SELECT * FROM pl_tab WHERE pal_num" . $w[0]);
while ($row = mysql_fetch_array($rrr)) {
echo '<tr><td>' . '&nbsp' . '</td>';
echo '<td rowspan="5">' . $row['descr'] . '</td>';
echo '<td><b>' . 'Total weight' . '<b></td>';
echo '<td>' . '&nbsp' . '</td><td>' . '&nbsp' . '</td></tr>';
echo '<td>' . '&nbsp' . '</td>';
echo '<td colspan="3">' . '&nbsp' . '</td>';
//this part should multiple based on how many checkboxes are ticked.
echo '<tr><td>' . $row['l_num'] . '</td>';
echo '<td>' . $row['pal_num'] . '</td>';
echo '<td>' . $row['weight 1'] . '</td><td>' . $row['weight 2'] . '</td></tr>';
}
echo "</table>";
}
May be this will work :
$w = "'".implode("','",$_POST['pal_num'])."'";
$rrr = mysql_query("SELECT * FROM pl_tab WHERE pal_num in (".$w.");");
...and may be you forgot a echo "<table>"; before the while :)

PHP and mySQL "UPDATE" doesn't actually update

So me and my friend came to a conclusion that it's the $_email variable that screws everything up. As long as it's hard coded in, it works. But as soon as it's left as a $_email everywhere, it doesn't. The message goes through as "updated" but it doesn't update.
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="text" name="firstName" id="firstName" value="' . $_row['firstName'] . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_row['lastName'] . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_row['email'] . '" /><br />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
//$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
/*echo '<form class="update" method="post" action="MT_vjones_updateRecord.php">';
echo '<input type="text" name="firstName" id="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_email . '" /><br />';
echo '</form>';*/
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="hidden" name="firstName" value="' . $_firstName . '" />';
echo '<input type="hidden" name="lastName" value="' . $_lastName . '" />';
echo '<input type="hidden" name="email" value="*testBACK2FUN#test.com*" />';
}
echo '<p><< Back to the Admin Page</p>';
As you can see, we put in the email address in there for testing purposes...
check the id matches what you are intending to update.
To be sure print the $_id and $_email prior to the update and after.
#user710502: You don't need to segregate quotes with double-quotes in PHP. It reads it anyway, the only time you might bother is if you are reading from an array
eg:
"UPATE midterm SET email='".$POST['email']."'"
$_query = "UPDATE midterm " .
"SET email = '$_email' WHERE id = '$_id'" ;
should be
$_query = "UPDATE midterm " .
"SET email = $_email".
"WHERE id = $_id " ;
Reason is you are using $_ before variable which is not a valid variable declaration.
Because $_ is reserved for SUPER GLOBAL in php (i.e $_SESSION,$_SERVER,$_POST,$_GET,$_COOKIE etc).
if its not a issue for you then you need to concat your variable as below.
$_query = "UPDATE midterm SET email = '".$_email."' WHERE id = '".$_id."'" ;
SOLVED! Form issues.
SHOULD BE:
<?php
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="hidden" name="firstName" id="firstName" value="' . $_row['firstName'] . '" />';
echo '<input type="hidden" name="lastName" id="lastName" value="' . $_row['lastName'] . '" />';
echo '<input type="hidden" name="email" id="email" value="' . $_row['email'] . '" />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="text" name="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" value="' . $_email . '" />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
}
echo '<p><< Back to the Admin Page</p>';
?>

Categories