The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Pre-fetching data for editing
Discuss Pre-fetching data for editing in the PHP Development forum on Dev Shed. Pre-fetching data for editing PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 15th, 2013, 12:18 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 56
Time spent in forums: 13 h 10 m 2 sec
Reputation Power: 1
|
|
|
PHP-General - Pre-fetching data for editing
Hi,
My update form only fetches data for text boxes and not for select combo boxes and texarea boxes.
Is there a way I can pre-fecth data for the two controls as well?
Joseph
|

February 15th, 2013, 03:21 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
pretty much anything is possible. But if you are talking about concrete code, we'll need to see it.
|

February 15th, 2013, 04:10 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 56
Time spent in forums: 13 h 10 m 2 sec
Reputation Power: 1
|
|
Ok, here is the code for my update form.
PHP Code:
<?php
$mysqli = new mysqli("localhost", "joseph", "", "collectionsdb");
// get value of object id that sent from address bar
//if(!isset($_GET['c_id']))
$c_id = $_GET['c_id'];
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// get value of object id that was sent from address bar
/* Create the prepared statement */
if ($stmt = $mysqli->prepare("SELECT c_id,ctitle,csubject,creference,cyear,cobjecttype,cmaterial,ctechnic,cwidth,cheight,cperiod,cmarking s,cdescription,csource,cartist,cfilename FROM collections WHERE c_id='$c_id'")) {
/* Execute the prepared Statement */
$stmt->execute();
/* Bind results to variables */
$stmt->bind_result($c_id,$ctitle,$csubject,$creference,$cyear,$cobjecttype,$cmaterial,$ctechnic,$cwidth,$ch eight,$cperiod,$cmarkings,$cdescription,$csource,$cartist,$cfilename);
/* fetch values */
while ($rows = $stmt->fetch()) {
// display records in a table
// image table
?>
<table border="1" align="left">
<tr>
<td rowspan=16 valign=top> <?php echo '<img src="./images/'.$cfilename.'" width="300" height="400" />'; ?> </td>
</tr>
<tr><td>ID</td><td><?php echo $c_id; ?></td></tr>
<tr><td>TITLE</td><td width="100"><input name="ctitle" type="text" id="ctitle" value="<?php echo $ctitle; ?>" size="30"></td></tr>
<tr><td>SUBJECT</td><td width="100"><input name="csubject" type="text" id="csubject" value="<?php echo $csubject; ?>" size="30"></td></tr>
<tr><td>REFERENCE No.</td><td width="100"><input name="creference" type="text" id="creference" value="<?php echo $creference; ?>" size="30"></td></tr>
<tr><td>YEAR</td><td width="100"><input name="cyear" type="text" id="cyear" value="<?php echo $cyear; ?>" size="5"></td></tr>
<tr><td>OBJECT TYPE</td>
<td>
<select name="cobjecttype" id="cobjecttype" tabindex="">
<option value="">---Select object type---</option>
<option value="ceramic">Ceramic</option>
<option value="clock">Clock</option>
<option value="gold">Gold and silverware</option>
<option value="mask">Mask</option>
<option value="painting">Painting</option>
<option value="sculpture">Sculpture</option>
<option value="tapestry">Tapestry</option>
</select>
</td></tr>
<tr><td>MATERIAL USED</td>
<td>
<select name="cmaterial" id="cmaterial" tabindex="" >
<option value="">---Select Material---</option>
<option value="brass">Brass</option>
<option value="oil">Oil</option>
<option value="wood">Wood</option>
<option value="carved">Canvas/Cotton/Fabric/Linen/Wool</option>
</select>
</td></tr>
<tr><td>TECHNIC</td>
<td>
<select name="ctechnic" id="ctechnic" tabindex="7" >
<option value="">---Select Technic---</option>
<option value="cast">Cast</option>
<option value="carved">Carved</option>
<option value="etched">Etched</option>
</select>
</td>
<tr><td>WIDTH</td><td width="100"><input name="cwidth" type="text" id="cwidth" value="<?php echo $cwidth; ?>" size="10"></td></tr>
<tr><td>HEIGHT</td><td width="100"><input name="cheight" type="text" id="cheight" value="<?php echo $cheight; ?>" size="10"></td></tr>
<tr><td>PERIOD</td><td width="100"><input name="cperiod" type="text" id="cperiod" value="<?php echo $cperiod; ?>" size="30"></td></tr>
<tr><td>MARKINGS</td><td width="100"><input name="cmarkings" type="text" id="cmarkings" value="<?php echo $cmarkings; ?>" size="30"></td></tr>
<tr><td>DESCRIPTION</td><td width="400"><textarea name="cdescription" rows="2" cols="50" id="cdescription" value="<?php echo $cdescription; ?>"></textarea></td></tr>
<tr><td>SOURCE</td><td width="100"><input name="csource" type="text" id="csource" value="<?php echo $csource; ?>" size="30"></td></tr>
<tr><td>ARTIST</td><td width="100"><input name="cartist" type="text" id="cartist" value="<?php echo $cartist; ?>" size="30"></td></tr>
</table>
<table>
<?php
echo "<td width=\"70\"><a href=\"details.php?c_id=$c_id\">Cancel</a></td>";
?>
</table>
<?php
}
/* Close the statement */
$stmt->close();
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
/* close our connection */
$mysqli->close();
?>
Joseph
|

February 15th, 2013, 04:46 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
I see you still haven't secured you code. In that case I'll assume you're doing it for a dare or out of some kind of masochism ...
As to the default values: An option can be preselected with the selected attribute. So for each option you have to check if the value matches the value in $_POST, and if it does, you add the selected attribute. You can also do this in a loop to avoid repeating the same code over and over again.
PHP Code:
$options = array(
'Ceramic' => 'ceramic'
, 'Clock' => 'clock'
, 'Gold and silverware' => 'gold'
...
);
foreach ($options as $description => $value) {
$selected =
$value == $_POST['(whatever)'] ? 'selected' : '';
echo '<option value="' . html_escape($value) . '" ' . $selected . '>' . html_escape($description) . '</option>';
}
|

February 15th, 2013, 07:50 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 56
Time spent in forums: 13 h 10 m 2 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Jacques1 I see you still haven't secured you code. In that case I'll assume you're doing it for a dare or out of some kind of masochism ...
As to the default values: An option can be preselected with the selected attribute. So for each option you have to check if the value matches the value in $_POST, and if it does, you add the selected attribute. You can also do this in a loop to avoid repeating the same code over and over again.
PHP Code:
$options = array(
'Ceramic' => 'ceramic'
, 'Clock' => 'clock'
, 'Gold and silverware' => 'gold'
...
);
foreach ($options as $description => $value) {
$selected =
$value == $_POST['(whatever)'] ? 'selected' : '';
echo '<option value="' . html_escape($value) . '" ' . $selected . '>' . html_escape($description) . '</option>';
}
|
Thanx alot once again for your help.
joseph
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|