Solved

PHP MySQL INSERT

Posted on 2013-10-24
6
728 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 110

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax 6 41
How to remove  "0":  in a json string 4 17
SQL query 7 18
Access 2010 Only Including Numbers After Certain Limit 3 8
This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

726 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