How to echo the results in PHP - php

I need to be able to display the results of the submitted form with radio buttons. I want the ID to display with either of the results '1' or '2' from the radio button values.
i.e. ID: 13 - Value: 2
if the row id is set within name the radio buttons work fine but I'm not sure how to display the results from it
if the name of the radio button is set to 'ans' it links all radio buttons together, when I want them working per id
Please see my below code to explain a little better - I'm just not sure how to echo this
<?php
$sql = "SELECT * FROM 'tz_todo' ORDER BY 'position' ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1' width='100%' bordercolor='#000000' style='border-collapse: collapse'><tr class='t1' bgcolor='#00204F'><th>
<font color='#FFFFFF'>ID</font></th><th><font color='#FFFFFF'>CHECK</font></th>
<th width='50'>
<font color='#FFFFFF'>OK</font></th><th width='50'><font color='#FFFFFF'>FAIL</font></th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>
<p align='center'>" . $row["id"] . "</td><td>
<p align='center'>" . $row["text"] . "</td><td align='center' width='50'><input type='radio' name='" . $row["id"] . "' value='1'><br></td>
<td align='center' width='50'><input type='radio' name='" . $row["id"] . "' value='2'><br></td></tr>";
}
echo "</table><input type='submit' value='Submit' name='submit' class='buttons'> ";
} else {
echo "0 results";
}
$conn->close();
?>
</form>
<?php
if (isset($_POST['submit'])) {
echo("????????????????????????????????????");
}
?>
I've now amended it slightly and have it generating results but now the IDs aren't linked to the two radio buttons (1 and 2)
<?php
$sql = "SELECT * FROM `tz_todo` ORDER BY `position` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1' width='100%' bordercolor='#000000' style='border-collapse: collapse'><tr class='t1' bgcolor='#00204F'><th>
<font color='#FFFFFF'>ID</font></th><th><font color='#FFFFFF'>CHECK</font></th>
<th width='50'>
<font color='#FFFFFF'>OK</font></th><th width='50'><font color='#FFFFFF'>FAIL</font></th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
$rowid = $row["id"];
echo "<tr><td>
<p align='center'>" . $row["id"] . "</td><td>
<p align='center'>" . $row["text"] . "</td><td align='center' width='50'><input type='radio' name='ans' value='1'><br></td>
<td align='center' width='50'><input type='radio' name='ans' value='2'><br></td></tr>";
}
echo "</table><input type='submit' value='Submit' name='submit' class='buttons'> ";
} else {
echo "0 results";
}
$conn->close();
?>
</form>
<?php
if (isset($_POST['ans'])) {
$answer = $_POST['ans'];
if ($answer == "1") {
echo 'OK';
} elseif ($answer == "2") {
echo 'FAIL';
}
}
?>
Please let me know if you would like me to explain a little better or differently
Thanks,
Tom

I think I understand your problem. You should name the radio button this way for example
name='ans" . $row["id"] . "'
This way, radio buttons will work together for each row.

Instead of using the name field within the radio button, I've merged it all into the value field:
<input type='radio' name='radio' value='".$rowid." - 1'>
but then I have the problem that you can only select 1 radio button for all rows

Related

Updating Page won't work

