检查php中的文本框是否为空

时间:2015-02-21 07:01:49

标签: php html

我有一个php / html,用户输入许多值,然后使用mysqli和插入查询更新到数据库中,如果用户没有在任何文本框中输入任何内容我想要一条消息为了显示信息不存在,我尝试使用

if($ownerName == "" ){
    echo("Missing Information!"); 

但它不起作用。

这是我的PHP代码

<?php

include "connect.php";
if($_POST["submit"])
{
    $ownerName = $_POST['OwnerName'];
    $location = $_POST['Location'];
    $phoneNumber = $_POST['PhoneNumber'];

   if($ownerName == "" || $location == "" || $phoneNumber == ""){
    echo("Missing Information!");

    /*if(is_numeric($phoneNumber) ){
                        settype($phoneNumber, "integer");
            }*/
 

    $query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";




    if (!mysqli_query($con,$query))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "<script type='text/javascript'>alert('1 record added')</script>";
    

    mysqli_close($con); 
}
?>

这是我的表格

<html><head><title>Connect to Database</title></head><body> 

<font size="4"> Enter owner details</font><br><br> 
<form action="update.php" method="post" >

Owner Name:<input  type="text" name="OwnerName"> 
Location: <input  type="text" name="Location"> 
Phone Number:<input  type="text" name="PhoneNumber"> 
<input type="submit" name = "submit" value="Submit Value"> 
</form></body></html>

7 个答案:

答案 0 :(得分:3)

试试这个:

if((!isset($ownerName)) || $ownerName == "" || (!isset($location)) || $location == "" || (!isset($phoneNumber)) || $phoneNumber == "")){
    echo("Missing Information!");

    /*if(is_numeric($phoneNumber) ){
                        settype($phoneNumber, "integer");
            }*/

}
    else{
    $query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";




if (!mysqli_query($con,$query))
{
    die('Error: ' . mysqli_error($con));
}
echo "<script type='text/javascript'>alert('1 record added')</script>";


mysqli_close($con); 

        }

答案 1 :(得分:1)

试试这个

改变你的条件
 if($ownerName == "" || $location == "" || $phoneNumber == ""){
    echo("Missing Information!");
$query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";




    if (!mysqli_query($con,$query))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "<script type='text/javascript'>alert('1 record added')</script>";


    mysqli_close($con); 
}

 if($ownerName == "" || $location == "" || $phoneNumber == ""){
    echo("Missing Information!");
    } else {
$query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";




    if (!mysqli_query($con,$query))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "<script type='text/javascript'>alert('1 record added')</script>";


    mysqli_close($con); 
}                                     

答案 2 :(得分:0)

试试这个

&#13;
&#13;
<?php

include "connect.php";
if($_POST["submit"])
{
    $ownerName = $_POST['OwnerName'];
    $location = $_POST['Location'];
    $phoneNumber = $_POST['PhoneNumber'];}

   if(empty($ownerName) && empty($location) && empty($phoneNumber))

{
    echo("Missing Information!");}

    /*if(is_numeric($phoneNumber) ){
                        settype($phoneNumber, "integer");
            }*/
 


else{
    $query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";




    if (!mysqli_query($con,$query))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "<script type='text/javascript'>alert('1 record added')</script>";
    

    mysqli_close($con); 
}
?>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

尝试使用empty()函数或isset()

答案 4 :(得分:0)

<?php

    include "connect.php";
    if(isset($_POST["submit"]))
    {
        $ownerName = isset($_POST['OwnerName']) ? $_POST['OwnerName']: '';
        $location =  isset($_POST['Location']) ? $_POST['Location']: '';
        $phoneNumber = isset($_POST['PhoneNumber']) ? $_POST['PhoneNumber']: '';

        //preventing sql injection
        /*
          $ownerName = mysqli_real_escape_string($con, $ownerName);
          $$location = mysqli_real_escape_string($con, $ownerName);
          $$phoneNumber = mysqli_real_escape_string($con, $ownerName);
         */

        if(empty($ownerName) || empty($location) || empty($phoneNumber)){
            echo("Missing Information!");

            /*if(is_numeric($phoneNumber) ){
             settype($phoneNumber, "integer");
             }*/


        }else{
            $query = "INSERT INTO OWNER VALUES ('$ownerName','$location', $phoneNumber)";




            if (!mysqli_query($con,$query))
            {
                die('Error: ' . mysqli_error($con));
            }
            echo "<script type='text/javascript'>alert('1 record added')</script>";
        }
         mysqli_close($con);
    ?>

还尝试使用var_dump($ _ POST)查看发送的内容。

修改

我已经更新了if / else语句所以它现在应该可以工作了。此外,请确保您启用了错误报告:

<?php
// Turn off error reporting
error_reporting(0);

// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Report all errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>

答案 5 :(得分:0)

试试这个:

<?php

include "connect.php";
if($_POST["submit"])
{
 $ownerName = $_POST['OwnerName'];
 $location = $_POST['Location'];
 $phoneNumber = $_POST['PhoneNumber'];

 if($ownerName == "" || $location == "" || $phoneNumber == ""){
   echo("Missing Information!");
 }
 else{

 $query = "INSERT INTO OWNER VALUES ('$ownerName','$location',  $phoneNumber)";

   if (!mysqli_query($con,$query))
   {
    die('Error: ' . mysqli_error($con));
   }
   echo "<script type='text/javascript'>alert('1 record added')</script>";
 }
  mysqli_close($con); 
}   
?>

答案 6 :(得分:0)

**Please replace your code from below code**
<?php

    include "connect.php";

    if($_POST["submit"])
    {
        $ownerName  = $_POST['OwnerName'];
        $location   = $_POST['Location'];
        $phoneNumber    = $_POST['PhoneNumber'];

        if($ownerName == "" || $location == "" || $phoneNumber == ""){
            echo("Missing Information!");
        } else {
            // please replace your column name (`owner_name`, `location`, `phone_number`) 
            $query = "INSERT INTO OWNER (`owner_name`, `location`, `phone`) VALUES ('" . $ownerName . "', '" . $location. "', " . $phoneNumber. ")";

            if (!mysqli_query($con, $query))
            {
                die('Error: ' . mysqli_error($con));
            }
            echo "<script type='text/javascript'>alert('1 record added')</script>";

            mysqli_close($con); 

        }
    }
    ?>