在php中需要错误和NULL的建议

时间:2014-11-24 00:05:56

标签: mysql forms null die

我们正在使用这个脚本:

<?php 
require("common.php"); 
if(!empty($_POST)) 
{ 
    $query = " 
        SELECT 
            1 
        FROM customers 
        WHERE 
            phone1 = :phone1
    "; 
    $query_params = array( 
        ':phone1' => $_POST['phone1'] 
    ); 

    try 
    {  
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $row = $stmt->fetch(); 
    if($row) 
    { 
        die("This phone number is already in use"); 
    } 
            $query = " 
        SELECT 
            1 
        FROM customers 
        WHERE 
            phone2 = :phone2
    "; 
    $query_params = array( 
        ':phone2' => $_POST['phone2'] 
    ); 
    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $row = $stmt->fetch(); 
    if($row) 
    { 
        die("This phone number is already in use"); 
    } 
    $query = " 
        SELECT 
            1 
        FROM customers 
        WHERE 
            email = :email 
    "; 
    $query_params = array( 
        ':email' => $_POST['email'] 
    ); 
    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $row = $stmt->fetch(); 
    if($row) 
    { 
        die("This email address is already registered"); 
    } 
    $query = " 
        INSERT INTO customers ( 
            firstname, 
            lastname, 
            phone1, 
            phone2,
            street,
            city,
            state,
            zip,
            email
        ) VALUES ( 
            :firstname, 
            :lastname, 
            :phone1,
            :phone2,
            :street,
            :city,
            :state,
            :zip,
            :email
        ) 
    "; 
    $query_params = array( 
        ':firstname' => $_POST['firstname'], 
        ':lastname' => $_POST['lastname'], 
        ':phone1' => $_POST['phone1'], 
        ':phone2' => $_POST['phone2'], 
        ':street' => $_POST['street'], 
        ':city' => $_POST['city'], 
        ':state' => $_POST['state'], 
        ':zip' => $_POST['zip'], 
        ':email' => $_POST['email'] 
    ); 
    try 
    {
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    {  
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    header("Location: adminmain.php");  
    die("Redirecting to adminmain.php"); 
} 
?>

形式:

<h1>Add Customer to Database</h1> 
<form action="addcust.php" method="post"> 
    First Name:<br /> 
    <input type="text" name="firstname" value="" /><
    <br /><br /> 
    Last Name:<br /> 
    <input type="text" name="lastname" value="" /> 
    <br /><br />
    Phone 1:<br /> 
    <input type="text" name="phone1" value="" /> 
    <br /><br />
    Phone 2:<br /> 
    <input type="text" name="phone2" value="" /> 
    <br /><br />    
    Street Address:<br /> 
    <input type="text" name="street" value="" /> 
    <br /><br />
    City:<br /> 
    <input type="text" name="city" value="" /> 
    <br /><br /> 
    State:<br /> 
    <input type="text" name="state" value="" /> 
    <br /><br />
    Zip:<br /> 
    <input type="text" name="zip" value="" /> 
    <br /><br />    
    E-Mail:<br /> 
    <input type="text" name="email" value="" /> 
    <br /><br /> 
    <input type="submit" value="Add Customer" /> 
</form>

并且想知道是否有人可以帮助我或指出我如何解决两件事:当表单中的字段留空时,它将空字符串输入mysql而不是NULL。

另外,无论如何都有这个位:

        if($row) 
    { 
        die("This email address is already registered"); 
    } 

在表单字段旁边显示为错误而不是新页面?

谢谢!

0 个答案:

没有答案