#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2

    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
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    pretty much anything is possible. But if you are talking about concrete code, we'll need to see it.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2
    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,cmarkings,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,$cheight,$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
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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>';

    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2
    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

IMN logo majestic logo threadwatch logo seochat tools logo