Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

PHP MySQL INSERT

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
rgranlund
Asked:
rgranlund
  • 3
  • 2
2 Solutions
 
Ray PaseurCommented:
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
 
Chris StanyonCommented:
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
 
rgranlundAuthor Commented:
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
rgranlundAuthor Commented:
Not sure yet.  I may have one last question
0
 
Chris StanyonCommented:
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
 
Chris StanyonCommented:
OK. You just edited your previous comment!
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now