sql INSERT成功/不成功的消息无法正常显示

时间:2015-12-03 03:03:13

标签: php mysqli

我使用表单将记录插入到我的数据库中。错误检查工作,记录正确插入。问题是记录插入消息显示在表单上方而不是在其下方。我已经尝试移动INSERT查询,因此它在表单下显示,但是当我这样做时,错误检查不起作用。任何帮助解决这个问题将不胜感激。

    <p class="first"><span class="error">* required field.</span></p>
    <br>

<?php
function test_input($data){
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

$servername = "localhost";
$username = "oldga740_Tonymm";
$password = "JtAjDm#6";
$dbname = "oldga740_SeniorProject";

// create connection
$connection = new mysqli($servername, $username, $password, $dbname);

if(isset($_POST['submit']) && !$connection->connect_error){
    // to track errors
    $error = false;

    // now validate input fields
    if (empty($_POST['Project']) || !isset($_POST['Project'])){
        $ProjectErr = "Project name is required";
        $error = true;
    }elseif(!preg_match("/^[A-Za-z0-9.-]+$/",$_POST['Project'])){
        // check if project only contains number, letters, comma's periods and whitespace
        $ProjectErr = "Only letters, numbers, comma's, periods and white space allowed";
        $error = true;
    }else{
        $Project = test_input($_POST['Project']);
    }

    if (empty($_POST['Client']) || !isset($_POST['Client'])){
        $ClientErr = "Client name is required";
        $error = true;
    }elseif(!preg_match("/^[A-Za-z 0-9.-]+$/",$_POST['Client'])){
        // check if project only contains number, letters, comma's periods and whitespace
        $ClientErr = "Only letters, numbers, comma's, periods and white space allowed";
        $error = true;
    }else{
        $Client = test_input($_POST['Client']);
    }

    if (empty($_POST['LastName']) || !isset($_POST['LastName'])){
        $LastNameErr = "Last name is required";
        $error = true;
    }elseif(!preg_match("/^[A-Za-z0-9-]+$/",$_POST['LastName'])){
        // check if last name only contains letters and whitespace
        $LastNameErr = "Only letters and white space allowed";
        $error = true;
    }else{
        $LastName = test_input($_POST['LastName']);
    }

    if (empty($_POST['DateReceived']) || !isset($_POST['DateReceived'])){
        $DateReceivedErr = "Date received is required";
        $error = true;
    }elseif(!preg_match("/^\d{4}-\d{2}-\d{2}$/",$_POST['DateReceived'])){
        // check if data received only contains letters and whitespace
        $DateReceivedErr = "Date must be entered as YYYY/MM/DD";
        $error = true;
    }else{
        $DateReceived = test_input($_POST['DateReceived']);
    }

    if(!$error){
        $query = "INSERT INTO Projects (Project, Client, LastName, DateReceived) VALUES ('$Project', '$Client', '$LastName', '$DateReceived')";
        if($connection->query($query)){
            echo "record is successfully inserted!";
        }else{
            echo "error: record could not be inserted";
        }
    }
}

?>

<?php
    $connection->close();
?>
<form action="http://www.oldgamer60.com/Project/NewProject.php" method="post">

<div class="fieldset">
        <fieldset>

        Project: <input type="text" name="Project" value="<?php if(isset($Project)){ echo $Project; } ?>">
        <span class="error">* <?php if(isset($ProjectErr)){ echo $ProjectErr; } ?></span>
        <br><br>
        Client: <input type="text" name="Client" value="<?php if(isset($Client)){ echo $Client; } ?>">
        <span class="error">* <?php if(isset($ClientErr)){ echo $ClientErr; } ?></span>
        <br><br>
        LastName: <input type="text" name="LastName" value="<?php if(isset($LastName)){ echo $LastName; } ?>">
        <span class="error">* <?php if(isset($LastNameErr)){ echo $LastNameErr; } ?></span>
        <br><br>
        DateReceived: <input type="text" name="DateReceived" value="<?php if(isset($DateReceived)){ echo $DateReceived; } ?>">
        <span class="error">* <?php if(isset($DateReceivedErr)){ echo $DateReceivedErr; } ?></span>
        <br><br>
        <input type="submit" name="submit" value="Submit"> 
     </fieldset>
    </div>
    </form>

</div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

使用变量a存储消息,然后在任何需要的地方显示它,如下所示:

// your code

if(isset($_POST['submit']) && !$connection->connect_error){
    // your code

    if(!$error){
        $query = "INSERT INTO Projects (Project, Client, LastName, DateReceived) VALUES ('$Project', '$Client', '$LastName', '$DateReceived')";
        if($connection->query($query)){
            $message = "record is successfully inserted!";
        }else{
            $message = "error: record could not be inserted";
        }
    }
}

?>

<?php
    $connection->close();
?>
<form action="http://www.oldgamer60.com/Project/NewProject.php" method="post">

    <div class="fieldset">
        <fieldset>
            Project: <input type="text" name="Project" value="<?php if(isset($Project)){ echo $Project; } ?>">
            <span class="error">* <?php if(isset($ProjectErr)){ echo $ProjectErr; } ?></span>
            <br><br>
            Client: <input type="text" name="Client" value="<?php if(isset($Client)){ echo $Client; } ?>">
            <span class="error">* <?php if(isset($ClientErr)){ echo $ClientErr; } ?></span>
            <br><br>
            LastName: <input type="text" name="LastName" value="<?php if(isset($LastName)){ echo $LastName; } ?>">
            <span class="error">* <?php if(isset($LastNameErr)){ echo $LastNameErr; } ?></span>
            <br><br>
            DateReceived: <input type="text" name="DateReceived" value="<?php if(isset($DateReceived)){ echo $DateReceived; } ?>">
            <span class="error">* <?php if(isset($DateReceivedErr)){ echo $DateReceivedErr; } ?></span>
            <br><br>
            <input type="submit" name="submit" value="Submit"> 
        </fieldset>
    </div>
</form>

    <?php

        if(isset($message)){ echo $message; }

    ?>

// your code