数据库中没有添加任何内容(数据库连接有问题吗?)

时间:2018-06-20 03:10:30

标签: php html mysql sql database

我建立了一个名为“捐赠”的表格。结构是这样的。

  

Id(P键)日期时间不能为NULL CURRENT_TIMESTAMP

     

CName文本utf32_bin可以为空

     

EName文本utf32_bin可以为NULL

     

电话int(20)可以为空

     

电子邮件文本utf32_bin可以为空

     

地址文本utf32_bin可以为NULL

     

门票tinyint(1)可以为空

     

金额int(255)可以为NULL

我试图创建一个“创建”页面。

<?php
// Include config file
require_once 'database.php';
print_r($_POST);
// Define variables and initialize with empty values
$CName = $Address = $Amount = "";
$CName_err = $Address_err = $Amount_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Validate name
    $input_CName = trim($_POST["CName"]);
    if(empty($input_CName)){
        $CName_err = "Please enter a name.";
    } elseif(!filter_var(trim($_POST["CName"]), FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z'-.\s ]+$/")))){
        $CName_err = 'Please enter a valid name.';
    } else{
        $CName = $input_CName;
    }

    // Validate address
    $input_Address = trim($_POST["Address"]);
    if(empty($input_Address)){
        $Address_err = 'Please enter an address.';     
    } else{
        $Address = $input_Address;
    }

    // Validate Amount
    $input_Amount = trim($_POST["Amount"]);
    if(empty($input_Amount)){
        $Amount_err = "Please enter the amount.";     
    } elseif(!ctype_digit($input_Amount)){
        $Amount_err = 'Please enter a positive integer value.';
    } else{
        $Amount = $input_Amount;
    }

    // Check input errors before inserting in database
    if(empty($CName_err) && empty($Address_err) && empty($Amount_err)){
        // Prepare an insert statement
          $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO donation (CName, Address, Amount) VALUES (?, ?, ?)";

          $q = $pdo->prepare($sql);
            $q->execute(array($name,$email,$mobile));
            Database::disconnect();
            header("Location: index.php");
}}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create Record</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        .wrapper{
            width: 500px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header">
                        <h2>Create Record</h2>
                    </div>
                    <p>Please fill this form and submit to add employee record to the database.</p>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <div class="form-group <?php echo (!empty($CName_err)) ? 'has-error' : ''; ?>">
                            <label>Name</label>
                            <input type="text" name="CName" class="form-control" value="<?php echo $CName; ?>">
                            <span class="help-block"><?php echo $CName_err;?></span>
                        </div>
                        <div class="form-group <?php echo (!empty($Address_err)) ? 'has-error' : ''; ?>">
                            <label>Address</label>
                            <textarea name="Address" class="form-control"><?php echo $Address; ?></textarea>
                            <span class="help-block"><?php echo $Address_err;?></span>
                        </div>
                        <div class="form-group <?php echo (!empty($Amount_err)) ? 'has-error' : ''; ?>">
                            <label>Amount</label>
                            <input type="text" name="Amount" class="form-control" value="<?php echo $Amount; ?>">
                            <span class="help-block"><?php echo $Amount_err;?></span>
                        </div>
                        <input type="submit" class="btn btn-primary" value="Submit">
                        <a href="index.php" class="btn btn-default">Cancel</a>
                    </form>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>

我试图将CNameAddressAmount提交到数据库。

什么都不会传递到CNameAddressAmount中。 但是实际上是创建了新的记录行,并且根据CURRENT_TIMESTAMP创建了ID。

我真的怀疑是否发生了某些数据库连接问题。

database.php

<?php
class Database
{
    private static $dbName = 'donaton_info' ;
    private static $dbHost = 'localhost' ;
    private static $dbUsername = 'root';
    private static $dbUserPassword = '1234';

    private static $cont  = null;

    public function __construct() {
        die('Init function is not allowed');
    }

    public static function connect()
    {
       // One connection through whole application
       if ( null == self::$cont )
       {     
        try
        {
          self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword ); 
        }
        catch(PDOException $e)
        {
          die($e->getMessage()); 
        }
       }
       return self::$cont;
    }

    public static function disconnect()
    {
        self::$cont = null;
    }
}
?>

index.php enter image description here

1 个答案:

答案 0 :(得分:1)

您在execute数组中传递了错误的变量。

$q->execute(array($name,$email,$mobile));        

应该是。

$q->execute(array($Cname,$Address,$Amount));