1

I make edit.php with show all data in form from MySQL.
All data is show on form rightly, but it's not work on dropdown and textarea.

I need help and this is my code

<form method="post" action="editdata.php">
<?php 
  include 'config.php';
  $id = $_GET['id'];
  $sqlTampil = "select * from data_korban Where kasus_id=$id"; 
  $qryTampil = mysql_query($sqlTampil); 
  $dataTampil = mysql_fetch_array($qryTampil); 
?>  

Dropdown value is still default, not selected value and TextArea is blank

<select name="agama" id="agama" value="<?php    echo $rows -> agama;?>">
    <option value="Islam">Islam</option>
    <option value="Khatolik">Khatolik</option>
    <option value="Protestan">Protestan</option>
    <option value="Hindu">Hindu</option>
    <option value="Buddha">Buddha</option>
    <option value="Lain-Lain">Lain-Lain</option>
</select>

<textarea id="alamatkorban" rows="5" name="alamatkorban" 
          value="<?php echo $rows -> alamatkorban;?>" 
          cols="33">
</textarea>

Thank You for Your Help

George Kagan
  • 5,913
  • 8
  • 46
  • 50
user3466927
  • 11
  • 1
  • 1
  • 2
  • Dispite the sql injection vulnerability, where does $rows come from? – Royal Bg Mar 28 '14 at 00:18
  • Also, textarea does not have value attribute – Royal Bg Mar 28 '14 at 00:19
  • $rows is for show column 'agama' – user3466927 Mar 28 '14 at 00:20
  • Please pleasee user parameterized queries, prepared statements and escape user input, the code you have here has a BIG security vulnerability as @RoyalBg said to have a little more info look at: [OWASP SQL Injection Prevention Sheet](https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet), [Prevent SQL Injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php), [OWASP Query parameterization Cheat Sheet](https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet), [mysql_real_scape_string PHP](http://it1.php.net/mysql_real_escape_string) – Samuel Lopez Mar 28 '14 at 07:49

6 Answers6

7

Your biggest issue is you are accessing your database values incorrectly. mysql_fetch_array() does not return an object. It returns an array. So you use array syntax ($rows['key']) not object syntax ($rows->key).

Just check to see if the option value matches the value of $rows['agama']. If so, add the selected attribute.

<select name="agama" id="agama">
    <option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
    <option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
    <option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
    <option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
    <option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
    <option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>

An even better way would be to put all of your options in an array and loop through them to generate your options. Then you can check their values as you loop through them. This would be less code an easier to maintain.

<select name="agama" id="agama">
<?php
$agamas = array('Islam', 'Khatolik', 'Protestan', 'Hindu', 'Buddha', 'Lain-Lain');
foreach ($agamas as $agama) {
      $selected = ($rows['agama'] === $agama) ? ' selected="selected"' : '';
?>
    <option value="Islam"<?php echo $selected; ?>>Islam</option>
<?php
}
?>
</select>

To fix your textarea issue, <textarea> does not have a value attribute. You need to place the content in between the <textarea></textarea> tags:

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php echo $rows['alamatkorban'] ;?></textarea>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • thank you for your help, but the result is: value for the dropdown still default. I have selected Hindu for Agama, when I try to edit, all form is work correctly, dropdown is show default value and textarea is blank ... – user3466927 Mar 28 '14 at 00:37
  • If you do ` what do you see? – John Conde Mar 28 '14 at 01:02
2

Okay let us assume that there is a variable that holds the selected value and we name it $selected and the options for our select will be stored in $options.

$selected = "Buddha" ;
$options  = array('Islam', 'Khatolik', 'Protestan', 'Hindu', 'Buddha', 'Lain-lain');

In your edit.php file you should try creating the select element via php echo

<?php
    foreach($options as $option){
        if($selected == $option){
            echo "<option selected='selected' value='$option'>$option</option>" ;
        }else{
            echo "<option value='$option'>$option</option>" ;
        }
    }
?>
YouSer
  • 393
  • 3
  • 12
0

You had your textarea declared value wrong. There's no value tag for textarea. What you need to put to your textarea is like this:

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php echo $rows -> alamatkorban; ?></textarea>
0

the option that you want selected needs to have the "selected" property in the option tag.

Islam.

The content of the textarea should exist inside of the open/close e.g.

<textarea id="alamatkorban" rows="5" name="alamatkorban" cols="33"><?php    echo $rows -> alamatkorban;?></textarea>
James Palawaga
  • 288
  • 2
  • 9
0

Looks like you've got a couple problems: As mentioned, <textarea> tags don't use the value property, but rather you change the inner HTML, so you have:

    <textarea>Text inside the text area is written here, like <?php echo $var; ?></textarea>

Your other problem is because you need a 'Selected' option inside the tag for the option you want selected by default. So:

    <select id="selector">
<?php
$optionArray=array("Option 1","Option 2","Option 3");
foreach ($optionArray as $option){?>
    <option id="<?= $option? >"<? if ($rows[$option]==$option){ echo " selected"; } ?>><?= $option ?></option>
<?}?>

Should do it - that way, you can keep all of your options in an array that is simply looped through. The <?= ($var) ?> tags are php short tags equivalent to <?php echo ($var); ?> to keep things a little shorter.

Helpful
  • 702
  • 4
  • 16
0

You may try this...textarea should exist inside of the open/close e.g.

<textarea rows="5" cols="33" id="alamatkorban" name="alamatkorban" autofocus autocomplete="off"><?php echo @$row["alamatkorban"]; ?></textarea>
Tapash
  • 27
  • 3
  • 10