PHP $_POST check if empty

Status
Not open for further replies.

Skythrust

Member
Jul 9, 2019
133
7
Hi All,

I am looking for a thing to check if the post field is filled in.

Like when I have a firstname, middlename and lastname and I insert just the first and lastname normally in SQLSRV he is set this to a NULL value.
Now he add some none data which is a blank colomn in SQLSRV

So I was assuming that I can easily fix this with an if else statement but that doesn't work.

For now I have this code

PHP:
if(EMPTY($_POST['middlename']))
    {
        $code = 'SET middlename = NULL';
    }
else
    {
        $code = 'SET middlename =' $_POST['middlename'];
    }

Any idea's how I can realize this?
Thanks!
 

Laynester

a bad bitch
Nov 7, 2018
305
437
in your database you could just allow it to be null, i think so anyway.. also you should really be filtering your POST's
Post automatically merged:

in your database you could just allow it to be null, i think so anyway.. also you should really be filtering your POST's
 

Skythrust

Member
Jul 9, 2019
133
7
in your database you could just allow it to be null, i think so anyway.. also you should really be filtering your POST's
Post automatically merged:

in your database you could just allow it to be null, i think so anyway.. also you should really be filtering your POST's

Already tried this but that isn't an option for me.

you can try doing checks such as if not empty then continue else give an error something.
Yeah I am looking for that option, but not with an error just if field is empty than NULL else $_POST
 

Johno

:: xHosts :: www.xhosts.uk
Sep 12, 2011
586
255
If you want to set it as null when there has been no value provided you could do that on the table, default value null then if something is set insert it
 

Skythrust

Member
Jul 9, 2019
133
7
If you want to set it as null when there has been no value provided you could do that on the table, default value null then if something is set insert it
I see the way what you mean, but I am looking for a solution to create an if else statement because there are more things where I need that.
Like IF $_POST['middlename'] = EMTPY THEN firstname + " " + lastname else firstname + " " + middlename + " " + lastname
 

Higoka

Active Member
Dec 16, 2018
174
74
simple as that
PHP:
if (isset($_POST['middlename'])) {
    $code = "SET middlename = '{$_POST['middlename']}'";
} else {
    $code = 'SET middlename = NULL';
}
 
Last edited:

Skythrust

Member
Jul 9, 2019
133
7
simple as that
PHP:
if (isset($_POST['middlename'])) {
    $code = "SET middlename = '{$_POST['middlename']}'";
} else {
    $code = 'SET middlename = NULL';
}

That doesn't work, I got this as result in SQL SET middlename = ' which looks like he is insert the isset code and not the else NULL code.
 

Skythrust

Member
Jul 9, 2019
133
7
Can you share your entire page code so we can see everything that is going on ?

Sure, keep in mind that that registration form has some variables for the languages, the rest is working fine but I would like to have an if else statement for at least just the middlename

Registration_process;

PHP:
<?php 
header("Location: ../registration.php?submit=AddVisitor");
session_start();
include 'config.php';
include 'variables.php';

$fname = $_POST['firstname'];

//$mname = $_POST['middlename'];
$lname = $_POST['lastname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$license = $_POST['license'];
$company = $_POST['company'];
$host = $_POST['host'];
$option = $_POST['option'];
$badge = rand ($RandomMin,$RandomMax);
$custid = $Current_CustomerId;
$reason = $_POST['reason'];
$visitday = $_POST['visitday'];
//$test = $_POST['firstname'] ." ". $_POST['middlename'] ." ".$_POST['lastname'];
if (isset($_POST['middlename'])) {
    $code = "SET middlename = '{$_POST['middlename']}'";
} else {
    $code = 'SET middlename = NULL';
}


$connectionInfo = array( "Database"=>$SQL_InitialCatalog, "UID"=>$SQL_UserID, "PWD"=>$SQL_Password);
$conn = sqlsrv_connect( $SQL_DataSource, $connectionInfo);
if ($conn === false) { 
    echo "Could not connect.\n"; 
    die(print_r(sqlsrv_errors(), true)); 
} 

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

        $query = "INSERT INTO Visits (Firstname,Middlename,Lastname,Email,Phone,License,Company,Host,Department,CredentialNumber,ReasonForVisit,VisitDay,CustomerId) VALUES ('{$fname}','{$code}','{$lname}','{$mail}','{$phone}','{$license}','{$company}','{$host}','{$option}','{$badge}','{$reason}','{$visitday}','{$custid}')

        ";
        $stmt = sqlsrv_query($conn, $query);   


        if( $stmt === false )
        {
            die( print_r( sqlsrv_errors(), true));
        }
    }
?>

