2016-04-27 14 views
1

Ich versuche, Daten aus einem Formular in meine Datenbank einzufügen, aber es scheint nicht zu funktionieren. Ich habe nach der Einfügeabfrage ein Echo eingefügt, damit ich überprüfen kann, ob die Daten eingefügt wurden, aber nicht, was ich geschrieben habe. Gibt es ein Problem mit meiner Frage oder irgendeinem Teil meines PHP?Daten werden nicht in Datenbank eingefügt

Meine PHP:

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "stat_system"; 

$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $position = $exp = $ref = $batchtxt = $hiredate = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = ""; 
$error_flag = 0;     


if (isset($_POST['submit'])) { 

       $holdAttain = (isset($_POST['cmbAttain'])); 
       $holdPos = (isset($_POST['cmbPosition'])); 

       if (!empty($_POST['firstname'])) { 
        $fname = $_POST['firstname']; 
       } 

       if (!empty($_POST['lastname'])) { 
        $lname = $_POST['lastname']; 
       } 

       if (!empty($_POST['middlename'])) { 
        $mname = $_POST['middlename']; 
       } 

       if (!empty($_POST['contact'])) { 
        $contact = $_POST['contact']; 
       } 

       if (!empty($_POST['age'])) { 
        $age = $_POST['age']; 
       } 

       if (isset($_POST['cmbAttain'])) { 
        $attain = $_POST['cmbAttain']; 
       } 

       if(isset($_POST['school'])) { 
        $school = $_POST['school']; 
       } 

       if(isset($_POST['course'])) { 
        $course = $_POST['course']; 
       } 

       if (isset($_POST['exp'])) { 
        $exp = $_POST['exp']; 
       } 

       if (!empty($_POST['remarks'])) { 
        $remarks = $_POST['remarks']; 
       } 

       if (isset($_POST['nonbpo'])) { 
        $nonbpo = $_POST['nonbpo']; 
       } 

       if (isset($_POST['prevbpo'])) { 
        $prevbpo = $_POST['prevbpo']; 
       } 

       if (!empty($_POST['ref'])) { 
        $ref = $_POST['ref']; 
       } 

       if (isset($_POST['hiredate'])) { 
        $hiredate = $_POST['hiredate']; 
       } 

       if (isset($_POST['batchtxt'])) { 
        $batchtxt = $_POST['batchtxt']; 
       } 

       if (!empty($_POST['nho'])) { 
        $nho = $_POST['nho']; 
       } 

       if($error_flag == 0){ 
        $sql = mysqli_query($conn,"INSERT INTO applicants (appID, appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBPO, appPosition, appHireDate, appNHO, appReferrer) 
                VALUES (NULL, '$lname', '$fname', '$mname', $age, '$contact', $batchtxt, '$exp', $remarks, '$school', '$course', '$attain', '$nonbpo', '$position', $hiredate, $nho, '$ref')"); 
         echo "<script type='text/javascript'> 
         $(document).ready(function(){ 
         $('#succModal').modal('show'); 
         }); 
         </script>"; 
        $fname = $lname = $mname = $contact = $age = $attain = $course = $school = $batchtxt = $ref = $hiredate = $position = $exp = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = ""; 

       } 
       else { 
        print '<script type="text/javascript">'; 
        print 'alert("Please fill in all the fields!")'; 
        print '</script>'; 

       } 
      } 

    mysqli_close($conn); 
?> 

Mein HTML:

<div id="addApplicant" class="addApp-marginleft" style="height:1000px"> 

     <form id="registration" class="form-horizontal" method="post" action="index.php"> 
      <div class="row"> 
       <div align="center"> 
        <h3>Add Applicant</h3> 
        <br> 
       </div> 
      </div> 

      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>First name:</small></label> 
       <div class="col-sm-4"> 
        <input required type="text" name="firstname" autocomplete="off" placeholder="Firstname" id="firstname" class="form-control" value="<?php echo $fname;?>"> 
       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small> Last name:</small></label> 
       <div class="col-sm-4"> 
        <input required type="text" name="lastname" autocomplete="off" id="lastname" placeholder="Lastname" class="form-control" value="<?php echo $lname;?>"> 
       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Middle name:</small></label> 
       <div class="col-sm-4"> 
        <input required type="text" name="middlename" autocomplete="off" id="middlename" placeholder="middlename" class="form-control" value="<?php echo $mname;?>"> 
       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Contact number:</small></label> 
       <span id="errmsg"></span> 
       <div class="col-sm-4"> 
        <input required type="text" name="contactnum" autocomplete="off" onkeypress="return isNumber(event)" placeholder="Contact number" id="contact" class="form-control" maxlength="11" value="<?php echo $contact;?>"/> 
       </div> 
       <?php 
        echo '<script>'; 
        echo 'function isNumber(evt) {'; 
         echo 'evt = (evt) ? evt : window.event;'; 
         echo 'var charCode = (evt.which) ? evt.which : evt.keyCode;'; 
         echo 'if (charCode > 31 && (charCode < 48 || charCode > 57)) {'; 
          echo 'return false;'; 
         echo '}'; 
         echo 'return true;'; 
        echo '}'; 
        echo '</script>'; 
       ?> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Age:</small></label> 
       <div class="col-sm-4"> 
        <input required type="text" autocomplete="off" placeholder="age" name="age" id="age" class="form-control" value="<?php echo $age;?>"> 
       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Graduate:</small></label> 
       <div class="col-sm-4"> 
        <select required name="cmbAttain" id="cmbAttain" class="form-control" onChange="disableCmb();"> 
         <option value="">Choose</option> 
         <option value="Yes" <?php if($holdAttain == "Yes") echo "selected"; ?>>Yes</option> 
         <option value="No" <?php if($holdAttain == "No") echo "selected"; ?>>No</option> 
        </select> 

       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>School:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="school" autocomplete="off" id="school" placeholder="School" class="form-control" value="<?php echo $school;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Course:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="course" autocomplete="off" id="course" placeholder="Course" class="form-control" value="<?php echo $course;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Batch:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="batchtxt" autocomplete="off" id="batchtxt" placeholder="Batch" class="form-control" value="<?php echo $batchtxt;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Experience:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="exp" autocomplete="off" id="exp" placeholder="Experience" class="form-control" value="<?php echo $exp;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Previous BPO:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="prevbpo" autocomplete="off" id="prevbpo" placeholder="Previous BPO" class="form-control" value="<?php echo $prevbpo;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Non-BPO:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="nonbpo" autocomplete="off" id="nonbpo" placeholder="Non-BPO" class="form-control" value="<?php echo $nonbpo;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Remarks:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="remarks" autocomplete="off" id="remarks" placeholder="Remarks" class="form-control" value="<?php echo $remarks;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Hire Date:</small></label> 
       <div class="col-sm-4"> 
        <input type="date" name="hiredate" id="hiredate" class="form-control" autocomplete="off" value="<?php echo $hiredate;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Position:</small></label> 
       <div class="col-sm-4"> 
        <select required name="cmbPosition" id="cmbPosition" class="form-control" data-size="5" > 
         <option selected value="">Choose</option> 
         <option value="Customer Service Representative" <?php if($holdPos == "Customer Service Representative") echo "selected"; ?>>Customer Service Representative</option> 
         <option value="Image Enhancer" <?php if($holdPos == "Image Enhancer") echo "selected"; ?>>Image Enhancer</option> 
        </select> 
       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>NHO:</small></label> 
       <div class="col-sm-4"> 
        <input type="date" name="nho" id="nho" class="form-control" autocomplete="off" value="<?php echo $nho;?>"> 
        </div> 
      </div> 
      <div class="form-group" align="center" > 
       <label class="col-sm-4 control-label"><small>Referrer:</small></label> 
       <div class="col-sm-4"> 
        <input type="text" name="ref" autocomplete="off" id="ref" placeholder="Name of Referrer" class="form-control" value="<?php echo $ref;?>"> 
       </div> 
      </div> 
      <div class="form-group" align="center" > 
       <div class="col-sm-4"> 
       </div> 
       <div class="col-sm-4"> 
        <div class="btn-group " role="group" aria-label="..."> 
         <input id="submitbtn" type="submit" name="submit" class="btn btn btn-success" value="Submit" data-target="#succModal"> 
         <input type="reset" name="reset" class="btn btn-warning" value="Clear"> 
        </div> 
       </div> 
       <div class="col-sm-4"> 
       </div> 
      </div> 
     </form> 
    </div> 
    <div class="container"> 
    <!-- Register Success Modal --> 
     <div class="modal fade" id="succModal" role="dialog"> 
      <div class="modal-dialog modal-sm"> 
       <div class="modal-content"> 

        <div class="modal-body" align="center"> 

         <p>REGISTRATION SUCCESSFUL</p> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
+0

Schauen Sie (und zeigen Sie uns) für die richtige Fehlermeldung, oder fügen Sie Debugging-Informationen entlang Ihres Codes hinzu. –

+0

@ J.Chomel Es zeigt mir keine Fehlermeldung, nur die Daten werden nicht in die Datenbank eingefügt. Ist es in Ordnung, $ .post() zu haben, das mehrere Daten übergibt? Ich denke, das Hauptproblem ist nur da oder mein PHP. –

+0

echo '$ sql' und führen Sie die Abfrage direkt in mysql.Es zeigt Ihnen den Fehler –

Antwort

0

Verwendung $ _POST statt $ _REQUEST ..

wie:

$lname = $_POST['lname']; 
+0

Es funktioniert immer noch nicht:/ –

0

Gerade auf einen Blick , Sie sind NULL-Tests sollten durchgetrennt werden, nicht ||. So wie Sie es haben, überprüft es nur, ob einer (irgendeine) von ihnen einen Wert enthält.

0

Es gibt einige Fehler mit Ihnen ist Abfrage, ersetzen Sie einfach mit

$sql = "INSERT INTO applicants (appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBpo, appPosition, appHireDate, appNHO, appReferrer) VALUES ('$lname','$fname','$mname',$age,'$con',$batch,'$exp','$rem','$prevbpo','$school','$course','$gradsit', '$nbpo', '$pos', '$hdate', '$nho', '$ref')"; 

hier gibt es keine Notwendigkeit, hinzuzufügen appid wie es auto-inkrementiert ist. Außerdem fehlen einige einfache Anführungszeichen.

+0

appHireDate Typ ist Datum, sollte ich noch '$ hdate' setzen? –

0
  • überspringen Wert senden NULL in Ihrer insert Anweisung
  • wo Sie prüfen, ob $ _POST leer ist, wenn es leere Menge $ ERROR_FLAG ist = 1
  • Innen if (isset($_POST['submit'])) { print $ _POST

Hoffe, das hilft beim Debuggen. Wenn das Problem immer noch auftritt, überprüfen Sie Ihr Fehlerprotokoll

Verwandte Themen