Updating Page won't work - php

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.

Related

How to echo the results in 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

PHP/HTML: activate users through admin page using radio button

I'm creating an activate/deactivate based on the admin input.
The idea is to allow the admin to view all the accounts from accounts table and choose one from Selections (activate or deactivate ) using radios input.
I really would appreciate it if you guys can help me out on this page, my project is almost done.
Thank you All for your suggestions, the I'm just trying to let the admin have the option to activate or deactivate from the user account, using this code.
The Code basically view all the informations of the accounts, and the admin just checkbox anyone she wants to activate and choose from the radios (activate/deactivate) and based on her input will set the value in the database to 0 which is deactivate and 1 to activate .
thank you
<?php
session_start();
if( isset($_SESSION['username']) ){
//Connect to DB
//include_once("Project/CIEconn.php");
$mysqlCON= mysqli_connect("localhost", "root", "","CIE") or die(mysqli_connect_error());
mysqli_select_db($mysqlCON,'CIE') or die ("no database");
if( isset($_POST['ActivateUsers']) ){
if( empty($_POST['Id']) || $_POST['Id'] == 0 ){
echo"<h4> please choose someone to activate </h4>";
}else{
$impid = implode("' , '" , $_POST['Id']);
$sqlDelete = "UPDATE Accounts SET Activated='$_POST[activate]' WHERE Id IN ('" . $impid . "')";
$DeleteQuery = mysqli_query($mysqlCON,$sqlDelete) or die ("Error : ".mysqli_error($mysqlCON));
}
}
$sqlCommand = "SELECT * FROM Accounts ";
$result = mysqli_query($mysqlCON,$sqlCommand) or die(mysql_error());
echo "<h1> Activate / Deactive User Account </h1>";
echo "<table border='1' width = 80% align=center >
<tr>
<th>Check </th>
<th>SSU ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Type </th>
<th>Activated</th>
<th>Activate/Deactive</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<form action='ActivateD.php' method='post'>";
echo "<td> <input type='checkbox' name='Id[]' value='". $row['SSU'] ."' /> </td> " ;
echo "<td align=center > " . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['Email'] . "</td>";
echo "<td align=center>" . $row['First_Name'] . "</td>";
echo "<td align=center>" . $row['Last_Name'] . "</td>";
echo "<td align=center>" . $row['userType'] . "</td>";
echo "<td align=center>" . $row['Activated'] . "</td>";
echo "<td align=center>" .
"
<input type='radio' name='activate' value='1' > Activate
<input type='radio' name='activate' value='0'> Deactive
</form>"
. "</td>";
echo "</tr> ";
}
echo "</table>";
echo " <br>
<form action='ActivateD.php' method='post'>
<div align='center'>
<input type='submit' name='ActivateUsers' value='Activate/Deactive'>
</div>
</form>";
mysqli_close($mysqlCON);
}
else{echo "must logout to see this page..!!";}
?>
<html>
<head><title> Activate / Deactive User Account </title>
<style type="text/css">
body{
background-color: #23438e;
}
table{
background: white;
}
h1{
color: #FF942B;
text-align: center;
padding-top : 50px;
text-decoration: none;
}
</style>
</head>
<body>
</body>
</html>
There appear to be several problems with your script:
You are wrapping a form around each of your radio inputs and checkboxes but a separate form around your submit button. In this setup, the form isn't going to send the right data when submitted.
Duplicating radio boxes with the same name on every row. Because you are only utilizing a single value for active/inactive, you should place one set of radio buttons at the bottom of your form.
Your SQL query escaping is incorrect:
Instead of
$sqlDelete = "UPDATE Accounts SET Activated='$_POST[activate]' WHERE Id IN ('" . $impid . "')";
You should use $sql = "update Accounts set Activated='" . $_POST['activate'] . "' where Id in ('" . $impid . "')";
Lack of error handling and input sanitization

php table array direction

<div class="section" id="section2">
<div class="slide" id="slide1">
<div class="box">
<div class="haha1">Library</div>
<p>
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =5 ;
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
}
?>
</table>
<br>
<center>Page 1</center>
</div></div>
<?php
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result))
{
?>
<table class="box1" width="700" border="1">
<tr>
<td><img src="<?php echo $row['image']; ?>" height="250" width="150"><br>
<?php echo $row['bookname']; ?><br>
<?php echo $row['author']; ?><br>
<input type="submit" name="add" value="Add Cart"></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<?php } ?>
This is my table when I add new item list down vertically but I want array item horizontally example [item1,item2,item3,item4] after array 4 item horizontally only create another td then array again horizontally 4 time.
Required output:
{1,2,3,4}
{5,6,7,8}
array from left to right
else array vertically {1,5} {2,6},{3,7],[4,8]
try change the table this way
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =5 ;
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
}
?>
</table>
for testing the echo try a simple case like this (only the button)
echo "<td><input type='submit' name='add'
value='Add Cart'></td>";
In this sample (I have not you db an your IMG so i have change the var value with string value you can see 2 row of pic 4 in the first and 2 in the second.
Then if you don'obtain the same result could be you have some problem with your select or your value..
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =4 ;
$book = 0;
/* $sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){*/
while( $book<=5 ){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
/* echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";*/
echo "<td><img src='" . "row['image']" .
"' height='250' width='150'><br />" ."row['bookname']<br />" . "row['author']<br />" . "<input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
$book++;
}
?>
</table>

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.

Categories