Registration Form;
PHP:
<form action="inc/registration_process.php" method="post">
    <div>
        <div class="row">
            <?php echo $registration_firstname ?><font color="red" font-weight="bold">*</font>:         
            <input class="form_text" type="text" name="firstname" style="width:30%;margin-left:27px;" required/>    <br>
            <?php echo $registration_middlename ?>:         
            <input class="form_text" type="text" name="middlename" style="width:15%;margin-top:10px;margin-left:17px;"/> <br>
            <?php echo $registration_lastname ?><font color="red" font-weight="bold">*</font>:       
            <input class="form_text" type="text" name="lastname" style="width:30%;margin-top:10px;margin-left:28px;" required/> <br>
            <?php echo $registration_mail ?><font color="red" font-weight="bold">*</font>:       
            <input class="form_text" type="text" name="mail" style="width:30%;margin-top:10px;margin-left:15px;" required/> <br>
                                    
            <?php echo $registration_phone ?>:                 
            <input class="form_text" type="text" name="phone" style="width:30%;margin-top:10px;margin-left:57px;"/> <br>
            <?php echo $registration_license ?>:         
            <input class="form_text" type="text" name="license" style="width:30%;margin-top:10px;margin-left:17px;"/> <br>
                                    
            <?php echo $registration_company ?>:             
            <input class="form_text" type="text" name="company" style="width:30%;margin-top:10px;margin-left:34px;"/> <br>
            <?php echo $registration_host ?>:                 
            <input class="form_text" type="text" name="host" style="width:30%;margin-top:10px;margin-left:67px;"/> <br>
            <?php echo $registration_department ?>:         
            <select name="option" id="Option"  style="width:30%;margin-top:10px;margin-left:22px;">
            
                <option value=""><?php echo $IndexDepartment ?></option>
                    <?php
                        $connectionInfo = array( "Database"=>$SQL_InitialCatalog, "UID"=>$SQL_UserID, "PWD"=>$SQL_Password);
                        $conn = sqlsrv_connect( $SQL_DataSource, $connectionInfo);

                        $sql = "  SELECT [Name] FROM Options WHERE CustomerId = ('$Current_CustomerId')";
                        $result = sqlsrv_query($conn,$sql) or die("Couldn't execute query");
                                            
                        while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
                        echo '<option value="'.$data['Name'].'">';
                        echo $data['Name'];
                        echo "</option>";}                                       
                        ?>
            </select> <br>
            
            <?php echo $registration_visitday ?>:         
            <input class="form_text" type="date" name="visitday" style="width:16%;margin-top:10px;margin-left:5px;"> <br>
            <?php echo $registration_reason ?>:
            <input class="form_text" type="text" name="reason" style="width:30%;margin-top:10px;margin-left:44px;"/> <br>
            
            <input class="btnsettings" type="submit" name="submit" value="Sign in"/> <br>       
        </div>
    </div>
</form>
 

Skythrust

Member
Jul 9, 2019
133
7
i dont know what you mean
This is what you'll see in SQL; in red the field which has the code what you send me yet. in orange what it looks like without your code, and I would like to have it as the fields in green
 

Higoka

Active Member
Dec 16, 2018
174
74
This is what you'll see in SQL; in red the field which has the code what you send me yet. in orange what it looks like without your code, and I would like to have it as the fields in green
yes of course, then you do something like this:
PHP:
$code = $_POST['middlename'] ?? null;
 

Skythrust

Member
Jul 9, 2019
133
7
yes of course, then you do something like this:
PHP:
$code = $_POST['middlename'] ?? null;
Still the column is empty and not the value NULL

That's because one of your SQL values is $code.
PHP:
if (isset($_POST['middlename'])) {
    $code = filter_input(INPUT_POST, 'middlename', FILTER_SANITIZE_STRING);
} else {
    $code = 'NULL';
}
This doesn't work for me.
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
Show me the code after you made the modifications. There's no reason it shouldn't work like that. If middlename is set, it sets $code variable to whatever middlename was set to with some sanitization. If it's not setting, it should be setting it to NULL.
 

Skythrust

Member
Jul 9, 2019
133
7
Show me the code after you made the modifications. There's no reason it shouldn't work like that. If middlename is set, it sets $code variable to whatever middlename was set to with some sanitization. If it's not setting, it should be setting it to NULL.

Code:
<?php  
header("Location: ../registration.php?submit=AddVisitor");
session_start();
include 'config.php';
include 'variables.php';

$fname = $_POST['firstname'];

//$mname = $_POST['middlename'];
$lname = $_POST['lastname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$license = $_POST['license'];
$company = $_POST['company'];
$host = $_POST['host'];
$option = $_POST['option'];
$badge = rand ($RandomMin,$RandomMax);
$custid = $Current_CustomerId;
$reason = $_POST['reason'];
$visitday = $_POST['visitday'];
//$test = $_POST['firstname'] ." ". $_POST['middlename'] ." ".$_POST['lastname'];


if (isset($_POST['middlename'])) {
    $code = filter_input(INPUT_POST, 'middlename', FILTER_SANITIZE_STRING);
} else {
    $code = 'NULL';
}


$connectionInfo = array( "Database"=>$SQL_InitialCatalog, "UID"=>$SQL_UserID, "PWD"=>$SQL_Password);
$conn = sqlsrv_connect( $SQL_DataSource, $connectionInfo);
if ($conn === false) {  
    echo "Could not connect.\n";  
    die(print_r(sqlsrv_errors(), true));  
}  

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

        $query = "INSERT INTO Visits (Firstname,Middlename,Lastname,Email,Phone,License,Company,Host,Department,CredentialNumber,ReasonForVisit,VisitDay,CustomerId) VALUES ('{$fname}','{$code}','{$lname}','{$mail}','{$phone}','{$license}','{$company}','{$host}','{$option}','{$badge}','{$reason}','{$visitday}','{$custid}')

        ";
        $stmt = sqlsrv_query($conn, $query);    


        if( $stmt === false ) 
        {
            die( print_r( sqlsrv_errors(), true));
        }
    }
?>
 
Status
Not open for further replies.

Users who are viewing this thread

Top