Hi guys,
Well im having a little trouble with arrays, im currently building a system but dont want to have to write out $var = 800000000 times over.
So my plan of action is to do a working method as follows
<?php
if($SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($POST['apply']))
{
$errors = array();
$postdata = array();
foreach($_POST as $key => $value)
{
$postdata[$key] = trim(strip_tags($value));
}
validate_post($postdata, $errors);
}
}
elseif(validate_post() == true)
{
insertdata($postdata, $table='my table');
}
else
{
$errors = '';
//
SHOW FORM + errors
if($errors > 0){foreach($errors as $error => $err){print($err);}}
}
FUNCTIONS
function validate_post($postdata, $errors = null)
{
foreach($postdata as $key => $value)
{
// validate
if(empty($value))
{
array_push($errors, 'No field must be empty!');
}
if($key == 'email')
{
if(preg_match('regex', $value)
{
array_push($errors, 'Email is in-correct')
}
}
return $errors;
}
return true;
}
function insertdata($postdata, $table, $errors = null)
{
foreach($postdata as $key => $value)
{
$query = 'INSERT INTO '.$table.'($key)VALUES('".mysqli_real_escape_string($value)."') ';
if($result->mysqli_query($mysqli, $query))
{
print('HOOOOORAY');
}
else
{
print('DAMN YOU!');
}
}
}
?>
As you can see im not great with arrays, so if anyone could look over my code?
Im not looking for someone to building the code for me but maybe a push in the right direction would be great guys.
Regards,