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>' . ' ' . '</td>';
echo '<td rowspan="5">' . $row['descr'] . '</td>';
echo '<td><b>' . 'Total weight' . '<b></td>';
echo '<td>' . ' ' . '</td><td>' . ' ' . '</td></tr>';
echo '<td>' . ' ' . '</td>';
echo '<td colspan="3">' . ' ' . '</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 :)
Related
I am trying to make a table where data from a database is shown and by pressing on the update button the (not the 'lidnummer one')cells on the row should change to a form input field with the data from that row as the initial input values.
I've copied the idea from something I found and changed it to fit all the data I want to show, but I can't get the row to change upon pressing the 'update' button.
echo '<tr>';
echo '<td>Lidnummer</td>';
echo '<td>Voornaam</td>';
echo '<td>Achternaam</td>';
echo '<td>Huisnummer</td>';
echo '<td>Adres</td>';
echo '<td>E-mailadres(sen)</td>';
echo '<td>Telefoonnummer(s)</td>';
echo '<td>Update</td>';
echo '<td>Delete</td>';
echo '<tr>';
for ($j = 0 ; $j < $num_members ; ++$j)
{
$row = $select_result->fetch_array(MYSQLI_ASSOC);
echo '<tr>';
if(isset($_GET['lidnummer']) && $row['lidnummer'] == $_GET['lidnummer'])
{
echo '<form action="includes/update_ledenlijst.php" method="POST">';
echo '<td>' . $row["lidnummer"] . '</td>';
echo '<td><input type="text" name="voornaam" value="' . $row['voornaam'] . '"></td>';
echo '<td><input type="text" name="naam" value="' . $row['naam'] . '"></td>';
echo '<td><input type="text" name="huisnummer" value="' . $row['huisnummer'] . '"></td>';
echo '<td><select id="adres" name="adres">';
include 'includes/select_postcodes.php';
echo '</select>';
echo "<td>";
echo '<textarea>';
toon_contactgegevens("telefoonnummers", $row, $conn, "telefoonnummer", "form_data");
echo '</textarea>';
echo '</td>';
echo '<td>';
echo '<textarea>';
toon_contactgegevens('emails', $row, $conn, 'email', 'form_data');
echo '</textarea>';
echo '</td>';
echo '<td><button type="submit" class="buttons">Save</button></td>';
echo '<td>----</td>';
echo '</form>';
} else {
echo '<td>' . htmlspecialchars($row["lidnummer"]) . "</td>";
echo '<td>' . htmlspecialchars($row["voornaam"]) . "</td>";
echo '<td>' . htmlspecialchars($row["naam"]) . "</td>";
echo '<td>' . htmlspecialchars($row["huisnummer"]) . "</td>";
echo '<td>' . htmlspecialchars($row["adres"]) . ", " . htmlspecialchars($row["postcode"]) . " " . htmlspecialchars($row["woonplaats"]) . '</td>';
echo '<td>';
toon_contactgegevens('emails', $row, $conn, 'email', "table_data");
echo '</td>';
echo '<td>';
toon_contactgegevens("telefoonnummers", $row, $conn, "telefoonnummer", "table_data");
echo '</td>';
echo '<td><a class="buttons" href="home_ledenlijst.php?id='. $row["lidnummer"] . '" role="button">Update</a></td>';
}
echo "<td><a class='buttons' href='includes/delete_ledenlijst.php?id='" . $row['lidnummer'] . "' role='button'>Delete</a></td>";
echo "</tr>";
}
This will probably be something I have overlooked and is really easy.. But I am new to this so I am still learning :)
You're passing a value called id to the server:
href="home_ledenlijst.php?id='. $row["lidnummer"] . '"
But looking for a value called lidnummer:
$_GET['lidnummer']
How you identify the values needs to be the same. Try looking for the id value:
$_GET['id']
You are checking for $_GET['lidnummer'] but you are setting id='. $row["lidnummer"]
Change
id='. $row["lidnummer"]
to
lidnummer='. $row["lidnummer"]
Cheers! :)
=C=
I have a form that queries my server for data using MySQL. I am using a form that sends get requests. It doesn't show anything and I don't know why. I is so strange because my query is valid and I tested it on PHPmyadmin..I am not striving for answers only, I want to know why this happened and what is the reason behind it.
Here is my code:
<form name="get" action="Roster.php" method="get">
<select name="course" id="course">
<?php
$get = mysqli_query($con, "SELECT teaching.Course_ID FROM `teaching` WHERE teaching.F_ID=213000000 ");
while ($row = mysqli_fetch_assoc($get)) {
echo '<option value ="' . $row["Course_ID"] . '"> ' . $row["Course_ID"] . ' </option>';
}
?>
</select>
<select name="group">
<?php
$get = mysqli_query($con, "SELECT `Group_ID` FROM `teaching` WHERE `teaching`.F_ID= 213000000");
while ($row = mysqli_fetch_array($get)) {
echo '<option value ="' . $row["Group_ID"] . '"> ' . $row["Group_ID"] . ' </option>';
}
?>
</select>
<date-util format="yyyy-mm-dd">
<label for="Date" > Date </label><input id="meeting" name="date" type="date" />
</date-util>
<input type="submit" name="Send" value="Get"/>
</form>
<?php
if ($_GET['submit']) {
$sql = " SELECT enrollment.S_ID,student.ID,student.F_Name,student.L_name,attendance.Status,attendance.Date
From enrollment
INNER JOIN student On enrollment.S_ID
INNER JOIN attendance On enrollment.S_ID
where enrollment.Course_ID =" . $_GET["course"] . "and enrollment.Group_ID =" . $_GET["group"] . "and attendance.date =" . $_GET["date"] . " ";
$result = mysqli_query($con, $sql);
$message = "Please Choose Course_ID and Group_ID ";
if ($result > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Hello";
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['F_Name'] . " " . $row['L.name'] . '</td>';
echo '<td>' . $row['Date'] . '</td>';
echo '<td>' . $row['Status'] . '</td>';
echo "</tr>";
}
} else {
echo "<tr>";
echo '<td>' . $message . '</td>';
}
}
?>
$_GET['submit'] does not exist. You need to add submit as your name attribute to your button like so
<input type="submit" name="submit" value="Get"/>
Also you should use prepared statements to prevent SQL injection attacks.
Fetching questions from database and displaying in screen now what should be the logical part or how to implement for checking if selected answer is correct or not and how to store correct answer in database and verifying them.
Here is the code
<?php
// Create connection
$conn = new mysqli("localhost","root","","QuizQuestions");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br><br>";
$sql = "SELECT Question, Answer1, Answer2, Answer3, Answer4 FROM Questions";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i = 1;
while ($row = $result->fetch_assoc()) {
echo "<br>Question: " . $row["Question"] . "<br>";
echo ' A) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer1"] . '">' . $row["Answer1"] . '<br>';
echo ' B) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer2"] . '">' . $row["Answer2"] . '<br>';
echo ' C) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer3"] . '">' . $row["Answer3"] . '<br>';
echo ' D) <input type="radio" name="ans' . $i . '" value="' .
$row["Answer4"] . '">' . $row["Answer4"] . '<br>';
$i++;
}
} else {
echo "0 results";
}
$conn->close();
?>
Because all of the radio inputs have the same name. They all will be considered as same radio group. You need to have different names for different questions. Something like -
$i = 1;
while ($row = $result->fetch_assoc()) {
echo "<br>Question: " . $row["Question"] . "<br>";
echo ' A) <input type="radio" name="ans' . $i . '" value="' . $row["Answer1"] . '">' . $row["Answer1"] . '<br>';
echo ' B) <input type="radio" name="ans' . $i . '" value="' . $row["Answer2"] . '">' . $row["Answer2"] . '<br>';
echo ' C) <input type="radio" name="ans' . $i . '" value="' . $row["Answer3"] . '">' . $row["Answer3"] . '<br>';
echo ' D) <input type="radio" name="ans' . $i . '" value="' . $row["Answer4"] . '">' . $row["Answer4"] . '<br>';
$i++;
}
You also can use the row id instead of the of $i.
You have set same name for all radio buttons. You should group the radio buttons for each question. For that you can get the question id from the database and set the radio button name like
echo ' A) <input type="radio" name="ans'.$row["id"].'"
value="'.$row["Answer1"].'">'.$row["Answer1"].'<br>';
Make sure you have different names for each input field in the radio group so that it treats each question as a different record.
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>' ;
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