PHP插入数据库检查错误


PHP insert into database en check errors

嗨,我正在做一个学习php的小项目,我写了几个函数:

在这段代码中,我基本上检查了表单中的所有字段,并将哈希值从false更改为true,如果它们被填充,在最后循环哈希,并将键(这是错误)添加到数组中,如果它的值为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;}
    }

这里我将字段添加到DB中,它调用前面的函数来检查是否所有字段都已填写。

    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;
    }
}

这里我创建了我的表单,也有babysitterToevoegenAanDB()函数。这个和表单一起返回,可以在页面上调用。

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> ';
}

基本情况是:

  1. 我调用最后一个函数babysitteForm,它显示了应用
  2. 的表单
  3. BabysitterForm包含检查是否设置了submit的babySitterToevoegenAanDB函数
  4. 如果是,它会检查所有字段如果有错误它必须返回一个包含错误的数组

但是,如果我单击submit而不填写任何内容,则不会向errorArray添加任何内容。

没有添加到错误数组中是正常的。函数isset($_POST['anything'])将只检查该字段是否在post变量中设置。

post变量由表单填充。

基本上,如果您的表单包含7个输入字段,所有这些输入字段的值将被放入$ _POST。

$_POST变量的输出应该是这样的:

print_r($_POST);
结果:

array('element_1'=>'','element_2'=>'' ... 

所以你的isset函数将检查值'element_x'是否设置,它是,它只是不包含任何内容。

你需要重写你的支票,这样他们检查的是内容,而不是存在。我是唯一的如果不确定是否设置了e. $_GET变量,则在检查它们时使用,并且不要在表单中发送变量(因为这些总是被设置的)。

我建议您将支票重写为:

     $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;        
     }

对所有的检查都这样做。也反转你的foreach循环:

foreach($naamarray as $key => $value){
            if($value == true){
                array_push($errorArray,$key);
            }
     }

在您的表单之前返回函数,我也有一些疑问。我觉得这样更清楚要创建一个函数,只包含PHP页面,而不是这个。

则u将得到如下代码:

require_once('inc/functions.php');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
  babysitterToevoegenAanDB();
} else {
  babysitterForm();
}

最后一个技巧:给你的输入字段一个清晰的名字,而不仅仅是element_x ... .