Bibu
Bibu

Reputation: 201

update values from textarea in php

So..in a textarea I'm bringing the values stored in my table to modify them like this:

$l=mysql_query("select * from intrebari where cod_chestionar='".$_SESSION['cod']."'");

echo "<form method='POST' action='lalalalal.php'>";
echo "<textarea  name='edit' cols='50'>";
while($p=mysql_fetch_array($l))

{

   echo $p[denumire_intrebare];
   echo "\n";

}
echo "</textarea>";
echo "<input type='submit' value='Finished' name='save_edit'/>";
echo "</form>";
}

It's all fine..the values, that are questions, are brought on separate line. Now I want to make an update if necessary. So in my update file I'm doing like this:

$a=$_POST['edit'];

$a_array=explode("\n",$a);


     foreach($a_array as $b)

     {


$sql=mysql_query("UPDATE intrebari set denumire_intrebare='$b' WHERE cod_chestionar='".$_SESSION['cod']."'");
echo $b;
}

I want for all questions that have the same cod_chestionar to make the update. In this moment if I make an update the same value is put to all my questions. If I echo it the values are brought to me as I modified them but it doesn't make the update in table. Can you give an opinion because I just can't figure it out.

Upvotes: 0

Views: 6376

Answers (1)

Night2
Night2

Reputation: 1153

You are using one <textarea> for multi records, you have to use separate textareas to be able to modify them and update them separately.

You must have an unique ID field in your intrebari table and we will name it id, we will use it do solve your problem:

<?php

$l = mysql_query("select * from intrebari where cod_chestionar='".$_SESSION['cod']."'");

echo "<form method='POST' action='lalalalal.php'>";

while($p = mysql_fetch_array($l)){
    echo "<textarea  name='edit[".$p['id']."]' cols='50'>"; // Using `id` field here to make array of textareas
    echo $p[denumire_intrebare];
    echo "</textarea>";
}

echo "<input type='submit' value='Finished' name='save_edit'/>";
echo "</form>";

?>

And the update:

<?php

$a = $_POST['edit'];

foreach($a AS $id => $b){
    $b = mysql_real_escape_string($b);
    $id = mysql_real_escape_string($id);

    $sql = mysql_query("UPDATE intrebari set denumire_intrebare='$b' WHERE id='".$id."'"); // Using id field again
}

?>

Upvotes: 1

Related Questions