【问题标题】:Nothing is added in the db (db connection problems?)数据库中没有添加任何内容(数据库连接问题?)
【发布时间】:2018-06-20 03:10:29
【问题描述】:

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

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

CName text utf32_bin 可以为NULL

EName text utf32_bin 可以为NULL

电话 int(20) 可以为 NULL

电子邮件文本 utf32_bin 可以为 NULL

地址文本utf32_bin可以为NULL

Ticket tinyint(1) 可以为 NULL

金额 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 。 但是新的一行记录是真的创建的,ID是根据CURRENT_TIMESTAMP创建的。

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

数据库.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

【问题讨论】:

  • 数据库连接问题会导致您无法连接到数据库。这里的问题出在您的插入查询中。

标签: php html mysql sql database


【解决方案1】:

您在执行数组中传递了不正确的变量。

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

应该是。

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

【讨论】:

  • 这是一个答案,请编写适当的代码来解决您指出的问题。
  • $q->execute(array($name,$email,$mobile));应该。 $q->execute(array($Cname,$Address,$Amount));
  • 编辑您的答案并将其放在那里。不在评论区
  • 谢谢。抱歉,刚接触这个。
  • 我现在可以输入 $address, $Amount 了。但是CName还是不行,输入null。本来我想在这个字段输入汉字。但是我不能输入中文或英文。是与编码问题有关吗? CName 使用的是CName text utf8_unicode_ci 。这个字段是故意输入中文的。
猜你喜欢
  • 1970-01-01
  • 2011-09-23
  • 2020-06-12
  • 2020-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多