PHP insert into database en check errors
Hi I'm doing a small project for learning php, and I wrote a few functions :
In the this piece of code, I basically check all the fields in my form and change values in a hash from false to true if they are filled in, at the end I loop the hash, and add the key (which is the error) to the array if its value is false.
function testBabysitterForAllElements(){
global $db;
$errorArray;
$naamarray["naam"]=false;
$naamarray["voornaam"]=false;
$naamarray["adres"]=false;
$naamarray["woonplaats"]=false;
$naamarray["postcode"]=false;
$naamarray["telefoonnummer"]=false;
$naamarray["geboortedatum"]=false;
$naamarray["adres"]=false;
$naamarray["wachtwoord"]=false;
$naamarray["email"]=false;
if(isset($_POST['element_1'])){
$naamarray["naam"]=true;
}
if(isset($_POST['element_2'])){
$naamarray["voornaam"]=true;
}
if(isset($_POST['element_3'])){
$naamarray["adres"]=true;
}
if(isset($_POST['element_4'])){
$naamarray["woonplaats"]=true;
}
if(isset($_POST['element_5'])){
$naamarray["postcode"]=true;
}
if(isset($_POST['element_6'])){
$naamarray["telefoonnummer"]=true;
}
if(isset($_POST['element_7'])){
$naamarray["email"]=true;
connectToDB();
$sql='SELECT inlognaam FROM kauffman.login WHERE inlognaam like \''.$_POST['element_7'].'\';';
$rows=$db->queryRow($sql);
if($rows){
array_push($errorArray,"email adres is reeds geregistreerd.");
}
}
if(isset($_POST['element_8_1']) && isset($_POST['element_8_2']) && isset($_POST['element_8_3'])){
$naamarray["geboortedatum"]=true;
}
if(isset($_POST['element_10']) && isset($_POST['element_10_1'])){
$naamarray['wachtwoord']=true;
}
foreach($naamarray as $key => $value){
if($value == false){
array_push($errorArray,$key);
}
}
if(!empty($errorArray)){return $errorArray;}
}
Here I add the fields to the DB, it calls the previous function to check if all fields are filled in.
function babysitterToevoegenAanDB(){
global $db;
if(isset($_POST['submit'])){
$errorArray=testBabysitterForAllElements();
echo $errorArray;
$succes=('succesvol toegevoegd');
$result;
if(!empty($errorArray)){
connectToDB();
$sql = "INSERT into kauffman.login(inlognaam,functie,paswoord) VALUES ('".antiInjectie($_POST['element_7'])."','gezin','".pwHashGenerator(antiInjectie($_POST['element_10_1']))."');";
$sql2 = 'SELECT serialKey from kauffman.login WHERE inlognaam='.antiInjectie($_POST['element_7']).';' ;
$result= $succes;
}else{
$result= $errorArray;
}
echo $result;
}
}
Here I create my form that also has the babysitterToevoegenAanDB()
function. This and the form are returned and can be called on a page.
function babysitterForm(){
return babysitterToevoegenAanDB().' <form id="babysitForm" class="appnitro" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">
<div class="form_description">
<h2>Babysitter</h2>
<p>Gelieve je hier in te schrijven als babysitter</p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="element_1">Naam </label>
<div>
<input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value="'.
((isset($_POST['element_1']))? htmlentities($_POST['element_1']):'')
. '"/>
</div><p class="guidelines" id="guide_1"><small>Voer uw naam in.</small></p>
</li> <li id="li_2" >
<label class="description" for="element_2">Voornaam </label>
<div>
<input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_2']))? htmlentities($_POST['element_2']):'').'"/>
</div><p class="guidelines" id="guide_2"><small>Vul uw voornaam in.</small></p>
</li> <li id="li_3" >
<label class="description" for="element_3">Adres </label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_3']))? htmlentities($_POST['element_3']):'').'"/>
</div><p class="guidelines" id="guide_3"><small>vul uw straat en huisnummer in.</small></p>
</li> <li id="li_4" >
<label class="description" for="element_4">Woonplaats </label>
<div>
<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_4']))? htmlentities($_POST['element_4']):'').'"/>
</div><p class="guidelines" id="guide_4"><small>vul uw woonplaats in.</small></p>
</li> <li id="li_5" >
<label class="description" for="element_5">Postcode </label>
<div>
<input id="element_5" name="element_5" class="element text small" type="text" maxlength="255" value="'.((isset($_POST['element_5']))? htmlentities($_POST['element_5']):'').'"/>
</div><p class="guidelines" id="guide_5"><small>Vul uw Postcode hier in</small></p>
</li> <li id="li_6" >
<label class="description" for="element_6">telefoonnummer </label>
<div>
<input id="element_6" name="element_6" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_6']))? htmlentities($_POST['element_6']):'').'"/>
</div><p class="guidelines" id="guide_6"><small>Vul uw telefoonnummer in.</small></p>
</li> <li id="li_7" >
<label class="description" for="element_7">email </label>
<div>
<input id="element_7" name="element_7" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_7']))? htmlentities($_POST['element_7']):'').'"/>
</div><p class="guidelines" id="guide_7"><small>vul uw email in.</small></p>
</li> <li id="li_8" >
<label class="description" >Geboortedatum </label>
<span>
<input id="element_8_1" name="element_8_1" class="element text" size="2" maxlength="2" value="'.((isset($_POST['element_8_1']))? htmlentities($_POST['element_8_1']):'').'" type="text"> /
<label for="element_8_1">MM</label>
</span>
<span>
<input id="element_8_2" name="element_8_2" class="element text" size="2" maxlength="2" value="'.((isset($_POST['element_8_2']))? htmlentities($_POST['element_8_2']):'').'" type="text"> /
<label for="element_8_2">DD</label>
</span>
<span>
<input id="element_8_3" name="element_8_3" class="element text" size="4" maxlength="4" value="'.((isset($_POST['element_8_3']))? htmlentities($_POST['element_8_3']):'').'" type="text">
<label for="element_8_3">YYYY</label>
</span>
<li id="li_10" >
<label class="description" for="element_10">Wachtwoord </label>
<div>
<input id="element_10" name="element_10" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_10']))? htmlentities($_POST['element_10']):'').'"/>
<input id="element_10_1" name="element_10_1" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_10_1']))? htmlentities($_POST['element_10_1']):'').'"/>
</div><p class="guidelines" id="guide_10开发者_开发问答"><small>Vul uw wachtwoord twee maal in. Hierdoor voorkomt men typfouten door validatie.</small></p>
</li>
<li id="li_9" >
<label class="description" for="element_9">Opmerkingen </label>
<div>
<textarea id="element_9" name="element_9" class="element textarea medium" ></textarea>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="206335" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form> ';
}
What basically happens is :
- I call the last function babysitteForm which shows a form to apply
- BabysitterForm includes the babySitterToevoegenAanDB function that checks if submit is set
- If so it checks all the fields if there are errors it has to return an array with errors
However if I click submit without filling anyhting in, there is nothing added to the errorArray.
Its normal that nothing gets added to your error array. The function isset($_POST['anything']) will only check if that field is set in your post var.
The post var gets filled up by your form.
Basicly, if your form contains 7 input fields, all these input fields their value will be placed into $_POST.
A print of your $_POST var would be something like:
print_r($_POST);
result:
array('element_1'=>'','element_2'=>'' ...
so your isset function will check if the value 'element_x' is set, wich it is, it just contains nothing.
u need to re-write your checks so they check the content, not the existance. Isset is only of use when checking f.e. $_GET vars if u are not sure they are set and not post vars in a form (since these will always be set).
I would suggest re-writing your checks in the form of:
$naamarray["naam"]=true;
$naamarray["voornaam"]=true;
$naamarray["adres"]=true;
$naamarray["woonplaats"]=true;
$naamarray["postcode"]=true;
$naamarray["telefoonnummer"]=true;
$naamarray["geboortedatum"]=true;
$naamarray["adres"]=true;
$naamarray["wachtwoord"]=true;
$naamarray["email"]=true;
if($_POST['element_1'] == ""){
$naamarray["naam"]=false;
}
do this for all your checks. also reverse your foreach loop:
foreach($naamarray as $key => $value){
if($value == true){
array_push($errorArray,$key);
}
}
I also have some doubts with the returning of the function before your form. I think its clearer to create a function and just include the php page instead of this.
then u would get code like:
require_once('inc/functions.php');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
babysitterToevoegenAanDB();
} else {
babysitterForm();
}
And the final tip: give your input field clear names, not just element_x ... .
精彩评论