Script working with mysql and php into a textarea and back
- by Tribalcomm
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!