vendredi 24 septembre 2021

MySql Entry Shows NULL

When you tick the optional checkbox to subscribe to my newsletter on my html form and when you submit the form, my Mysql Database entry "newsletter_subscribe_yes" column shows NULL entry. Why ?

Particular Form Input

<label for="newsletter_subcribe_yes">Newsletter Subcribe:Yes</label>
<input type="checkbox" name="newsletter_subcribe_yes" id="newsletter_subcribe_yes" value="Newsletter Subcribe:Yes">
<br>

Full Form Submission Script

<?php
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting(E_ALL);
?>

<form method="POST" name="textfield" id="textfield" action="" required>
<label for="domain">Domain:</label><br>
<input type="text" name="domain" id="website_domain" maxlength="255" size="25">
<br>
<label for="email">Email:</label><br>
<input type="email" name="domain_email" id="email_address" maxlength="255" size="20">
<br>
<label for="url">Url:</label><br>
<input type="url" name="url" id="website_address" maxlength="255" size="50">
<br>
<label for="text">Title:</label><br>
<input type="text" name="title" id="title" value="" maxlength="255" size="60">
<br>
<label for="text">Anchor:</label><br>
<input type="text" name="anchor" id="link_text" value="" maxlength="255" size="60">
<br>
<label for="description">Description:</label><br>
<textarea name="description" id="website_description" cols="50" rows="10"></textarea>
<br>
<label for="keywords">Keywords:</label><br>
<textarea name="keywords" id="website_keywords" cols="50" rows="10"></textarea>
<br>
<b>Terms & Conditions:</b>
<input type="radio" name="tos_agree" id="yes" value="Agree to TOS: Yes:"><label for="yes">Agree to TOS: Yes:</label>
<input type="radio" name="tos_agree" id="no" value="Agree to TOS: No:"><label for="no">Agree to TOS: No:</label>
<br>
<label for="newsletter_subcribe_yes">Newsletter Subcribe:Yes</label>
<input type="checkbox" name="newsletter_subcribe_yes" id="newsletter_subcribe_yes" value="Newsletter Subcribe:Yes">
<br>
<button type="reset">Reset!</button>
<br>
<button type="submit" name="submit" id="submit">Submit!</button><br>
</form>

<?php

$labels_assoc = array("domain_email"=>"required","domain"=>"required","url"=>"required","title"=>"not required","anchor"=>"required","description"=>"required","keywords"=>"required","tos_agree"=>"required","newsletter_subscribe_yes"=>"not required");
$labels_keys = array_keys($labels_assoc);

if($_SERVER['REQUEST_METHOD']==='POST')
{
    $errors = array();
    //Form Labels (name="") & MySql Table Column Names.
    $labels_assoc = array("domain_email"=>"required","domain"=>"required","url"=>"required","title"=>"not required","anchor"=>"required","description"=>"required","keywords"=>"required","tos_agree"=>"required","newsletter_subscribe_yes"=>"not required");
    $labels_keys = array_keys($labels_assoc);

    foreach($labels_assoc as $key=>$value)
    {
        if($value=="required")
        {
            if(empty($_POST[$key])||!is_string($_POST[$key]))
            {
                $errors[] = '<b>' .ucfirst($key) .'</b>'  .' is required!';
            }
            else
            {
                //Required Input
                if($key == 'domain_email' && !filter_input(INPUT_POST,"domain_email",FILTER_VALIDATE_EMAIL))
                {
                    die('Enter a valid Email!');
                }
                //Required Input
                elseif($key == 'domain' && !filter_input(INPUT_POST,"domain",FILTER_VALIDATE_DOMAIN))
                {
                    die('Enter a valid Domain!');
                }
                //Required Input
                elseif($key == 'url' && !filter_input(INPUT_POST,"url",FILTER_VALIDATE_URL))
                {
                    die('Enter a valid Url!');
                }
                //Optional Input
                elseif($key == 'title' && !EMPTY($key['title']) && !filter_input(INPUT_POST,"title",FILTER_SANITIZE_STRING))
                {
                    die('Enter a valid Page Title!');
                }
                elseif($key == 'anchor' && !filter_input(INPUT_POST,"anchor",FILTER_SANITIZE_STRING))
                {
                    die('Enter a valid Page Anchor!');
                }
                elseif($key == 'description' && !filter_input(INPUT_POST,"description",FILTER_SANITIZE_STRING))
                {
                    die('Enter a valid Page Description!');
                }
                elseif($key == 'tos_agree' && EMPTY($_POST['tos_agree']))
                {
                    die('You must agree to tos (terms & conditions)!');
                }
            }
        }
    }

    if(!empty($errors))
    {
        foreach($errors as $error)
        {
            echo '<b>Line ' .__LINE__ .': </b>'; echo 'Error Alert: ' .$error; echo '<br>';
        }
    }
    
    //TEST RESULT:
    //$_POST[''] outputs the value of value="".
    //For php to extract user input, the radio html needs to have the value="". Php cannot extract without it.


    //MYSQLI CONNECTION.
    mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

    $server = 'localhost';
    $user = 'root';
    $password = '';
    $database = 'index';

    if(!$conn = mysqli_connect("$server","$user","$password","$database"))
    {
        echo 'Mysqli Connection Error' .mysqli_connect_error($conn);
        echo 'Mysqli Connection Error Number' .mysqli_connect_errno($conn);
    }

    mysqli_set_charset($conn,'utf8mb4');


    $col1 = $labels_keys["0"];
    $col2 = $labels_keys["1"];
    $col3 = $labels_keys["2"];
    $col4 = $labels_keys["3"];
    $col5 = $labels_keys["4"];
    $col6 = $labels_keys["5"];
    $col7 = $labels_keys["6"];
    $col8 = $labels_keys["7"];
    $col9 = $labels_keys["8"];

    $sql = "INSERT into links ($col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8,$col9) VALUES (?,?,?,?,?,?,?,?,?)";
    $stmt = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($stmt,$sql);
    mysqli_stmt_bind_param($stmt,'sssssssss',$_POST["$col1"],$_POST["$col2"],$_POST["$col3"],$_POST["$col4"],$_POST["$col5"],$_POST["$col6"],$_POST["$col7"],$_POST["$col8"],$_POST["$col9"]);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
    }

?>

I'd appreciate any mentions of any other coding errors on my script apart from the problem I mentioned.

Thank You!




Aucun commentaire:

Enregistrer un commentaire