views:

194

answers:

1

hi. with this code:

    <?
if (isset($_POST['onay'])) {
foreach  ($_POST['secilen'] as $zuha) {
$olay = mysql_query("update mp3 SET aktif = '1' WHERE id = '$zuha'");
}
if ($olay) {
    echo "islem tamam";
    exit;
}
}
?>

and with this form: (select * from mp3 aktif = '0')

<form name="form" method="post">
<input readonly type="text" name="id" value="<?=$haciosman['id']?>" />
<input type="text" name="baslik" value="<?=$haciosman['baslik']?>
<input type="checkbox" name="secilen[]" value="<?=$haciosman['id']?>">
<input type="submit" name="onay" value="Onayla" />

I can set "aktif to 1" for each row that i checked. but i want to update "baslik" too! how can i update each "baslik" with that php code?

+1  A: 

If I understand you correctly you want to do this:

mysql_query("UPDATE mp3 
             SET 
                 aktif = '1', 
                 baslik = '" . mysql_escape_string($_POST['baslik']) ."' 
             WHERE id = '$zuha'");

But this is similar to what you already have in your original question:

mysql_query("update mp3 set aktif = '1', 
                              baslik = '$_POST[baslik]' 
               where id = '$_POST[id]'")

The only difference now is that you make the query in a loop and use a different ID variable.

My comments on your code in your original question stay the same. ( ;) )

P.S.: Introduced mysql_escape_string() for a little security.

UPDATE: Ok I think I got your problem now. You basically want to create an input field collection for every entry in your database, something like this:

<form name="form" method="post">
<table>

<?php while($haciosman = ($query)):?>
<tr>
   <td><input readonly type="text" name="id" value="<?=$haciosman['id']?>" /></td>
   <td><input type="text" name="baslik[<?php echo $haciosman['id'] ?>]" value="<?=$haciosman['baslik']?> </td>
   <td><input type="checkbox" name="secilen[]" value="<?=$haciosman['id']?>">

</tr>
<?php endwhile; ?> 

</table>
<input type="submit" name="onay" value="Onayla" />
</form>

You just define another the names of the text fields as arrays baslik[$haciosman['id']], similar as you did for the checkboxes but this time you specify the array key which is the ID of the record.

Then you PHP code you can access this array ($_POST['baslik'] contains an array now) like this:

if (isset($_POST['onay'])) {
  foreach  ($_POST['secilen'] as $zuha) {
    $olay = mysql_query("UPDATE mp3 
                         SET 
                            aktif = '1', 
                            baslik = '" . mysql_escape_string($_POST['baslik'][$zuha]) ."' 
                         WHERE id = '$zuha'");
  }
}

UPDATE 2:

You should really read the official short introduction to forms, follow the links in this article and in general read the official documentation to get more insight in how to work with PHP.

Felix Kling
yes but this is not updating each row. its only updating one row. ive 3 rows. when i use this query baslik is duplicating. lets change words! "baslik" is "NAME" ok? I have 3 rows! I want to edit 3 row's names. when i click submit 3 rows gets same name
Ronnie Chester Lynwood
@Ronnie Chester Lynwood: Of course they do because you only have one NAME input field. If you want to update every three names you have to provide three name input fields, similar to the select boxes.
Felix Kling
<?while ($haciosman = mysql_fetch_array($veliaht)) {?> <tr> <td width="42" height="31"><input type="text" name="id" value="<?=$haciosman['id']?>" /></td> <td width="271"><label> <input type="text" name="baslik" value="<?=$haciosman['baslik']?>" /> </label></td> <td width="168"><?=$haciosman['sarkisozu']?></td> <td width="171"><?=$haciosman['ekleyen']?></td> <td width="170"><input type="checkbox" name="secilen[]" value="<?=$haciosman['id']?>"> <? } ?> <input type="submit" name="onay" value="Onayla" />im using this
Ronnie Chester Lynwood
@Ronnie Chester Lynwood: I created somehting similar. Have a look at my updated answer and compare.
Felix Kling
I tried your code. It set aktif to 1 but it set baslik null
Ronnie Chester Lynwood
FINALLY! <td><input type="text" name="baslik[<?=$haciosman['id']?>]" value="<?=$haciosman['baslik']?>" /> </td>with this it worked out! MAAAANY MANY MANY MANY MANY THANKS FOR YOUR HELP!! I promise I will read as much as I can. thaank you!!
Ronnie Chester Lynwood
@Ronnie Chester Lynwood: Ah yes, I forgot to echo the ID value! God catch! :) I am glad that it works now :) If it helped you, you should mark the answer as accepted then.
Felix Kling
@felix: marked :)
Ronnie Chester Lynwood