echo checkbox that when checked sets complete to ID in database - php

I have a table that I am echoing out and now I added a check box to the end of it.
I would like when that box is checked. It can be checked on multiple items and then the save button is clicked. It would go to the post page and set complete in the database to "Yes" for all the IDs that were checked.
Problem is I do not understand how the checkbox will know which ID is which once it goes to the post page.
<?php
$conduct = $_SESSION['username'];
$query = mysqli_query($con, "SELECT * FROM newworders WHERE train = 'Yes' AND conductor = '$conduct' AND complete = ' '");
echo "<table id='tb' border='1'>
<tr class='head'>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . '' . $row['first'] . '<br />' . "</td>";
echo "<td>" . '' . $row['last'] . '<br />' . "</td>";
echo "<td>"."<input type='checkbox' value='Yes' name='complete'" . "</br>". "</td>";
}
<div class='new'>
<form action="savepending.php" method='POST'>
<input type='submit' name ='save'/>
</form>
</div>
Then the post page.
<?php
if(isset($_POST['save']))
{
$query = mysqli_query($con, "UPDATE newworders SET complete = 'Yes' WHERE ")
}
?>
I have no idea what to put in the WHERE part. I just dont understand how it will tell which ID is which.

Make your checkboxes an arrray, indexed by newworders primary key. Then you can just iterate through complete. PHP arrays are associative, so you will only have as many elements as you have checkboxes.

Related

Create a table from and SQL database containing a drop down menu with a list of names from another SQL table

I need to create a table with a drop down menu and submit button in each row.
The drop down menu contains a list of advisers from an SQL table. When i select and adviser and i press the submit button the id of the item in the current row along with then selected adviser id or name must be sent to another page. In my case it is sent to delete.php.
My code bellow displays a drop down menu and a submit button for each row of the table, however when you press the submit button it will only work correctly if you press the submit button located at the bottom of the table, if i press any other it appears to not send the info from the drop down menu.
( i know my code appear messy, i am experimenting if something is unclear ask me and i will clarify. )
Thank you very much!
<!DOCTYPE html>
<html>
<body>
<?php
//this is he code for the qeue
// connect to the database udinh sqli
$con = get_sqli();
// get results from database
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
//select whole list of students from walk_in
mysqli_select_db($con,"login");
$sql="SELECT * FROM walk_in";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
mysqli_close($con);
//Table to dispaly qeueu of students
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th><th>Advisor Student wants to see</th><th>P ID</th><th>Select Advisor to notify on send</th><th>Send Student</th><th> </tr>";
echo "<tr>";
//create a table of students by displaying all the data from result and adding a button
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Advisor'] . "</td>";
echo "<td>" . $row['pid'] . "</td>";
// echo '<form action="delete.php?id2=' . $row['id'] . '" method="post">';
// drop down menu for selecting advisor as a form submission
// used to name each submit button with the id from walk_in
$formId = $row['id'] ;
echo "<td>" ;
//create a form to submit the sleected advisor and the seelcted student to be removed from the queue
echo '<form action="delete.php?id=' . $row['id'] . '" method="post">';
//another query used to retreive the list of advisors to pupulate the drop down menu
//create a drop down menu with advisors resulting from the queue
echo '<select name="formStatus">';
$con = get_sqli();
mysqli_select_db($con,"login");
$sql="SELECT * FROM login_details WHERE level = 0 AND logged = 1";
$result2 = mysqli_query($con,$sql);
if (!$result2) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//loops through all advisors for drop down menu creation
while ($row2 = mysqli_fetch_array($result2)) {
$id = $row2['id'];
echo '<option value="'.$id.'">'.$id.'</option>';
}
echo'<option selected="selected"></option>';
echo '</select>';
echo '<td><input type="submit" name="formSubmit" value= "'.$formId.'" /><td>';
//echo '<td><input type="submit" name="formSubmit" value= /><td>';
//echo '<td>Send</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Here are the tables i am using:
login_details table containing ADVISER details
I forgot to close the form, the issue has been fixed. Thank you all!

How to access values of checkboxes in php mysql

