Forum Moderators: coopster

Message Too Old, No Replies

Posting multi-line form to MySQL

Using Smarty templates

         

FleaPit

9:53 am on Feb 1, 2005 (gmt 0)

10+ Year Member



Hi guys, I'm not a programmer by any stretch but feel confident in messing about with code, cutting/pasting etc and have a basic idea and recognition of php functions. That said, I am trying to hack together a php function using smarty which will allow me to input a list of names into an existing sql table using a multi-line form.

$form=1;
if(count($_POST)){
$error = Add_Names();
if(empty($error))
$form=0;
}
$smarty->assign('Form', $form);
$smarty->display('admin_names_add.html');

function Add_Names(){
$q = "INSERT INTO sd_names(id,name) ".
" VALUES(NULL, '".mysql_escape_string($_POST['name'])."')";
// echo $q;
mysql_query($q);
}

OK, this works for entering all the names within the form but they all get inserted as one entry. How can I get each line within the form to be inserted as a separate entry with it's own auto-increment id?

Please be gentle as I really don't know what I'm doing but thought I may be able to hack this basic function together!

Zipper

10:09 am on Feb 1, 2005 (gmt 0)

10+ Year Member



try this.. didn't test it though

function Add_Names(){
$names = explode("\r\n", $_POST['name']);
foreach($names as $name){
$q = "INSERT INTO sd_names(id,name) ".
" VALUES('', '".mysql_escape_string($name)."')";
mysql_query($q);
}
}

FleaPit

10:37 am on Feb 1, 2005 (gmt 0)

10+ Year Member



That's great Zipper, works perfect :)