Update sql table of only rows where checkbox is checked php - php

I have a table of values that I am trying to update only the date of. I have a checkbox that I want only the select rows to updated when its checked and user clicks the submit button. The issue I am having is let's say I have 4 records and I only want record 1,2 and 4 updated. What is happening is records 1,2 and 3 are being updated. I know it has something to do with the array in the foreach loop. Side note I know the sql code allows for injection I am trying to get this to work first before worrying about that. Thank you for any and all help and time.
Table Code
$sql = "SELECT * FROM tblUnitMaster WHERE JobNumber = '" . $JobNumber . "' AND `EngReleaseDate` IS NULL OR `EngReleaseDate` = '' ORDER BY UnitID";
$result=$conn->query($sql);
echo "<style>";
echo "th {";
echo "background-color: #6EB4FF;";
echo "color: white;";
echo "}";
echo "</style>";
echo "<form action=pmReleaseEngineering3.php method=post>";
echo "<input type=hidden name=JobNumber value=$JobNumber />";
echo "<table border=2 cellpadding=4 cellspacing=10>";
echo "<tr>";
echo "<th><b><font size=4></font></b></th>";
echo "<th><b><font size=4>"."Job"."</font></b></th>";
echo "<th><b><font size=4>"."Code"."</font></b></th>";
echo "<th><b><font size=4>"."Model Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Serial Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Scope"."</font></b></th>";
echo "<th><b><font size=4>"."Date"."</font></b></th>";
echo "</tr>";
while ($row=$result->fetch_array())
{
echo "<tr>";
//echo "<td><font color=silver size=4>".$row[0]."</font></td>"; // Unit Number
echo "<td><input type=checkbox name=check[] value=".$row[0]." checked></td>";
echo "<td><input name=UnitID type=text font color=silver size=3 value=".$row[0]." readonly/></td>"; // Unit Number
echo "<td><font color=silver size=4>".$row[5]."</font></td>"; // Job Code
echo "<td><font color=silver size=4>".$row[2]."</font></td>"; // Model Number
echo "<td><font color=silver size=4>".$row[3]."</font></td>"; // Serial Number
echo "<td><font color=silver size=4>".$row[4]."</font></td>"; // Scope
echo "<td><input name=EngReleaseDate size=6 type=datetime value=" . date('Y-m-d'). " /></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<button class=button style=vertical-align:middle><span>Submit</span></button>";
echo "</form>";
echo "<br>";
PHP SQL Update TBL Code
include('pmconnect.php');
$JobNumber=$_REQUEST['JobNumber']; //array
$UnitID=$_POST['UnitID']; // array
$EngReleaseDate=$_POST['EngReleaseDate']; // array
if(isset($_POST['check'])){
//if(!empty($_POST['check'])) {
foreach($_POST['check'] as $a => $B){
$sql = "UPDATE tblUnitMaster SET " .
"EngReleaseDate='" . $EngReleaseDate . "'" .
"WHERE UnitID='" . $UnitID . "'";
//$result=$conn->query($sql);
if ($conn->multi_query($sql) === TRUE)
echo "<table border=1 cellpadding=4 cellspacing=10>";
echo "<tbody>";
echo "<tr>";
echo "<td><b><font size=4>Unit ID</font></b></td>";
echo "<td><b><font size=4>Eng. Release</font></b></td>";
echo "</tr>";
echo "<tr>";
echo "<td><font size=4> $UnitID </font></td>";
echo "<td><font size=4> $EngReleaseDate </font></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo"<br>";
}
}

Related

Getting database data into a html table

I need to get the data of the database to an HTML table.
There were similar questions asked before but none of them were similar to my requirement.
I need to get the first row of the database to the first row of the table.
So I did this.
echo "<table id='main-table' border='1px solid'> <thead> ";
echo "<tr>";
echo "<th>Board No</th>";
echo "<th>Number</th>";
echo "</tr></thead><tbody>";
while($query->fetch()){
echo "<form action='' class='' method='post'><tr>";
line 55 --> echo "<td>" . Board Code:".$row["board_code"] . Number:".$row["status"] . "</td>";
echo "</tr> </form>";
}//End of While
echo "</tbody></table>";
There is a syntax error in line 55 as mentioned above in the code.
Check this code:
echo "<table id='main-table' border='1px solid'> <thead> ";
echo "<tr>";
echo "<th>Board No</th>";
echo "<th>Number</th>";
echo "</tr></thead><tbody>";
while($query->fetch()){
echo "<form action='' class='' method='post'><tr>";
echo "<td> Board Code:".$row["board_code"]. "</td>";
echo "<td>Number:".$row["status"]. "</td>";
echo "</tr> </form>";
}
echo "</tbody></table>";
"<td>" . Board Code:".$row["board_code"] ." Number:".$row["status"] . "</td>";
edit your code like if it does not work please share your whole code will help you for sure
Check with this code
echo "<form action='' class='' method='post'><tr>";
line 55 --> echo "<td>" . Board Code:".$row["board_code"] ." Number:".$row["status"] . "</td>";
echo "</tr> </form>";

Submit multiple values only from one button

Hi i am trying to submit the multiple form values only from one button i have 10 records and one record has one button of submit and 10 records have 10 buttons of submit what i want there should be only one button of submit and i can submit he whole 10 records from one button:
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "<th>Action</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.' , ',') . "</td>";
if(isset($quantity)){
echo "<td>";
echo "<input type='text' name='quantity' value='{$quantity}' disabled class='form-control' />";
echo "</td>";
echo "<td>";
echo "<td>";
echo "<button class='btn btn-success' disabled>";
echo "<span></span> Submitted!";
echo "</button>";
echo "</td>";
echo "</td>";
}else{
echo "<td>";
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
echo "</td>";
echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
echo "<td>";
echo "<button class='btn btn-primary add-to-cart'>";
echo "<span></span>Submit to cart";
echo "</button>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
what i want only one button from i can submit all my values: Any help would be high appreciated:
$action = isset($_GET['action']) ? $_GET['action'] : "";
$product_id = isset($_GET['product_id']) ? $_GET['product_id'] : "1";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "1";
$image = isset($_GET['image']) ? $_GET['image'] : "1";
if($action=='added'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> added to your cart!";
echo "</div>";
}
else if($action=='failed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> failed to add to your cart!";
echo "</div>";
}
// left join to select products
$query = "SELECT p.id, p.name, p.price, p.image, ci.quantity
FROM products p
LEFT JOIN cart_items ci
ON p.id = ci.product_id
ORDER BY p.name";
$stmt = $con->prepare( $query );
$stmt->execute();
// count number of products returned
$num = $stmt->rowCount();
HTML CODE
<?php
// connect to database
include 'config/db_connect.php';
// page headers
$page_title="Cart";
include 'layout_head.php';
// parameters
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$image = isset($_GET['image']) ? $_GET['image'] : "";
// display a message
if($action=='removed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> was removed from your cart!";
echo "</div>";
}
else if($action=='quantity_updated'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity was updated!";
echo "</div>";
}
else if($action=='failed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity failed to updated!";
echo "</div>";
}
else if($action=='invalid_value'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity is invalid!";
echo "</div>";
}
// select products in the cart
$query="SELECT p.id, p.name, p.price, p.image, ci.quantity, ci.quantity * p.price AS subtotal
FROM cart_items ci
LEFT JOIN products p
ON ci.product_id = p.id";
$stmt=$con->prepare( $query );
$stmt->execute();
// count number of rows returned
$num=$stmt->rowCount();
if($num>0){
//start table
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:15em;'>Quantity</th>";
echo "<th>Sub Total</th>";
echo "<th>Action</th>";
echo "</tr>";
$total=0;
while( $row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.', ',') . "</td>";
echo "<td>";
echo "<div class='input-group'>";
echo "<input type='number' name='quantity[]' value='{$quantity}' class='form-control'>";
echo "<span class='input-group-btn'>";
echo "<button class='btn btn-default update-quantity' type='button'>Update</button>";
echo "</span>";
echo "</div>";
echo "</td>";
echo "<td>$" . number_format($subtotal, 2, '.', ',') . "</td>";
echo "<td>";
echo "<a href='remove_from_cart.php?id={$id}&name={$name}' class='btn btn-danger'>";
echo "<span></span> Delete from cart";
echo "</a>";
echo "</td>";
echo "</tr>";
$total += $subtotal;
}
echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>$" . number_format($total, 2, '.', ',') . "</td>";
echo "<td>";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}else{
echo "<div class='alert alert-danger'>";
echo "<strong>No products found</strong> in your cart!";
echo "</div>";
}
include 'layout_foot.php';
?>
Use name quantity[] instead of quantity.By this way you can receive post data as an array. After the loop use only one submit button.
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
Then receive it by using foreach
foreach($_POST['quantity'] as $qty)
{
echo $qty;
//Here you will receive all the quantities one by one.
}
Form the code
<?php
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.' , ',') . "</td>";
if(isset($quantity)){
echo "<td>";
echo "<input type='text' name='quantity[]' value='{$quantity}' disabled class='form-control' />";
echo "</td>";
echo "<td>";
echo "</td>";
echo "<td>";
echo "</td>";
}else{
echo "<td>";
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
echo "</td>";
echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
echo "<td>";
echo "</td>";
}
echo "</tr>";
}
echo "<tr><td colspan='4'>";
echo "<button class='btn btn-primary add-to-cart' type='submit'>";
echo "<span></span>Submit to cart";
echo "</button>";
echo "</td></tr>";
echo "</table>";

Selection from option based on database values

I'm working on last part on my project, I'm building web-site, in this part, I want to display options of a job ( whether the job still in progress or Completed )
I gave my row in mysql enum values, "Completed","InProgress"
and when the student pick a job, the JobStatus will be "InProgress"
and the student can change this value from his JobLists page, when it's done, he can change it to Completed. and it will be changed in the Database
and this is my code trying to, in this Code, it shows me an Error on the Update Query
JobStatus = '".$_POST['JobStatus'] is not Defined ?? any one can help PLEASE Guys
<?php
//Connect to DB
include('CIEcon.php');
$sqlCommand ="SELECT Accounts.SSU , Jobs.JobName, Jobs.Description, Jobs.DueDate,Jobs.JobId, JobsLists.JobStatus FROM JobsLists,Jobs,Accounts WHERE Accounts.SSU = JobsLists.SSU AND Jobs.JobId = JobsLists.JobId And Accounts.SSU = '".$_SESSION['SSU']."' ";
$result = mysqli_query($dbCIE,$sqlCommand) or die(mysql_error());
echo "<form action='JobsLists.php' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" .
"<select>
<option name = JobStatus[".$row['JobId']."] value='InProgress' selected> In Progress </option>
<option name = JobStatus[".$row['JobId']."] value='Completed' > Completed </option>
</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";
//Connect to DB
include('CIEcon.php');
// save the SSU for the current user to save the sata when insert jobs in jobslist
$SSU = $_SESSION['SSU'];
/////
//handle this when to save a status.
if( isset($_POST['save']) ){
if( empty($_POST['JobId']) || $_POST['JobId'] == 0 ){
echo"<h4> Status Wasn't Changed.. </h4>";
}else{
include('CIEcon.php'); //$dbCIE
foreach($_POST['JobId'] AS $i){
/// update JobsLists table with the new status..
$sqlUpdate = "UPDATE JobsLists SET JobStatus = '".$_POST['JobStatus'][$i]."' WHERE JobId = '" . $i . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));
}
// TEST ONLY ////////----------------------------------------////////////
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<h4> You have successfully Saved your statuse </h4>";
}else{ echo "<h4> Error occurred </h4> "; }
////////----------------------------------------////////////
} // end of else, when user select something..
}
?>
It's because you haven't named the select box which you are trying to send values with.. HTML <option>s don't have a name, but only a value. it is this value which is the assigned to the name of the <select> in $_POST
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" .
echo "<select name='JobStatus[".$row['JobId']."]'>";
if($row['JobStatus'] == "InProgress"){
echo "<option value='InProgress' selected>In Progress</option>";
echo "<option value='Completed'>Completed</option>";
} else {
echo "<option value='InProgress'>In Progress</option>";
echo "<option value='Completed' selected> Completed </option>";
}
echo "</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";

