Add a Radio Button and Do an SQL Command in PHP - php

Good Afternoon.
I just want to ask a Question but before that let me explain it to all of you in a best way I can
As of now I have the ff.
Database: Election2016
Table: Candidate_Info
Fields: CandidateName and Position
As of now here is my code and the output of this is Show the Data in HTML Table
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con = mysqli_connect("localhost", "root", "", "election2016");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>CandidateName</th>
<th>Position</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</center>
</font>
</html>
And my target here is how can I attach a Radio Button to it? next to it? Attach a Radio Button in every row populated.
and my next target here which is very tricky to me is that when I press a button how will the data i choose with the corresponding radiobutton will be saved?
Example:
Candidate Name
Student 1
Position
President
Radio Button 1 (Example name of the Radio Button)
I selected RadioButton1 and press button "Save" how will Student 1 and President will be saved in a table?
I hope you understand TYIA

You should create one primary_key field to the table Candidate_Info as CandidateId with Auto increment and not null.
To add radio button
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><input type='radio' name='candidateid' value='".$row['CandidateId']."' />" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "</tr>";
}
while select the radio button and saving the form you should get the candidate record as
SELECT * FROM candidate_info WHERE CandidateId=[CandidateId]
replace the [CandidateId] with the selected radio button value.
After executing the above query you will be getting the selected candidate info, then you can save it.

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 can i echo data to checkbox field using JQuery Mobile and php?

In viewsessions.php, I am attempting to select multiple values from a db and insert it into grouped checkbox fields (eg. typeofactivity, employer, date, time, amount as one single checkbox field) using <input type="checkbox" name="example" id="example"> or similar in JQuery mobile, so that when displayed on the hmtl page, various checkboxes can be selected to send to another page. So far, i have managed to display the contents of the db using a table, but can't find a working solution to display the data in checkboxes?
Any ideas greatly appreciated! :)
Code so far:
viewsessions.php
<?php
$servername = "localhost";
$username = "root";
$password = "cornwall";
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.
$viewsessions = "SELECT typeofactivity, employer, date, time, amount FROM session_details";
$result = $con->query($viewsessions);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Type of Activity</th>
<th>Employer</th>
<th>Date</th>
<th>Time</th>
<th>Amount (GBP)</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["typeofactivity"]."</td>
<td>".$row["employer"]."</td>
<td>".$row["date"]."</td>
<td>".$row["time"]."</td>
<td>".$row["amount"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$con->close();
?>
echo "<tr>";
echo "<td>" .$row['typeofactivity']. "</td>";
echo "<td>" .$row['employer']. "</td>";
echo "<td>" .$row['date']. "</td>";
echo "<td>" .$row['time']. "</td>";
echo "<td>" .$row['amount']. "</td>";
echo "<td> <input type='checkbox' name='checkbox' value='' id='checkbox' /></td>";
echo "</tr>";

echo checkbox that when checked sets complete to ID in database

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.

Post 'SELECT' Value To Next Page PHP

Here is the form code:
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT ID, NAME FROM b_sonet_group ORDER BY ID DESC");
echo "<form class='form-vertical login-form' action='step-2.php' method='POST'>";
echo "<h4 class='form-title'>Step One: Choose Your Project</h4><div class='control-group'><div class='controls'>";
echo "<select>";
echo "<option value=''>Choose Your Project</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option name='ID' value='" . $row['ID'] . "'>" . $row['NAME'] . "</option>";
}
echo "</select>";
echo "</div></div>";
echo "<div class='form-actions'><button type='submit' name='submit' class='btn green pull-right'>Proceed to Step Two <i class='m-icon-swapright m-icon-white'></i></button></div></form>";
mysqli_close($con);
?>
What do I need to put on page 2 that retrieves the value ID from the form on the previous page and how do I print it so I can check it is the correct ID?
Simple I know but my brain has packed up and gone home.
You need to change your select to
echo "<select name=\"project\">";
On your second page you can get the value with
echo $_POST['project'];
You need to move the name from option to your select. Then echo $_POST['id'];

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