thanks to you all for this great web-site, it's my second time asking a question, I hope it's not too much ^^
I'm working on my project, and I've got kind of a weird issue with my update page,
it's basically displaying all data from a table in mysql, and when I checkbox the two rows to update them with new informations, it copies all new informations from the "(last row only)" and copy it all over the other rows, and the result is , all row become identical !!!! am I doing something wrong ? please help me guys this is my code..
I ALSO when I try to update any other row it wont update, but only update the last row...
<?php
session_start();
if( isset($_SESSION['username']) ){
include('../CIEcon.php');
echo "<form action= 'adminCleaning.php' method = 'post'>" ;
if(isset($_POST['update'])){
if( isset($_POST['id']) ){
if( empty($_POST['id']) || $_POST['id'] == 0 ){
echo"<h4> please choose something to delete </h4>";
}else{
echo $implid = implode("' , '", $_POST['id']);
$sqlUpdate = "UPDATE Cleaning SET JobName= '$_POST[jobname]',Description= '$_POST[description]',NoStudent='$_POST[nostudent]',DueDate='$_POST[duedate]' WHERE Id IN('" . $implid . "')";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate )or die(mysqli_error($dbCIE));
if (mysqli_affected_rows($dbCIE) > 0) {
echo "You have successfully updated your data.<br><br>";
}
else {
echo "The data you submitted matched the current data so nothing was changed.<br><br>";
}
} // end of else..
} // end of if isset($_POST['id']) ...
} // end of if isset($_POST['update']) ...
$sql = "SELECT * FROM Cleaning ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
/// NOW DISPLAY ALL INFO FROM CHOSEN DATABASE...
echo "
<table cellpadding ='4' border='1' width='80%' align='center'>
<tr>
<th class='tt' >Check </th>
<th class='tt'> Job's Name</th>
<th class='tt' >Description</th>
<th class='tt' > No Students needed</th>
<th class='tt' >Due Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<br>";
echo "<tr>";
echo "<td> <input type='checkbox' name='id[]' value='". $row['Id'] ."' /> </td>"; // array[] cause to edit more than one record...
echo "<td><input type='text' name='jobname' value='" . $row['JobName'] . "'> </td>";
echo "<td><input type='text' name='description' value='" . $row['Description'] . "'> </td>";
echo "<td><input type='text' name='nostudent' value='" . $row['NoStudent'] . "'> </td>";
echo "<td><input type='text' name='duedate' value='" . $row['DueDate'] . "'> </td>";
echo "</tr>";
}
echo "</table>";
/// END THE SEARCH HERE...........
echo " <br>
<div align='center'>
<input type='reset' value='clear' />
<input type='submit' name='update' value='update' />
</div> ";
mysqli_close($dbCIE);
echo "</form>";
}
else{echo "must logout to see this page..!!";}
?>
<html>
<head><title> ..Cleanding.... </title></head>
<style type="text/css">
body{
margin-top: 70px; /*space above the table....*/
background-color: #23438e;
}
table{
background-color: white;
}
.tt{
background: #f26822;
color: white ;
}
</style>
<body>
<!-- <a href= "../AdminIndex.php" > <button> Main Page </button></a> -->
</body>
</html>
The problem is that all your inputs have the same name, and you are using an IN to update, rather than an =
Change your input tags to follow this pattern:
<input type='text' name='jobname[".$row['Id']."]' value='" . $row['JobName'] . "'>
and the SQL to use this pattern:
JobName= '$_POST[jobname][$id]' ... WHERE Id = $id
If more than one id is sent at a time, you'll need a loop to loop through all the inputs and run the query.
Each row of the HTML table should be sent to the server with a unique id, and each update should be sent to the database with the appropriate data and id.

MySQL UPDATE statement is not updating database data through web form