How to random the choices in php

I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";

php mysql how to join two table

I have these codes:
<?php
$records = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('records', $records);
if(isset($_GET['src']))
{
$sql = "SELECT * FROM students where studentnumber like '%{$_GET['src']}%'";
$cnt = mysql_num_rows(mysql_query($sql));
if ($cnt == 0)
{
echo "<script>alert('No Record Found');</script>";
}
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo " <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo " <input type='button' name='view' value='View' class='btn btn-info'>";echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
$sql = 'SELECT * FROM students';
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo " <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo " <input type='button' name='view' value='View' class='btn btn-info'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
It contains search, edit, delete and view functions...now my question is...I wanted to join the two tables in the database by the column studentnumber...
my table students contains the column Lastname, Firstname, Middleinitial, Course, Year, Section, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, ID, CSDone, CSLeft...now my another table named students2 contains the following rows ID, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, CSDone, CSLeft...I want to display the information from my both tables...for example I want to view all the records from database with a studentnumber of 20101000...do I have to inner join the tables?
I'm just a newbie in php...
Thank you in advance... :)
This is a LEFT JOIN. It will return all of the records from students1, as well as any records from students2 where the record has the same studentnumber as a record in students1:
SELECT * FROM students1
LEFT JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
An INNER JOIN returns only records that produce a match, so you will only get records where there is an identical studentnumber in both students1 and students2. Based on your comments, I believe this is the style you are looking for:
SELECT * FROM students1
INNER JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
If you want to get your head around using JOINs, I'd recommend trying both of these statements to observe the results. Then try a few other approaches, perhaps using this excellent tutorial on the Coding Horror Blog.

Categories