Solved

PHP MySQL INSERT

Posted on 2013-10-24
6
673 Views
Last Modified: 2013-10-25
This is a final followup to a large piece of code I have been working on.

What the following does:
1. It runs a MySQL Query on two Tables (Schedule, Employees).
it checks to see if an employee is available on a certain date.  If they are all names of available employees are printed out.
2. The Employee_names are inserted into a form so that you can set a schedule date for that employee.

3. It runs a MySQL Query on another two tables (Schedule, Assets)
4.it checks to see if an asset is available on a certain date.  If they are all asset names of available assets are printed out.
5. The Asset_names are inserted into a form so that you can set a schedule date for that Asset.

I have added at the very end, a select box so as that you can associate the employee insert or asset Insert with a specific Workorder_id.
You can find the function on line: 171 - 174

How can I get this to pass that value into the form?

	$pdo = new PDO("mysql:host=localhost;dbname=lchsraid_lloyds", "username", "password");
    	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

		if (!empty($_POST)) {
	
			$start_date = $_POST['start_date'];
			
			if(isset($_POST['end_date'])) {
				$end_date = $_POST['end_date'];	
			} 
			
			if(empty($_POST['end_date'])) {
				$end_date = $_POST['start_date'];		
			}
			
			$s_date = new DateTime($start_date);
			$start = $s_date->format('l F d, Y');
			$e_date = new DateTime($end_date);
			$end = $e_date->format('l F d, Y');
			
			
			?>
	<div class="schedule-left">
		<form name="add_schedule" action="add_schedule.php" method="POST" />
		<fieldset>
		<span class="h3">DATES REQUESTED:: Employees</span><br /><span class="h4"><?php echo 'Start date:</span><span class="h5">'.$start.'</span><br /><span class="h4">End Date:</span><span class="h5">'.$end.'</span>'; ?>
				
		<table>			
			
<?php
try {
//create the query string
$sql = <<<EOT
SELECT  employee_id, first_name, last_name, driver_class_a, driver_class_b, driver_class_c
FROM    employees e
WHERE   NOT EXISTS (
        SELECT  s.employee_id
        FROM    schedule s
        WHERE   (e.employee_id = s.employee_id)
		AND (s.start_date BETWEEN :start_date AND :end_date
		OR s.end_date BETWEEN :start_date AND :end_date)
        )
ORDER BY driver_class_c DESC, driver_class_b, driver_class_a DESC
EOT;

//set the named parameters
$params = array(
	'start_date' => $start_date,
	'end_date' => $end_date
);

//prepare the query
$employees = $pdo->prepare($sql);

//execute the query
$employees->execute($params);

//loop through the results
while ($row = $employees->fetch(PDO::FETCH_OBJ)):
	//now you can echo out your table rows
	
	
	if($row->driver_class_a == "yes" || $row->driver_class_b == "yes" || $row->driver_class_c == "yes") {	 
		echo '<tr>
				<td width="25"><input type="checkbox" name="driver['.$row->employee_id.'][ticked]"/>
				<input type="hidden" name="driver['.$row->employee_id.'][first_name]" value="'.$row->first_name.'" />
				<input type="hidden" name="driver['.$row->employee_id.'][last_name]" value="'.$row->last_name.'" />
				<input type="hidden" name="driver['.$row->employee_id.'][start_date]" value="'.$start_date.'" />
				<input type="hidden" name="driver['.$row->employee_id.'][end_date]" value="'.$end_date.'" />
				</td>
				<td width="100">'.$row->first_name. ' ' . $row->last_name.'</td>';
				$name = $row->first_name . " " . $row->last_name;
				if ($row->driver_class_a == "yes") echo "<td width='200'>Class A Driver</td><td width='250'><span class='available'>AVAILABLE</span></td>";
				if ($row->driver_class_b == "yes") echo "<td width='200'>Class B Driver</td><td width='250'><span class='available'>AVAILABLE</span></td>";
				if ($row->driver_class_c == "yes") echo "<td width='200'>Class C Driver</td><td width='250'><span class='available'>AVAILABLE</span></td>";
		echo '</tr>';
	}
	if($row->driver_class_a == "" && $row->driver_class_b == "" && $row->driver_class_c == "") {	
		echo '<tr>
				<td width="25"><input type="checkbox" name="driver['.$row->employee_id.'][ticked]"/></td>
				<input type="hidden" name="driver['.$row->employee_id.'][first_name]" value="'.$row->first_name.'" />
				<input type="hidden" name="driver['.$row->employee_id.'][last_name]" value="'.$row->last_name.'" />
				<input type="hidden" name="driver['.$row->employee_id.'][start_date]" value="'.$start_date.'" />
				<input type="hidden" name="driver['.$row->employee_id.'][end_date]" value="'.$end_date.'" />
				</td>
				<td width="100">'.$row->first_name. ' ' . $row->last_name.'</td>
				<td width="200"><strong>Helper</strong></td><td width="250"><span class="available">AVAILABLE</span></td>';
		echo '</tr>';
	}
endwhile;

}
catch(PDOException $exca) {
   	var_dump($exca);
    trigger_error($exca->getMessage(), E_USER_ERROR);
}
?>
                                            
	</table>
		<br /><br />
		</fieldset>

	<fieldset>
		<span class="h3">DATES REQUESTED:: Assets</span><br /><span class="h4"><?php echo 'Start date:</span><span class="h5">'.$start.'</span><br /><span class="h4">End Date:</span><span class="h5">'.$end.'</span>'; ?>
				
		<table>			






<?php

try {
//create the query string
$sqlb = <<<EOT
SELECT  asset_id, asset_type, asset_name
FROM    assets a
WHERE   NOT EXISTS (
        SELECT  s.asset_id
        FROM    schedule s
        WHERE   (a.asset_id = s.asset_id)
		AND (s.start_date BETWEEN :start_date AND :end_date
		OR s.end_date BETWEEN :start_date AND :end_date)
        )
ORDER BY asset_type DESC
EOT;

//set the named parameters
$params = array(
	'start_date' => $start_date,
	'end_date' => $end_date
);

//prepare the query
$assets = $pdo->prepare($sqlb);

//execute the query
$assets->execute($params);

//loop through the results
while ($asset = $assets->fetch(PDO::FETCH_OBJ)):
	//now you can echo out your table rows	 
	echo '<tr>
			<td width="25"><input type="checkbox" name="asset['.$asset->asset_id.'][ticked]"/>
			<input type="hidden" name="asset['.$asset->asset_id.'][asset_name]" value="'.$asset->asset_name.'" />
			<input type="hidden" name="asset['.$asset->asset_id.'][asset_type]" value="'.$asset->asset_type.'" />
			<input type="hidden" name="asset['.$asset->asset_id.'][start_date]" value="'.$start_date.'" />
			<input type="hidden" name="asset['.$asset->asset_id.'][end_date]" value="'.$end_date.'" />
			</td>
			<td width="100">'.$asset->asset_name. '</td>
			<td width="200"><strong>' . $asset->asset_type.'</strong></td><td width="250"><span class="available">AVAILABLE</span></td>';
	echo '</tr>';
endwhile;

}
catch(PDOException $excb) {
   	var_dump($excb);
    trigger_error($excb->getMessage(), E_USER_ERROR);
}
 
 
?>

	</table>
	<table#workorder>
		<tr>
			<td><strong>Assign To Workorder::</strong></td>
			<td>
				<?php
					$workorder = new ShowWorkorders();
					$workorder->Workorders();
				?>
			</td>
		</tr>
	</table>
		<br /><br />
			<input type="submit" name="submitted" value="Click Here To Book Checked Employees And Assets:" /> 

		
		</fieldset>

Open in new window


Here is the function:
$pdo = new PDO("mysql:host=localhost;",........");
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
	
	$sql = "SELECT workorder_id, shippers_full_name
			FROM workorders
			WHERE active ='1'";
	// CURSOR_SCROLL ALLOWS REPOSITIONING THE CURSOR - LIKE DATA_SEEK() - BUT SADLY NOT WITH MYSQL
	$pdos = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

	//  START FIRST QUERY TRY GRAB EMPLOYEE ID FROM SCHEDULE			
	try {
    	$pdos->execute();
	
		$results = $pdos->fetchAll();
			if($results) {
		

		echo '<select><option>Chose Open Workoreder</option>';
			
	//loop through the results
				foreach ($results as $row) {
				$wid = $row['workorder_id'];
				$sfn = $row['shippers_full_name'];
				
				echo '<option name="workorder_id" id="workorder_id" value="'.$wid.'" />'.$sfn.'</option>';
				
			}
		echo '</select>';
		
		}
			
	}  //  END TRY			          	
	catch(PDOException $e) {
   		echo 'ERROR: ' . $e->getMessage();
	}

Open in new window

0
Comment
Question by:rgranlund
  • 3
  • 2
6 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
In the select control the name attribute goes into the select tag, and the value attribute goes into the option tag.  It looks like the script will generate HTML with the attributes in the wrong places.
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
Comment Utility
Ray's on the money on this one, but just to expand. When you create the <select> tag, you need the name attribute and when you create the options, you need the value attribute. Don't include an ID on the options - an ID has to be unique so don't include it in your loop

echo '<select name="workorder_id"><option value="">Chose Open Workoreder</option>';

echo '<option value="'.$wid.'" />'.$sfn.'</option>';

Open in new window

And please, for your safety (and my sanity) DO NOT INCLUDE USERNAMES AND PASSWORDS IN YOUR SCRIPTS! I've edited it out again, but you do need to be more careful :)
0
 
LVL 7

Author Closing Comment

by:rgranlund
Comment Utility
Ahh.. Something simple.  I figured.  I guess my eyes are tired.  Thanks for the heads up about the username, etc... I thought I had taken them out.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Author Comment

by:rgranlund
Comment Utility
Not sure yet.  I may have one last question
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Hard to tell without seeing the code for your action script. When you pull up the HTML page, have a look at the source and make sure it contains the <select> with the correct name and values and that it's inside your <form> element.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
OK. You just edited your previous comment!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now