I have a table with pizza name, pizza type and price. I used a loop to print all the items in a table.
<form method="Cart.php" method="post">
<table border="1" cellpadding="10">
<tr>
<th>Pizza name</th>
<th>Pizza type</th>
<th>Price</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['subcat_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['price']. "</td>";
echo "<td><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"checkbox\"></td>";
echo "</tr>";
}
echo "</table>";
?>
<input type="submit" name="addToCart" id="addToCart"/>
</form>
Now I want to access the pizza names and prices as I click the submit button.
You can access them by first checking if it exists...
if(isset($_POST['checkbox']){
$checkbox_value = $_POST['checkbox'];
}else{
$checkbox_value = ""; // set a default value here
}
I should add that checkboxes that are NOT "checked" will not pass along in a POST, so you need to explicitly check if it has been "checked" by calling
if(isset($_POST['checkbox'])){}
Which at that point you can decide to set a value yourself or use the value you set in the form.
Here is another way, Hope this helps.
if(isset($_POST['addToCart'])){
$check_list = $_POST['checkbox'];
foreach ($check_list as $checked=>$value) {
//Here you got all checked values in "$checked"
//Eg: to move checked values to array
array_push($pizzaOrdered, $value);
}
}
Try this just use the name of the check box $_POST['checkbox']

How to get the ID of all the checked boxes on displayed data base QUICKFIX?

I have displayed a table of my data from the data base with check boxes to the left. I want to find a way to link the check boxes to the question number (ID). when I hit submit I want the selected id's to be echoed. pretty much I want someone to be able to select the questions they want and then display them.
<?php
$con=mysqli_connect("####","####","#####","###");
$result = mysqli_query($con,"SELECT * FROM q_and_a ");
echo "<table border='1'>
<tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" name="questions[]" value="$id"></td>';
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
submit button
<form method="POST" action="makeTest.php">
<input type="submit" name="make" value="Make Test">
</form>
Make some edits to your code then it will work.
1. Change your query by adding fields not * (to ensure performance and display order)
$result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");
then before while block open form tag(HTML)
<?php
//above codes will be there as you show before
echo '<form method="POST" action="makeTest.php">';
while($row = mysqli_fetch_array($result)){
{ $id=$row['id']; // initialize your id here, so as to pass it in checkbox too
// then continue with your code
}
?>
<input type="submit" name="make" value="Make Test">
</form>
in maketest.php yo can hande ckeckbox using foreach, see below
foreach($_POST['questions'] as $questions){
//do your job
}

Updating data from checkbox clicked

My Code so far. The data gets pulled correctly
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Request");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Prayer Request</th>
<th>Deactivate Request</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Reg_F_Name'] . "</td>";
echo "<td>" . $row['Reg_L_Name'] . "</td>";
echo "<td>" . $row['Reg_Request'] . "</td>";
echo "<td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$rows['Reg_ID']. "\" /></td>";
echo "</tr>";
}
echo "</table>";
echo
"<form action='' method='post'>
<input type='submit' name='use_button' value='Update' />
</form>";
if(isset($_POST['use_button']))
{
echo "hey";
$del_id = $_POST['checkbox'];
$detectinglocations = 'your database table name';
foreach($del_id as $value){
$sql = "Update Request set Reg_Status=0 WHERE Reg_ID='".$value."'";
$result = mysql_query($sql);
}
}
mysqli_close($con);
?>
Nothing Happens when I Click Submit. I am wanting it to Update the reg_Status to 0 for every check box that is click. So whats my problem. Thank you in advance for helping!
try adding an input hidden field with same name as the checkbox name before each checkbox and with value 0 .
The checkbox doesnt get posted when not checked.

mysql php updating multiple entries through populated drop down menu

I'm having an issue trying to update multiple entries in my database via a php populated drop down menu. Here is the code on my page that populates the table showing me all entries currently in my database:
$result = mysqli_query($con,"SELECT * FROM Submissions");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Title</th>
<th>Text</th>
<th>Public Post OK?</th>
<th>Date/Time Submitted</th>
<th>Approved?</th>
<th>Test Approved</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . nl2br($row['text']) . "</td>";
echo "<td>" . $row['publicpost'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td><select name=\"approved\"><option value=\"" . $row['approved'] . "\">" . $row['approved'] . "</option><option value=\"yes\">Yes</option><option value=\"no\">No Again</option></select></td>";
echo "<td>" . $row['approved'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<br><br>
<form action="update.php" method="post">
<input type="submit" name="SubmitButton" value="Update" class="submit" style="cursor:pointer;">
</form>
<?php
mysqli_close($con);
?>
This is the php code for "update.php":
$approved = $_POST['approved'];
mysqli_query($con,"UPDATE Submissions SET approved = $approved");
$update_query= "UPDATE Submissions SET approved = '$approved'";
if(mysqli_query($con,$update_query)){
echo "updated";}
else {
echo "fail";}
?>
<form action="approvesubmissions.php">
<input type="submit" value="Approve Submissions page">
</form>
The goal is to have the ability to update the field "approved" with a drop down menu from "NO" to "YES" or vice versa. Instead, what is happening with this query, is that it is erasing the data in the "approved" field instead of updating it. I'm somewhat new to php and i have researched a TON on this and have come up with no solutions. Any help is GREATLY appreciated!
First, let's assume 'approved' is a TINYINT(1) or something.
Your select html should be more like this. It will autofill based on the DB value.
$selected = 'selected="selected"'; // pre-selection attribute
$isApproved = !!$row['approved']; // is this active? (approved is 1 or 0)
echo '<select name="approved">
<option value="1" ' . ($isApproved ? $selected : '') . '>Yes</option>
<option value="0" ' . (!$isApproved ? $selected : ''). '>No</option>
</select>';
Secondly, your form is at the bottom of the table, but your input that you want is in the table. When you submit your form, there is no $_POST['approved'], because that's technically not in a form. To fix, you'll need to put your opening form tag at the top before the table. Then, you'll want to put your submit button and closing form tag at the end, after you've echoed the table out.
Thirdly, your post.php page should NOT ever take user input directly into a query. But, simply do this:
// Convert input to boolean answer, then int (for the query).
$approved = isset($_POST['approved']) ? (int)!!$_POST['approved'] : 0;
mysqli_query($con,"UPDATE Submissions SET approved = '$approved'");
While we're on the topic, this would be a great time to jump into prepared statements for your project. It might sound scary, but it can save you from SQL injection.
// Make the prepared statement
$query = mysqli_prepare("UPDATE Submissions SET approved = ?");
// Safely bind your params
mysqli_stmt_bind_param($query, "i", $approved);
// Run it
mysqli_stmt_execute($query);
// "close" the statement (hint: it's reusable for things like bulk updates, etc)
mysqli_stmt_close($query);

Categories