忽略空提交的帖子?

时间:2014-02-26 16:35:03

标签: php html mysql

我正在编写一个查询来搜索数据库并打印出生成的客户编号。我一直在尝试添加错误检查,这样如果输入一个数字而不是在数据库中,它会打印出一条消息。有没有办法让php第一次忽略输入字段,这样我就不必在加载网页后弹出消息了?

<html>
<head>
<title>Select Customer Listing</title>
</head>
<?php
session_start();

echo "Hello, " .$_SESSION['username'];

//Address error handling
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);

//Define the Customer Number php variable name
$customer = $_POST['cusnum'];
print "<br>The customer number chosen is <strong>$customer</strong>.<br />";

//Attempt to connect
if($connection = @mysql_connect('localhost', 'username', 'password')){
    print '<p>Successfully connected to MySQL.</p>';
}
else{
    die('<p>Could not connect to MySQL because: <b>' .mysql_error().'</b></p>');
}

if(@mysql_select_db("GIULIANA_PREMIERE", $connection)){
    print '<p>The <i>GIULIANA_PREMIERE</i> database has been selected.</p><hr>';
}
else{
    die('<p>Could not select the GIULIANA_PREMIERE database because: <b.'     .mysql_error().'</b></p>');
}
?>

<?php

//define query
$query = "SELECT * FROM CUSTOMER WHERE CUSTOMER_NUM = '$customer'";
$queryALL = "SELECT * FROM CUSTOMER";

$lower = strtolower($customer);

//output the resulting query table
if($r = mysql_query($query)){
    while($row = mysql_fetch_array($r)){
        print "<p><b>{$row['CUSTOMER_NUM']}</b> : {$row['CUSTOMER_NAME']}<BR/>    </P>\n";
    }
}

//if any case of 'all' is entered, print out query
if(strcmp($lower, 'all') == 0){
    if($r2 = mysql_query($queryALL)){
        while($row2 = mysql_fetch_array($r2)){
            print "<p><b>{$row2['CUSTOMER_NUM']}</b> : {$row2['CUSTOMER_NAME']}    <BR/></P>\n";
        }
    }
}

//if no number was entered, display pop up
if(empty($customer) && counter != 1){
    print '<script language="javascript">';
    print 'alert("Please enter a customer number.")';
    print '</script>';
}

//if entered number was invalid, display message
if((mysql_num_rows($r) == 0) && (strcmp($lower, 'all') != 0)){
    print "Not a valid cutomer number.";
    print "<br>Please try again.";
}


?>
</body>
</html>

我已尝试将所有if语句更改为elseif,但我也无法将其发挥作用。

1 个答案:

答案 0 :(得分:0)

试试这个:

<html>
<head>
<title>Select Customer Listing</title>
<script>

<?php 
   $counter = 0; //any number but one
   if(empty($customer) && $counter != 1){ 
       $counter = 1; //the condition has been met, lets change the variable to 1 so   this condition isn't met again
?>
      alert("Please enter a customer number.");
<?php 
   } 
?>
</script>
</head>
//rest of your code