Script working with mysql and php into a textarea and back
Posted
by Tribalcomm
on Stack Overflow
See other posts from Stack Overflow
or by Tribalcomm
Published on 2010-04-04T05:09:44Z
Indexed on
2010/04/04
5:13 UTC
Read the original article
Hit count: 389
I am trying to write a custom script that will keep a list of strings in a textarea. Each line of the textarea will be a row from a table.
The problem I have is how to work the script to allow for adding, updating, or deleting rows based on a submit.
So, for instance, I currently have 3 rows in the database: john sue mark
I want to be able to delete sue and add richard and it will delete the row with sue and insert a row for richard.
My code so far is as follows:
To query the db and list it in the textarea: $basearray = mysql_query("SELECT name FROM mytable ORDER BY name");
<textarea name="names" cols=6 rows=12>');
<?php
foreach($basearray as $base){
echo $base->name."\n";
}
?>
</textarea>
After the submit, I have:
<?php
$namelist = $_REQUEST[names];
$newarray = explode("\n", $namelist);
foreach($newarray as $name) {
if (!in_array($name, $basearray)) {
mysql_query(DELETE FROM mytable WHERE word='$name'");
} elseif (in_array($name, $basearray)) {
;
} else {
mysql_query("INSERT INTO mytable (name) VALUES ("$name")");
}
}
?>
Please tell me what I am doing wrong. I am not getting any functions to work when I edit the contents of the textarea.
Thanks!
© Stack Overflow or respective owner