Scenario: I have a web form where the user can manually enter data (see below). The user will submit the form and the data will be automatically added to the database.
date, order_ref, first_name, last_name, postcode, country, quantity, scott_packing, packing_price, courier_price, dispatch_type, job_status
On another page, the user will be able to only view all the jobs that are currently being (this data is taken from the database) processed and add the tracking number and edit the packing_price,courier_price and job_status and submit the new data.
http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-04-23at104045_zps2a628d50.png
Issue: When the user clicks the 'submit all' button, the user is supposed to be redirected to the thank you page which simply notifies the user that their entry has been successful however at the moment, the user is only directed to a blank page which contains the navigation menu. I have checked the database to see if the data has been updated but nothing has changed. How do i get my update statement to work so that the user can update the existing jobs?
This is the code for the page that displays all the jobs:
<?
session_start();
if(!session_is_registered(myusername))
{
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
$sql = "SELECT * FROM Jobs";
$qry = mysql_query($sql);
echo "<div class='content'>";
echo "<form class='form_edit' method='post' action='updatejob.php'>";
echo "<table id='job_list' cellpadding='0' cellspacing='0'>
<tr>
<th>Job No</th>
<th>Date</th>
<th>Qty</th>
<th>Postcode</th>
<th>Country</th>
<th>Packed by Scott</th>
<th>Packing Price</th>
<th>Courier Price</th>
<th>Tracking No</th>
<th>Dispatch Type</th>
<th>Job Status</th>
</tr>";
while($row = mysql_fetch_array($qry))
{
echo "<tr>";
echo "<td width='80' style='text-align: center;'>" . $row['order_ref'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['date'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['quantity'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['postcode'] . "</td>";
echo "<td width='100' style='text-align: center;'>" . $row['country'] . "</td>";
echo "<td width='100' style='text-align: center;'><input type='CHECKBOX' id='scott_packing' name='scott_packing' value='". $row['scott_packing'] . "'></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='packing_price' name='packing_price' value='". $row['packing_price'] . "'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='courier_price' name='courier_price' value='". $row['courier_price']."'/></td>";
echo "<td width='100' style='text-align: center;'><input type='text' id='tracking_number' name='tracking_number' value='". $row['tracking_number'] . "'/></td>";
echo "<td width='100' style='text-align: center;'>" . $row['dispatch_type'] . "</td>";
echo "<td width='100' style='text-align: center;'><select name='job_status' id='job_status'>
<option value='". $row['job_status'] ."'>". $row['job_status']. " <option value='dispatched'>Dispatched</td>";
//echo "<td width='100' style='text-align: center;'><a href='editjob.php'>edit</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='submit all'/>";
echo "</form>";
mysql_close();
?>
And here is the code that is supposed to update the data:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:../index.php");
}
include("../template/header.php");
include("../controllers/cn.php");
if (isset($_POST['submit']))
{
$order_ref = $_POST['order_ref'];
$packing_price = $_POST['packing_price'];
$courier_price = $_POST['courier_price'];
$tracking_number = $_POST['tracking_number'];
$job_status = $_POST['job_status'];
$sql_qry = "UPDATE Jobs SET '$packing_price, $courier_price, $tracking_number, $job_status' WHERE order_ref = '$order_ref'";
$query = mysql_query($sql_query);
if(!$query)
{
die('Could not update data' .mysql_error());
} else
{
header("location: updatesuccess.php");
exit;
}
mysql_close();
}
?>
Your update query is wrong. The query of update should be like
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
In this case it will be something like
UPDATE Jobs SET packing_price='$packing_price',courier_price='$courier_price',tracking_number='$tracking_number',job_status='job_status' WHERE order_ref = '$order_ref'";
and also you missed out the name property for input fields. If you don't specify it you can't access it like $_POST['packing_price'] where packing_price is be the name of the input field.
Also add method="post" to the form like
echo "<form class='form_edit' action='updatejob.php' method='post'>";
Try with this query
$sql_qry = "UPDATE Jobs SET column1 = '$packing_price', column2 ='$courier_price', .... WHERE order_ref = '$order_ref'";
And like Roland Jansen stated you are missing the name attributes on your input tags

show checkbox data mysql

I am having some minor errors and was wondering if anyone could help!
I am creating a attendance system for a college.
This Scenario:
Student logs in successfully and then wants to view his/her attendance for a particular course.
Problem: I want it to show me both checked and uncheked data from mysql, it currently shows an empty checkbox (when its meant to be checked) also at the moment it is showing numerous duplicated data, is it possible i could limit that, say for example show one record per week , it shows all data and duplicates it.
<?php
$q3= "SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'
";
$result = mysql_query($q3) or die(mysql_error());
echo "<table border='1' align='center'><tr> <th><strong>Week Number</strong></th> <th><strong>Present</strong></th> <th><strong>Notes</strong></th> </tr> ";
while($row = mysql_fetch_assoc($result))
{
extract($row);
echo
"</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present'].
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
?>
Note: I am connected successfully to database, mysql is up and running, i am using sessions, currently it does show data for the student but does not show the existing checked or uncheked value, the checkbox is empty.
Can anyone help
In your code, you're not properly defining the checkbox to be checked. Make a code that adds checked="true" if the 'present' field is 1.
<?php
$q3 = " SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes
FROM courses, course_attendance, attendance, students
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'";
$result = mysql_query($q3) or die(mysql_error());
echo "
<table border='1' align='center'>
<tr>
<th><strong>Week Number</strong></th>
<th><strong>Present</strong></th>
<th><strong>Notes</strong></th>
</tr>
";
while($row = mysql_fetch_assoc($result)) {
$checked = '';
if($row['present'] == 1) {
$checked = ' checked="true"';
}
echo "
<tr>
<td width='200' align='center'>" . $row['week_number'] . "</td>
<td width='400' align='center'>
<input type='checkbox' name='present'" .$checked . "/>
</td>
<td width='400' align='center'>" . $row['notes'] . "</td>
</tr>
";
}
echo "</table>";
?>
Your
echo
"</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present'].
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
statement should be
$present = "";
if(.$row['present']==1)
{
$present = "checked =checked/>";
}
else
{
$present = "/>";
}
echo
"" .$row['week_number'].
"" .$row['notes'].
"";
Hope this helps you. Thanks
$checked = '';
if($present == 1) {
$checked = 'checked="checked"';
}
echo "</td><td width='200' align='center'>" .$row['week_number'].
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$checked . "/>" .
"</td><td width='400' align='center'>" .$row['notes'].
"</td><tr>";
}
echo "</table>";
Try.

post value's text field OrderForm

I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}

Display results in multiple html tables

I have been making a volunteer sign up sheet for our hockey club. I display the results in a table using a while loop.
Everything works good, but I am now wanting to make the output a little more user friendly. I order the query by the date and display it in a single table. What I would like to do is display a new table for each different date. The heading of that table would be the date and the rows would have all events for that date ordered by time. I think this would make it easier for people to find events on a specific date.
Here is how my table is set up: id, date, time, game, name1, name2, name3, name4, name5
Here is the query on my main page:
<?php
//displays the table
$result = mysql_query("SELECT * FROM namestable ORDER BY date, time ASC");
displayTable($result);
?>
This is my function:
<?php
function displayTable($result){
echo "
<table border='1'>
<tr class='top'>
<th class='date'>Date</th>
<th class='time'>Time</th>
<th class='game'>Game</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='sign'>Sign Up</th>
</tr>";
while($row = mysql_fetch_array($result))
{
//change the date format here
$fdate = date('M jS, Y l', strtotime($row['date']));
echo "<tr>";
echo "<td class='date'>" . $fdate . "</td>";
echo "<td class='time'>" . $row['time'] . "</td>";
echo "<td class='game'>" . $row['game'] . "</td>";
echo "<td class='nameA'>" . $row['name1'] . "</td>";
echo "<td class='nameB'>" . $row['name2'] . "</td>";
echo "<td class='nameA'>" . $row['name3'] . "</td>";
echo "<td class='nameB'>" . $row['name4'] . "</td>";
echo "<td class='nameA'>" . $row['name5'] . "</td>";
$id = $row['id'];
echo "<td class='sign'>" . "<form name='input' action='process.php' method='POST'>
<input type='text' name='name' maxlength='25' value='Enter Name' onfocus=\"if(this.value=='Enter Name') this.value='';\"/>
<input type='hidden' name='id' value='$id'/>
<input type='submit' value='Sign Up' />
<input type='submit' name='dname' value='Remove Name' /></form>" .
"<form name='delete' action='delete.php' method='POST'>
<input type='hidden' name='id' value='$id'/>";
if(isset($_SESSION['user_id'])){
echo "<input type='submit' name='delete' value='Delete Event' /></form>" .
"</td>";
}else{
echo "</form>" .
"</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<br />";
}
?>
Users can enter a name to sign up and delete their name.
The admins can add and delete events.
Any suggestions?
Since your results are already ordered by date, you can just store the previous date in a variable and create a new table every time this changes.
$olddate = '';
while($row = mysql_fetch_array($result))
{
$fdate = date('M jS, Y l', strtotime($row['date']));
if ( $olddate != $fdate ) { // date has changed:
// end the previous table (if it exists)
if ( $olddate != '' ) {
echo "</table>"
}
// start the new table. Do something with $fdate here if you like
echo "
<h3>$fdate</h3>
<table border='1'>
<tr class='top'>
...
</tr>";
}
// print a row as before.
echo "<tr>";
....
}
// end the last table
echo "</table>";
Basically the only thing that has changed is that the $fdate is stored also in $olddate. When we process a new row, if the date has changed (ie $olddate != $fdate), we create a new table.
For each row in the mysql result we still generate a table row as before (you may want to make some changes like not including the Date column any more).
$getdates = mysql_query("SELECT DISTINCT date FROM namestable ORDER BY date DESC");
$dates = mysql_num_rows($getdate);
if($dates > 0){
echo "<table width='100%' cellspacing='0' cellpadding='0'>";
while ($rowdates = mysql_fetch_assoc($getdates)) {
$date = $rowdates['date'];
$getevents = mysql_query("SELECT * FROM namestable WHERE date = '$date' ORDER BY time ASC");
$events = mysql_num_rows($getevents);
if($events > 0){
echo "<tr><td>$date</td></tr>";
while ($rowevents = mysql_fetch_assoc($getevents)) {
$event_time = $rowevents['time'];
// all other info you want to pull here
echo "<tr><td>ROW WITH EVENT INFO HERE</td></tr>";
} // end event loop
} // end if events > 0
echo "</table>";
} // end if dates > 0
} // end date loop

Categories