PHP form does not record

0

I have this Form html:

<form id="addRunner" name="addRunner" action="service.php" method="POST">
First Name: <input type="text" name="txtFirstName" id="txtFirstName" value="" placeholder="Firt Name"><br>
Last Name: <input type="text" name="txtLastName" id="txtLastName" value="" placeholder="Last Name"><br>
Gender: <select name="ddlGender" id="ddlGender">
         <option value="">--Please Select--</option>
         <option value="">Female</option>
         <option value="">Male</option>
</select><br>
Finish Time: <input type="text" name="txtMinutes" id="txtMinutes" value="" placeholder="(minutes)" maxlength="2">
<input type="text" name="txtSeconds" id="txtSeconds" size="10" maxlength="2" value="txtSeconds" placeholder="(seconds)">
<br><br>
<button type="submit" name="btnSave" id="btnSave">Add Runner</button>
<input type="hidden" name="action[]" id="action[]" value="addRunner">
</form>

This PHP code should validate and write on the basis of information submitted by Form, however nothing is written to the Base. Can anyone help me?

$a_html = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_SPECIAL_CHARS);

if ($a_html === 'addRunner') {

    $fname = htmlspecialchars(isset($_POST['txtFirstName']) ? $_POST['txtFirstName'] : 'txtFirstName');
    $lname = htmlspecialchars(isset($_POST['txtLastName']) ? $_POST['txtLastName'] : 'txtLastName');
    $gender = htmlspecialchars(isset($_POST['ddlGender']) ? $_POST['ddlGender'] : 'Valor Padrão');
    $minutes = htmlspecialchars(isset($_POST['txtMinutes']) ? $_POST['txtMinutes'] : 'ddlGender');
    $seconds = htmlspecialchars(isset($_POST['txtSeconds']) ? $_POST['txtSeconds'] : 'txtSeconds');
    $time = $minutes . ':' . $seconds;

    if (preg_match('/[^\w\s]/i', $fname) || preg_match('/[^\w\s]/i', $lname)) {
        echo 'Invalid name provided';
    }

    if (empty($gender)) {
        echo 'Gender select a please';
    }

    $string_sql = "INSERT INTO testemunho (first_name, last_name, gender, finish_time) VALUES (null,'{$fname}','{$lname}','{$gender}','{$time}')";

    $result = mysqli_query($conect, $string_sql);

    if ($result) {
        echo 'Runners: ', "$fname","$lname" . "Added Sucess";
    } else {
        echo 'Erro ao gravar dados';
    }
}
    
asked by anonymous 29.09.2018 / 20:51

2 answers

1
  • Brackets treat elements of the same name as an array that is not your case because you only have an input hidden with name="action[]" . Change this name to action without the brackets so that filter_input works correctly and the script goes into if ($a_html === 'addRunner') {

  • Option values are empty

     <option value="">Female</option>
     <option value="">Male</option>
    

    so it will always fall into this if

    if (empty($gender)) {
       echo 'Gender select a please';
    } 
    

    Although this does not cause errors, put values in the values type f and m for example.

  •   

    Once this is done, your INSERT should work.

        
    30.09.2018 / 01:11
    0

    You'll have to better debug your code to understand which part of the code is generating an unwanted output:

    Consider using the php function var_dump .

    In this link you have some tips: How to debug code in PHP?

    And look at the outputs of the variables: $result , $string_sql , $a_html

        
    29.09.2018 / 22:09