Selection from option based on database values - php

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>";

Related

Get table value html

I'm trying to update a mySQL table after a button click..The button click is not the problem but I wonder how I can get the klant_pk which is unique to update a certain record in mySQL. As you see I print out the mySql table at first. So is there anyone who know how I can get the according klant_pk after I click on a button in the table..
Thanks
$result = mysqli_query($con, "SELECT * FROM bestelling");
echo "Bestellingen";
echo "<table border='1' align='center'>
<tr>
<th>Bestelling_pk</th>
<th>Klant_pk</th>
<th>Product</th>
<th>Commentaar</th>
<th>Tijd</th>
<th> Voortgang </th>
<th> Status </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['bestelling_pk'] . "</td>";
echo "<td>" . $row['klant_pk'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['opmerking'] . "</td>";
echo "<td>" . $row['tijd'] . "</td>";
echo "<td> <input type='button' value='In Wacht' onclick='return change(this);' />";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
while ($row = mysqli_fetch_array($result)) {
...
echo "<td id='klank_pk_".$row['klant_pk']."'>" . $row['klant_pk'] . "</td>";
...
echo "<td> <input type='button' value='In Wacht' onclick='change(getElementById('klank_pk_".$row['klant_pk']."').value);' />";
...
}

Updating multiple MySql entrys

I've been working on creating an internal site for our company. I haven't had many issues until now. I have been able to retrieve and insert data into my database, but now for some reason when I try to UPDATE an entry, the database can't be selected for some strange reason. I've attached a copy of my code thus far. I don't know what I am missing. Thank you!
This is my code for looking up the information in the database:
<?php
session_start();
$transport = mysqli_connect("localhost", "user", "pw", "db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$raw_date = $_POST['appt_date'];
$date = date("Y-m-d", strtotime($raw_date));
if ($raw_date == '') {
echo "Please go back and pick a date";
exit;
}
$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";
$result = mysqli_query($transport, $sql);
$i=0;
echo "<h2 align='center'>Schedule for $raw_date</h2>";
echo "<table border='2' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th></tr>";
echo "<form name='update_times' method='post' action='depart.php'>\n";
while($row = mysqli_fetch_array($result))
{
echo "<input type='hidden' name='id[$i]' value=" . $row['id'] . "";
echo "<tr>";
echo "<td align='center'>" . $row['r_name'] . "</td>";
echo "<td align='center'>" . $row['room'] . "</td>";
echo "<td align='center'>" . date("g:i A", strtotime($row['appttime'])) . "</td>";
echo "<td align='center'>" . $row['apptphone'] . "</td>";
echo "<td align='center'>" . $row['l_name'] . "</td>";
echo "<td align='center'>" . $row['address'] . "</td>";
echo "<td align='center'>" . $row['city'] . "</td>";
echo "<td align='center'>" . $row['zip'] . "</td>";
echo "<td align='center'>" . $row['buscar'] . "</td>";
echo "<td align='center'>" . $row['escort_name'] . "</td>";
echo "<td align='center'>" . $row['transfer'] . "</td>";
echo "<td align='center'>" . $row['comments'] . "</td>";
echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
echo "</tr>";
++$i;
}
echo "<input type='submit' value='Set Depart Times'>";
echo "</form>";
echo "</table>";
$_SESSION['sessionVar'] = $raw_date;
?>
This is the update code:
<?php
session_start();
$transport = mysqli_connect('localhost', 'user', 'pw', 'db_name');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
$size = count($_POST['out']);
$i=0;
while ($i < $size)
{
$departing = $_POST['out'][$i];
$departing = date("H:i:s:u",strtotime($departing));
$id = $_POST['id'][$i];
$sql = "UPDATE transport.appointments SET depart = $departing WHERE id = $id";
mysqli_query($transport, $sql) or die ("Error in query: $sql");
echo "Depart times updated!";
++$i;
}
mysql_close($transport);
?>
For some reason the update code doesn't want to select my database. Thank you again!
The MySQL UPDATE syntax is:
UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value;
It looks like you've tried to reference your database and your table in the UPDATE query: transport.appointments. I might be wrong, but I can't find anything on the internet saying that is valid syntax.
Try just referencing the table:
$sql = "UPDATE appointments SET depart = $departing WHERE id = $id";

Drop down list in a table

Why does my dropdown list seperate in the table to diff columns. Example I want 3 options to be displayed in the dropdown list. These are data from my database.
Eg: company a, company b to be displayed on the drop down list.
Instead now only one option (company a) is available on the drop list and (company b) is displayed on the next row of the table instead of together on a single drop down list.
<?
$result = mysqli_query($con,"SELECT admin_no, name, GPA, gender, job_details.job_title, job_details.no_of_vacancy FROM student_details, job_details ORDER BY `GPA` DESC ");
$result2 = mysqli_query($con, "SELECT job_title FROM job_details;");
$row2 = mysqli_fetch_assoc($result2);
while($row = mysqli_fetch_assoc($result))
{
while ($row2 = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'><option value='". $row2['job_title'] ."'> ". $row2['job_title'] ."</option></td>";
echo "</tr>";
}
}
echo "</table>";
?>
</form>
Try this, You have missed to close </select> tag, I have rewritten the code i.e moved <option> tag generation above table generation section. So that to avoid unneccessary looping.
<?php
$result = mysqli_query($con,"SELECT admin_no, name, GPA, gender, job_details.job_title, job_details.no_of_vacancy FROM student_details, job_details ORDER BY `GPA` DESC ");
$result2 = mysqli_query($con, "SELECT job_title FROM job_details");
$row2 = mysqli_fetch_assoc($result2);
/*options sections start*/
$options= '';
while ($row2 = mysqli_fetch_array($result2))
{
$options .='<option value="'. $row2['job_title'] .'"> '. $row2['job_title'] .'</option>';
}
/*options sections end*/
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>".$options."</select></td>";
echo "</tr>";
}
echo "</table>";
?>
Try this:
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>";
while ($row2 = mysqli_fetch_array($result2))
{
echo "<option value='". $row2['job_title'] ."'> ". $row2['job_title'] ."</option>";
}
echo "</select></td>";
echo "</tr>";
}

