Solved

PHP MySQL INSERT

Posted on 2013-10-24
6
693 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
ID: 39598755
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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 39599193
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
ID: 39600305
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:rgranlund
ID: 39600334
Not sure yet.  I may have one last question
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39600367
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 43

Expert Comment

by:Chris Stanyon
ID: 39600370
OK. You just edited your previous comment!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

929 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

16 Experts available now in Live!

Get 1:1 Help Now