PHP/MYQL newbie needs help
Posted: Thu Feb 05, 2004 10:42 am
If I have a form with lots of fields, how can I write code that will check each one to make sure it is entered, and if not, print the error, and if so, run the query? Here's what I have:
if (empty($_POST['field1']) || empty($_POST['field2'])) {
$f1 = FALSE;
$f2 = FALSE;
$message .= '<p>You must complete every part of the form.</p>';
} else {
$f1= escape_data($_POST['field1']);
$f2 = escape_data($_POST['field2']);
}
if ($f1 && $f2) {
$query = "INSERT INTO tablename (field1,field2) VALUES ('$f1', '$f2')";
$result = @mysql_query ($query);
}
This is ok if I only have one or two fields, but if I have 30 or so, all the copy/pasting can get tedious. Is there a function that can find out what all the field names in my form are, set variables to them (I'm not sure if that part is even necessary), make sure they're all filled out, and put them into the table?
Thanks.
if (empty($_POST['field1']) || empty($_POST['field2'])) {
$f1 = FALSE;
$f2 = FALSE;
$message .= '<p>You must complete every part of the form.</p>';
} else {
$f1= escape_data($_POST['field1']);
$f2 = escape_data($_POST['field2']);
}
if ($f1 && $f2) {
$query = "INSERT INTO tablename (field1,field2) VALUES ('$f1', '$f2')";
$result = @mysql_query ($query);
}
This is ok if I only have one or two fields, but if I have 30 or so, all the copy/pasting can get tedious. Is there a function that can find out what all the field names in my form are, set variables to them (I'm not sure if that part is even necessary), make sure they're all filled out, and put them into the table?
Thanks.