Updating multiple MySql entrys - php

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

Related

How to make search based on pickup date and return date?

In the car page,i already input all the data into mysql database(car table).So i want to ease the user to find the car that available on pickup date and return date.So,I made search pickup date and return date in search page.But the error is "could not be execute".
This is using mysql and php.
car.php
</body>
<table align='center' width='40%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<form action="search.php" method="post" enctype="multipart/form-data">
<td><input type="date" name="Pickup_date" placeholder="Pickup_date" style='height:38px' required /></td>
<td><input type="date" name="Return_date" placeholder="Return_date" style='height:38px' required /></td>
<td><input type="submit" name="submitbook" value="Search" style='background-color: blue; border: none; color: white; padding: 10px 10px; text-align: center; text-decoration: none; display: inline-block; font-size: 14px;'/></td>
</div>
</form>
</table>
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo '<input type="hidden" name="pickup_date"
value="'.$pickup_date.'">';
echo '<input type="hidden" name="return_date"
value="'.$return_date.'">';
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
search.php
<?php
$link = mysqli_connect("localhost", "root", "", "online_car_rental");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$search=$_GET['s'];
$query="SELECT * FROM car WHERE date BETWEEN 'pickup_date' AND
'return_date'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>Add to Booking</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100' width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td>" . $row['pickup_date'] . "</td>";
echo "<td>" . $row['return_date'] . "</td>";
echo "<td><button onclick=\"booking_car('" . $row['car_id'] .
"')\">Book</button>
</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
</html>
i expect the output is based on pickup_date and return_date.But the output is "ERROR:Could not able to execute".
You Can check with this , When you have two tables you can use inner join query to fetch the data
SELECT columns
FROM table WHERE pickup_date < 'your_pickup_date' AND return_date >= 'your_pickup_date' ORDER
BY car or date;
Without knowing your schema, it's difficult to provide a detailed solution, but an example of a plausible query might be as follows:
"
SELECT c.olumns
, y.ou
, a.ctually
, w.ant
FROM car c
WHERE pickup_date < :return_date
AND return_date >= :pickup_date
ORDER
BY pickup_date
, car_id;
"

can anyone help me how to pass the results of separately to other page when schedule is clicked

1.
<?php
$sql = "(SELECT id,Firstname, Lastname, MI, Course, Date_Enrolled FROM listofstudents )";
$result = mysqli_query($con, $sql);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0){ // build a table to show results
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td align='center'>" . $row['Firstname'] . "</td>";
echo "<td align='center'>" . $row['Lastname'] . "</td>";
echo "<td align='center'>" . $row['MI'] . "</td>";
echo "<td align='center'>" . $row['Course'] . "</td>";
echo "<td align='center'>" . $row['Date_Enrolled'] . "</td>";
echo "<td align='center'> <a href='schedule.php?name=".$row['Firstname']."lastname=". $row['Lastname'] . " mi=". $row['MI'] ."'>Schedule</a></td>";
echo "</tr>";
}
} else{
echo "No rows returned.";
}
?>
List item
You are sending data in url witch is not safe
You can send tha ID and on the requested page you can the get the row by sql query.

PHP Form - Update database row using one or more checkboxes

