<label for="age">Age:</label>
    <input type="date" id="age" name="age" required/>
    <p>You should select one age  example09/04/1984.</p>
    <label for="sex">Sex:</label>
    <input type="radio" id="man" name="sex" value="man" required />
    <label for="man">Man</label>
    <input type="radio"  id="women" name="sex" value="women" required />
    <label for="women">Women</label>
    <p>You should select one sex example Man.</p>

it's not doing the insert in the database
echo $sex_validation; it`s not working

$sex_validation = preg_replace("/man|women/","",$sex);
echo $sex_validation;
$age_validation = preg_replace("/(19[0-9][0-9]|20[0-9][0-9]){4}-[01-12]{2}-[01-31]{2}/","", $age);
echo $age_validation;
print_r(date_parse_from_format("Y-n-j",$age_validation));
if(date_parse_from_format("Y-n-j",$age_validation)&& $sex_validation ){
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $link = mysqli_connect("localhost", "root", "P-11fl32fg14", "check-in");
    $query = "INSERT INTO check-in (age,sex) VALUES ('$age_validation ','$sex_validation ')";
    echo $query;
    mysqli_query($link, $query);
    mysqli_close($link);
}
elseif(!empty($age_validation) && !empty($sex_validation)){
    echo "All heads are mandatory";
}

Why are you replacing the values with an empty string in these lines?

$sex_validation = preg_replace("/man|women/","",$sex);
echo $sex_validation;
$age_validation = preg_replace("/(19[0-9][0-9]|20[0-9][0-9]){4}-[01-12]{2}-[01-31]{2}/","", $age);
echo $age_validation;

I'm guessing you want to use preg_match(), not preg_replace()?

Also, as was pointed out in another of your threads, that regex for the date is not going to work.

bertrc echo $sex_validation; it`s not working

It is doing what you wrote it to do. It is replacing 'man' or 'woman' with an empty string. Since an empty string is a false value, your logic test to build and run the sql query is false.

You have gone overboard with validation. You should not modify input data, e.g. the preg_replace() statements. You should only trim data (mainly so that you can detect if all white-space characters were entered), then validate it. If the data is valid, use it. If it is not valid, setup a message for the user telling them what was wrong with the data that they submitted.

If you store user/validation errors in an array, using the field name as the array index, it will simplify all your error logic. You can simply test at any point if the array holding the errors is empty() or not !empty().

Your code implies that you are copying every form field value to other variables. This is a waste of typing. Keep the form data as a set, in an array variable, then use elements in this array variable throughout the rest of the code. This will let you operate on the data using php array functions, such as trimming all the data at once with one line of code, and support more advanced programming, such as dynamically validating and dynamically processing the data.

date_parse_from_format() returns an array, regardless of any warnings or errors. You must specifically test the warning_count and error_count values in your code.

By typing an extra space on the end of each of the values being put into the sql query statement, you are modifying and corrupting the values. Why do you have those spaces there? As written in a reply in one of your previous threads, using prepared queries simplifies the sql query syntax, helping to prevent mistakes, and simplifies all the code, provided you switch to the much simpler PDO database extension.

pbismad provided you switch to the much simpler PDO database extension

Big 👍️ from me 🙂

    preg_match return 0 or 1 it doesn't return the string_
    I don't know pdo database extension

    The point of doing this is to validate the input data. To make sure that required data exists, and/or is a permitted value or format. Data submitted to your site can come from anywhere, not just your forms/links/cookies, can be set to anything, and cannot be trusted. You must validate all input data, on the server, before using it. This is the task you are trying to accomplish.

    Here is an outline of form processing/form code that will help clean up and simplify what you are doing -

    <?php
    
    // examine the input data
    //echo '<pre>'; print_r($_POST); echo '</pre>';
    
    $post = []; // array to hold a trimmed working copy of the form data
    $errors = []; // array to hold user/validation errors
    
    // define radio button choices
    $sex_choices = ['man','women'];
    
    // post method form processing
    if($_SERVER['REQUEST_METHOD'] === 'POST')
    {
    	// trim all the inputs at once
    	$post = array_map('trim',$_POST); // if any input is an array, use a recursive call-back function here instead of php's trim
    	
    // validate inputs
    // age (dob)
    if($post['age'] === '')
    {
    	$errors['age'] = "Age is required";
    }
    else
    {
    	$age_parts = explode('-',$post['age']);
    	if(!checkdate((int)$age_parts[1],(int)$age_parts[2],(int)$age_parts[0]))
    	{
    		$errors['age'] = "Age is invalid";
    	}
    }
    
    // sex
    if(!isset($post['sex']))
    {
    	$errors['sex'] = "Sex is required";
    }
    else
    {
    	if(!in_array($post['sex'],$sex_choices))
    	{
    		$errors['sex'] = "Sex is not a permitted choice";
    	}
    }
    
    // fake an error for demo purposes
    $errors['test'] = 'some error';
    
    // if no errors, use the form data
    if(empty($errors))
    {
    	// your code to build and execute the insert query would go here...
    	
    	// if this query could result in duplicate or out of range data, the error handling for the query would add an appropriate error message to the $errors array
    }
    
    // if no errors, success
    if(empty($errors))
    {
    	die(header("Refresh:0"));
    }
    }
    
    // html document - incomplete, only the necessary parts for the demo are shown
    ?>
    
    <?php
    // display any errors
    if(!empty($errors))
    {
    	echo '<p>'; echo implode('<br>',$errors); echo '</p>';
    }
    ?>
    
    <?php
    // display the form
    ?>
    <form method='post'>
    <label>Age:
    <input type="date" name="age" value="<?=htmlentities($post['age']??'',ENT_QUOTES)?>" required></label>
    <p>You should select one age example 09/04/1984.</p>
    Sex:
    <?php
    foreach($sex_choices as $choice)
    {
    	$chk = isset($post['sex']) && $post['sex'] === $choice ? 'checked' : '';
    	echo "<label><input type='radio' name='sex' $chk value='$choice' required>".ucfirst($choice)."</label>";
    }
    ?>
    <p>You should select one sex example Man.</p>
    <input type='submit'>
    </form>
    

      bertrc preg_match return 0 or 1 it doesn't return the string_

      You already have the string in the variable you supply to the preg functions. You just want to use preg_match() (or whatever other method makes sense) to do the validation. Then if all is validated okay, you can use those original variables in your DB queries. For instance, you don't really need to use preg-anything to see if a value is either "man" or "woman", you could just do:

      if($_POST['sex'] == 'man' || $_POST['sex'] == 'woman') {
          // it's good
      }
      // or...
      if(in_array($_POST['sex'], ['man', 'woman'])) {
          // it's good
      }
      
        Write a Reply...