php - 忽略空提交的帖子?

标签 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 但我也无法让它工作。

最佳答案

试试这个:

<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

关于php - 忽略空提交的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22047529/

相关文章:

php - 从下拉菜单中插入数据到数据库,其数据是从另一个数据库导入的

php - Laravel 查询生成器 - 查询不工作但在 SQL 控制台中工作

javascript - RGD : Dynamic adding HTML by jQuery issue

javascript - 什么时候在 html 页面的 head 和 body 部分使用 &lt;script&gt; 标签?

javascript - 如何使用类似网格的设计在悬停时将叠加 div 放置在另一个 div 上?

MySQL如果存在则更新,如果不存在则插入

javascript - 如何简洁下面的jquery代码片段

php - 如何为 Symfony2 站点正确设置 Varnish?

php - 将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表

php - Laravel - 所有用户的一个通知