We help IT Professionals succeed at work.

Use Select Query to Return Results as a Form

Ridgejp
Ridgejp asked
on
297 Views
Last Modified: 2017-03-17
Hi,

I've been using PHP/MySQL to pull back data in a table based format for sometime now - but how do I take the results and turn them into a form for viewing records on a cases by case basis?

Jason
Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer / Applications Consultant
CERTIFIED EXPERT

Commented:
but how do I take the results and turn them into a form for viewing records on a cases by case basis?
You need to write your own php page in which it got a form inside with its form elements, so that you able to re-populate the records back to your page.

hope this is clear.
CERTIFIED EXPERT

Commented:
Here is very simple example to populate values in form elements

<?php
$var_firstname = "Joan";//sample firstname value
$var_lastname  = "Mike";//sample lastname 	value
?>
<form>
  First name:<br>
  <input type="text" name="firstname" value="<?php echo $var_firstname; ?>"><br>
  Last name:<br>
  <input type="text" name="lastname" value="<?php echo $var_lastname; ?>">
</form>

Open in new window


Please give us more details on what is the data you have and sample of form you want to data to be populated.

Author

Commented:
Hi Peos,

My current query looks something like this for a table ...
<table id="table" class="table table-bordered table-striped table-hover">  
				<thead>
					<tr>
						<th class="success text-center">Part_ID</th>
						<th class="success">CAT_ID</th>
						<th class="success text-center">Qty</th>
						<th class="success text-center">Days.42</th>
						<th class="success text-center">Days.14</th>
						<th class="success text-center">+/-</th>
						<th class="success">Supplier</th>
						<th class="success text-center">SFP</th>
						<th class="success text-center">Cost</th>
						<th class="success text-center">Actions</th>        
					</tr>
				</thead>
				<tbody class="searchable">

	<?php

	$query = "select `partID`, `partDescription`, `qtyInStock`, `sfp`, round((sales42D/42),0) as sales42D, round((sales14D/14),0) as sales14D, round((sales14D/14)-(sales42D/42),0) as salesdiff,`supplierCompany`, `itemCost` FROM inventory";

	if (!$result = $mysqli->query($query)) {
		$err
		= "QUERY FAIL: "
		. $query
		. ' ERRNO: '
		. $mysqli->errno
		. ' ERROR: '
		. $mysqli->error
		;
		trigger_error($err, E_USER_ERROR);
		}

	while ($row = $result->fetch_object()) {
		$tr = <<<EOD
				<tr>
					<td class="text-center"> $row->partID </td>
					<td> $row->partDescription </td>  
					<td class="text-center"> $row->qtyInStock </td>
					<td class="text-center"> $row->sales42D </td>
					<td class="text-center"> $row->sales14D </td>
					<td class="text-center"> $row->salesdiff </td>
					<td> $row->supplierCompany </td>
					<td class="text-center"> $row->sfp</td>
					<td class="text-center"> £$row->itemCost </td>
					<td class="text-center"> 
						<div class="dropdown">   
							<button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">Options <span class="caret"></span></button>
							<ul class="dropdown-menu" role="menu">
							<li><a href="#">Edit</a></li>
							<li><a href="#">Delete</a></li>
						</div>
					</td>
				</tr> 
EOD;
		echo $tr;}

	?>
			</table>

Open in new window


So how do I incorporate the above into a similar format?

J
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Just what I'm looking for ... thanks very much!
CERTIFIED EXPERT

Commented:
You are welcome!

Author

Commented:
Hi,

I get the following error when I run the query ... any thoughts?

Notice: Undefined index: partID in /var/www/html/inventory_form.php on line 188

J

Author

Commented:
Along with this also ...

Fatal error: QUERY FAIL: select `partID`, `partDescription`, `qtyInStock`, `sfp`, round((sales42D/42),0) as sales42D, round((sales14D/14),0) as sales14D, round((sales14D/14)-(sales42D/42),0) as salesdiff,`supplierCompany`, `itemCost` FROM inventory WHERE partID= ERRNO: 1064 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 in /var/www/html/inventory_form.php on line 200

Author

Commented:
Scratch that ... sorted it ... spotted a lower case partid not partID in the URL which was throwing things ... J
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.