Currently stuck trying to configure a php page to update a MySQL database row "orderStatus" by checking one or more checkboxes. It should update the orderStatus of the selected row(s) to "validated" once the form is submitted. It's updating only the first row of the table likely due to $orderID = $_POST['id']; in the validate.php snippet. What I tried to do is retrieve all the orderIDs of the rows that have been checked and assign it to the variable/array $orderID, so that the orderStatus is changed only for those rows.
Here is the HTML:
<html>
<header>
<title>Validate an Order</title>
</header>
<body>
<h1>Validate an Order</h1>
<h4>Showing all unvalidated orders.</h4>
<br/>
<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM orders WHERE orderStatus = 'processing' ORDER BY orderDate DESC");
echo "<table border='1'>
<tr>
<th>Validate?</th>
<th>OrderID</th>
<th>OrderDate</th>
<th>OrderShipDate</th>
<th>OrderType</th>
<th>OrderMedia</th>
<th>OrderContent</th>
<th>OrderStatus</th>
<th>OrderQuantity</th>
<th>OrderCost</th>
<th>OrderDeposit</th>
<th>OrderDesc</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<form action='validate.php' method='post'><input type='hidden' name='id' value='".$row['orderID']."'><input type='checkbox' name='validate[]' value='validated'>" . "</td>";
echo "<td>" . $row['orderID'] . "</td>";
echo "<td>" . $row['orderDate'] . "</td>";
echo "<td>" . $row['orderShipDate'] . "</td>";
echo "<td>" . $row['orderType'] . "</td>";
echo "<td>" . $row['orderMedia'] . "</td>";
echo "<td>" . $row['orderContent'] . "</td>";
echo "<td>" . $row['orderStatus'] . "</td>";
echo "<td>" . $row['orderQuantity'] . "</td>";
echo "<td>" . $row['orderCost'] . "</td>";
echo "<td>" . $row['orderDeposit'] . "</td>";
echo "<td>" . $row['orderDesc'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'></form>";
mysqli_close($con);
?>
</body>
</html>
And here is the PHP for the form:
<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$orderID = $_POST['id'];
if(isset($_POST['validate'])){
foreach($_POST['validate'] as $validate){
mysqli_query($con,"UPDATE orders SET orderStatus = '$validate' WHERE orderID = $orderID");
}
}
mysqli_close($con);
?>
Here's what the page looks like.
Validate an Order
Any help is appreciated!
Problem is Form is inside while loop.When you're using with you need to either put the tags completely outside the , or have the entire inside one . Any other structure breaks the syntax of the and will be ignored by the browser, or rendered incorrectly.
Please try this
<html>
<header>
<title>Validate an Order</title>
</header>
<body>
<h1>Validate an Order</h1>
<h4>Showing all unvalidated orders.</h4>
<br/>
<?php
$con=mysqli_connect("***","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM orders WHERE orderStatus = 'processing' ORDER BY orderDate DESC");
echo "<form action='validate.php' method='post'>
<table border='1'>
<tr>
<th>Validate?</th>
<th>OrderID</th>
<th>OrderDate</th>
<th>OrderShipDate</th>
<th>OrderType</th>
<th>OrderMedia</th>
<th>OrderContent</th>
<th>OrderStatus</th>
<th>OrderQuantity</th>
<th>OrderCost</th>
<th>OrderDeposit</th>
<th>OrderDesc</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='hidden' name='id' value='".$row['orderID']."'><input type='checkbox' name='validate[]' value='validated'>" . "</td>";
echo "<td>" . $row['orderID'] . "</td>";
echo "<td>" . $row['orderDate'] . "</td>";
echo "<td>" . $row['orderShipDate'] . "</td>";
echo "<td>" . $row['orderType'] . "</td>";
echo "<td>" . $row['orderMedia'] . "</td>";
echo "<td>" . $row['orderContent'] . "</td>";
echo "<td>" . $row['orderStatus'] . "</td>";
echo "<td>" . $row['orderQuantity'] . "</td>";
echo "<td>" . $row['orderCost'] . "</td>";
echo "<td>" . $row['orderDeposit'] . "</td>";
echo "<td>" . $row['orderDesc'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' name='submit' value='Submit'>"
. "</form>";
mysqli_close($con);
?>
</body>
</html>
Thanks all! I wound up changing the input checkbox tag in the HTML to: <input type="checkbox" name="vid[]" id="validate" value='.$row['orderID'].' This grabs the orderIDs of those rows selected into the vid[] array.
Then in validate.php, the query sets the status to "validate" using $vid from foreach():
// if the vid array exists
if(isset($_POST['vid'])) {
// Loop through vid array "containing orderIDs" and set orderStatus to "validated" only for those orderIDs
foreach($_POST['vid'] as $vid) {
mysqli_query($con,"UPDATE orders SET orderStatus = 'validated' WHERE orderID = '$vid'");
}
}

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

Undefined index for mysql result

<?php
$result = mysqli_query($conn,"SELECT * FROM news ORDER BY date DESC
LIMIT 5")or die(mysql_error());
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
echo "<table align='left' CELLPADDING='50' >";
while($row = mysqli_fetch_array($result))
{
if ($row['photoid'] == NULL){
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" .$row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
}
else
{
$result = mysqli_query($conn,"SELECT news.title,news.date,news.news,photo.photo FROM news, photo WHERE news.photoid = photo.photoid ORDER BY date DESC") or die(mysql_error());
$row = mysqli_fetch_array($result);
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" . $row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
echo "<td style='width: 800px'>" . '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode( $row["photo"] ).'" >' . "</td>";
}
}
echo "</table>";
mysqli_close($conn);
?>
SO the website shows a news column, the code gets the news data from the database, if there is a photoid (if the news has a photo to go with it) then it adds the news to the table with a photo. If there is no photo is adds the news to the table without one. Simple. At the moment for testing I have two news articles both with photos, the first one works perfectly then the second errors
Undefined index: photoid in
For the line if ($row['photoid'] == NULL){. There is a photoid.
You need to check if variable/key is set before checking the value. Try this:
if (isset($row['photoid']) && $row['photoid'] == NULL){

Categories