Associate Button Value with Query Results In Seperate Tables

I have this query:
$result = mysqli_query($con,"SELECT * FROM b_tasks_report WHERE TASK_ID=$taskid ORDER BY WEEK_ID");
$current_week_id = -1;
while($row = mysqli_fetch_array($result))
{
if($current_week_id != $row['WEEK_ID'])
{
if($current_week_id != - 1)
{
echo "</table>";
}
echo "<table>";
echo "<tr class='no-border'><td class='no-border'><div class='task-detail-title'>Week Number: " . $row['WEEK_ID'] . "</div></td></tr>";
echo "<tr>";
echo "<th width='100'>Day</th>";
echo "<th width='75'>Start</th>";
echo "<th width='75'>End</th>";
echo "<th width='100'>Billable Hours</th>";
echo "<th width='100'>Non Billable Hours</th>";
echo "</tr>";
$current_week_id = $row['WEEK_ID'];
}
echo "<tr>";
echo "<td class='tdclass'>" . $row['DAY'] . "</td>";
echo "<td class='tdclass'>" . $row['START'] . "</td>";
echo "<td class='tdclass'>" . $row['END'] . "</td>";
echo "<td class='tdclass'>" . $row['BILLABLE_HOURS'] . "</td>";
echo "<td class='tdclass'>" . $row['NON_BILLABLE_HOURS'] . "</td>";
echo "</tr>";
}
if($current_week_id != - 1)
{
echo "</table>";
}
This provides me with seperate tables for each week ID. However I am looking to get buttons to display below each table which are associated with the results above. Is it possible to add a button with the value of WEEK_ID. Currently if I add a button to the top and the bottom with value:
<input type='image' name='submit' src="image/button.jpg" value=" . $row['WEEK_ID'] . ">
It doesn't show the right ID for the top tables and it shows nothing for the bottom. I do understand why this is but is there anyway I can associate this button underneath the table?
Use this outside the PHP tag:
<input type="image" name="submit" src="image/button.jpg" value="<?php echo $row['WEEK_ID'] ?>">
Use this inside the PHP tag.
echo '<input type="image" name="submit" src="image/button.jpg" value="' . $row['WEEK_ID'] . '">';

How do i delete the records from the table?

I am using a form to get the input of username and password to store the value into the database, once the form is submitted i have defined a table to show all the values from the users table, it have 3 fields (id, name, pass) i want to delete each record by it's id .
i am fetching the data from the users table by using the following code:
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "</tr>"; }
i want to add the delete hyperlink to delete the particular records by id.
i tried using the following code and i couldnt achieve it.
if(mysql_num_rows($result_select) > 0) {
if(isset($_POST['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_POST['id'];
$result_delete = mysql_query($query_delete);
}
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</table>";
}
I am a newbie to programming, i would appreciate if someone explain me in simple words.. thank you :)
First of all put your delete code in your page like this:
if(isset($_POST['submit'])) {
for($i = 0; $i < count($_POST['del']); $i++)
{
// check which records to delete
if (isset($_POST['del'][$i]))
{
$query_delete = "DELETE FROM users WHERE id = " . (int) $_POST['del'][$i];
$result_delete = mysql_query($query_delete) or die(mysql_error());
}
}
echo 'Record Deleted !!' . '<br /><br />';
}
Later put your select code and modify it like this:
echo '<form action="" method="POST">';
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\"></td>";
echo "</tr>";
}
echo '<input type="submit" name="submit">';
echo '</form>';
You need to change $_POST to $_GET, so this should work.
if(isset($_GET['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_GET['id'];
$result_delete = mysql_query($query_delete);
and also put values of attributes inside double quotes, like this
echo '<td>Delete</td>';

Categories