PHP is not sending data to MySQL anymore after 2 successful POSTs


#1

I don’t understand whats going on… I spent the last 4 hours trying to find out what the heck is wrong.

I created a job form where the users can apply for job positions though the website. I did 2 tests and they were a success. after that nothing wants to be saved in the DB.

I have a high doubt that the code responsible for not sending the data is the date()…please help me out.

CreateForm.php:

<?php
include('connection.php');

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

    $fullName = $_POST['fullName'];
    $email = $_POST['mobile'];
    $mobile = $_POST['email'];
    $dob = $_POST['dob'];
    $degree = $_POST['degree'];
    $expYears = $_POST['expYears'];
    $position = $_POST['position'];
    $whyHire = $_POST['whyHire'];
    $uploadCV = $_POST['uploadCV'];
    $dateApplied = $_POST['dateApplied'];


    $db = new Database();
    $db->connect();
    $db->insert('users',array('fullName'=>$fullName,'email'=>$email, 'mobile'=>$mobile,
                'dob'=>$dob, 'degree'=>$degree, 'expYears'=>$expYears, 'position'=>$position,
                'whyHire'=>$whyHire, 'uploadCV'=>$uploadCV, 'dateApplied'=>$dateApplied));  // Table name, column names and respective values
    $res = $db->getResult();
    print_r($res);

    if($res) {
        header("Location: http://localhost/Rocket/includes/thankYou.php");
        die();
    } else {
        echo "query error";
    }
}
?>

jobForm.php

<!DOCTYPE html>
<html>
<head>
    <title> Job Application Form </title>
</head>

<body>

<form action="../server/create-form.php" method="POST" name="addForm">
    <input type="text" name="fullName" placeholder="Full Name" />
    <input type="email" name="email" placeholder="E-mail Address" />
    <input type="number" name="mobile" placeholder="Mobile Number" />
    <!-- Birth Day -->
   <input type="date" name="dob" />
    <!-- Academic Degree -->
    <div>
        <select name="degree">
            <option value="High school">High School</option>
            <option value="Diploma">Diploma</option>
            <option value="Bachelor">Bachelor</option>
            <option value="Masters">Masters</option>
            <option value="PHD">PH.D</option>
        </select>
    </div>
    <!-- Years of EXP -->
    <div>
        <select name="expYears">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
            <option value="15">15</option>
            <option value="16">16</option>
            <option value="17">17</option>
            <option value="18">18</option>
            <option value="19">19</option>
            <option value="20">20</option>
        </select>
    </div>
    <!-- job position -->
    <div>
        <select name="jobPosition">
            <option name="jobPosition" selected ><?php echo $_GET['jobTitle']; ?></option>
            <option name="Web Developer" value="Web Developer">Web Developer</option>
            <option name="Web Designer" value="Web Designer">Web Designer</option>
            <option name="Graphic Designer" value="Graphic Designer">Graphic Designer</option>
            <option name="Data Scientist" value="Data Scientist">Data Scientist</option>
            <option name="Business Manager" value="Business Manager">Business Manager</option>
        </select>
    </div>
    <!-- why hire? -->
    <div>
        <textarea placeholder="Why should we hire you?"></textarea>
    </div>
    <!-- Upload CV -->
    <div>
        <input type="file" name="uploadCV"/>
    </div>
    <!-- date applied -->
    <input type="hidden" value="<?php echo date('Y-m-d')?>" name="dateApplied" />
    <!-- Submit Form -->
    <input type="submit" name="addForm" value="Apply for this position"/>
</form>

</body>
</html>

#2

Are you hitting Enter to submit your form? IE in particular used to have an issue with this, specifically not posting the submit button’s value if you used anything other than the button to submit the form. I would probably trigger off of ‘dateApplied’ instead, since it should always be set as a hidden field.

Also your ‘why hire’ textarea doesn’t have it’s name attribute set.

Beyond that i’m not sure why it would specifically start failing after a certain number of submissions…


#3

Alright, I’ll do these!

and I don’t use IE. never considered it :joy:


#4

Please check the PHP Online User Manual regarding the correct usage of header(…)

  $res = $db->getResult();

  // FOLLOWNG LINE IS NOT ALLOWED BEFORE CALLING header(...)
  print_r($res);

    if($res) {
        header("Location: http://localhost/Rocket/includes/thankYou.php");
        die();
    } else {
        echo "query error";
    }


Also PHP has excellent error checking and reporting which can be set by using the following:
<?php
declare(strict_types=1);
// best removed if online
error_reporting(-1); 
ini_set('display_errors', 'true');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// your script


#5

Could you also post the contents of connection.php please? The error could be in there as well.


#6

I added your code and I cot this error after submitting the form:

Notice : Undefined index: position in C:\xampp\htdocs\Rocket\server\create-form.php on line 19

Fatal error : Uncaught mysqli_sql_exception: Duplicate entry ‘0’ for key ‘mobile’ in C:\xampp\htdocs\Rocket\server\connection.php:149 Stack trace: #0 C:\xampp\htdocs\Rocket\server\connection.php(149): mysqli->query(‘INSERT INTO `us…’) #1 C:\xampp\htdocs\Rocket\server\create-form.php(29): Database->insert(‘users’, Array) #2 {main} thrown in C:\xampp\htdocs\Rocket\server\connection.php on line 149


#7

Are those two lines correct?

As far as the errors showing is concerned it looks like there is either a unique index on the email or mobile field. Or could be zero is not acceptable.

Hopefully phpmyadmin is installed so that the table can be examined and before trying to update the table check the values to ensure they are correct.


#8

I JUST noticed that :joy::joy::joy::joy::joy:

Oh dear. It worked. Thank you :joy: