【问题标题】:I could't insert data into MySQL table我无法将数据插入 MySQL 表
【发布时间】:2017-11-08 09:29:31
【问题描述】:

只有在我尝试插入数据时才会遇到这个问题,当我尝试将数据插入到具有不同值集的另一个表时,相同的代码运行得非常好。

<?php

class crud
{
    private $db;

    function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function create($atitle,$agen,$ams,$adob,$afn,$aln,$acob,$acoc,$aemail,$amob,$aaddr,$amesse)    {
        try
        {
            $stmt = $this->db->prepare("INSERT INTO contacts(title,gen,ms,dob,fn,ln,cob,coc,email,mob,addr,messe) VALUES(:atitle,:agen,:ams,:adob,:afn,:aln,:acob,:acoc,:aemail,:amob,:aaddr,:amesse)");
            $stmt->bindparam(":atitle",$atitle);
            $stmt->bindparam(":agen",$agen);
            $stmt->bindparam(":ams",$ams);
            $stmt->bindparam(":adob",$adob);
            $stmt->bindparam(":afn",$afn);
            $stmt->bindparam(":aln",$aln);
            $stmt->bindparam(":acob",$acob);
            $stmt->bindparam(":acoc",$acoc);
            $stmt->bindparam(":aemail",$aemail);
            $stmt->bindparam(":amob",$amob);
            $stmt->bindparam(":aaddr",$aaddr);
            $stmt->bindparam(":amesse",$amesse);
            $stmt->execute();
            return true;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }

    }
}

上面是用来插入数据的类。

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-save']))
{
    $atitle = $_POST['actitle'];
    $agen = $_POST['acgen'];
    $ams = $_POST['acms'];
    $acdate = $_POST['acdate'];
    $adob = date('Y-m-d', strtotime(str_replace('-', '/', $acdate)));
    $afn = $_POST['acfn'];
    $aln = $_POST['acln'];
    $acob = $_POST['accob'];
    $acoc = $_POST['accoc'];
    $aemail = $_POST['aemail'];
    $amob = $_POST['acmob'];
    $aaddr = $_POST['acaddr'];
    $amesse = $_POST['acmesse'];

    if($crud->create($atitle,$agen,$ams,$adob,$afn,$aln,$acob,$acoc,$aemail,$amob,$aaddr,$amesse))
    {
        header("Location: add-con.php?inserted");
    }
    else
    {
        header("Location: add-con.php?failure");
    }
}
?>
    <?php include_once 'header.php'; ?>
    <div class="clearfix"></div>

    <?php
if(isset($_GET['inserted']))
{
    ?>
    <div class="container">
        <div class="alert alert-info">
            <strong>WOW!</strong> Record was inserted successfully <a href="index.php">HOME</a>!
        </div>
    </div>
    <?php
}
else if(isset($_GET['failure']))
{
    ?>
        <div class="container">
            <div class="alert alert-warning">
                <strong>SORRY!</strong> ERROR while inserting record !
            </div>
        </div>
        <?php
}
?>

        <div class="clearfix"></div><br />

        <div class="container">


            <form method='post'>

                <table class='table table-bordered'>

                    <tr>
                        <td>Title</td>
                        <td><input type='text' name='actitle' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>Gender</td>
                        <td><input type='text' name='acgen' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>Marital Status</td>
                        <td><input type='text' name='acms' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>DOB</td>
                        <td><input type='text' name='acdate' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>First Name</td>
                        <td><input type='text' name='acfn' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type='text' name='acln' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>Country of Birth</td>
                        <td><input type='text' name='accob' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>COC</td>
                        <td><input type='text' name='accoc' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>email</td>
                        <td><input type='text' name='acemail' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>mob</td>
                        <td><input type='text' name='acmob' class='form-control' required></td>
                    </tr>
                    <tr>
                        <td>address</td>
                        <td><input type='text' name='acaddr' class='form-control' required></td>
                    </tr>

                    <tr>
                        <td>messege</td>
                        <td><input type='text' name='acmesse' class='form-control' required></td>
                    </tr>
                    <td colspan="2">
                        <button type="submit" class="btn btn-primary" name="btn-save">
            <span class="glyphicon glyphicon-plus"></span> Create New Record
            </button>
                        <a href="index.php" class="btn btn-large btn-success"><i class="glyphicon glyphicon-backward"></i> &nbsp; Back to index</a>
                    </td>
                    </tr>

                </table>
            </form>


        </div>

        <?php 
include_once 'footer.php'; 
?>

这是我的 html 部分,我在其中输入我的值。

<?php

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "event";


try
{
    $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
    $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

include_once 'class.crud.php';

$crud = new crud($DB_con);

?>

这是数据库配置文件。

请帮我看看我哪里出错了?

【问题讨论】:

  • @Kirk Beard...帮帮我。这段代码有什么问题?
  • 你有什么错误吗?
  • 没有错误。我可以把源码发给你。你能帮我检查一下吗?提前致谢。
  • 这里的罪魁祸首是电子邮件字段。它在解析电子邮件时留下一个空白字段。如果我删除电子邮件部分,一切都可以正常工作而无需更改代码。帮我在这里插入一封电子邮件。

标签: php mysql sql pdo insert-into


【解决方案1】:

我有同样的问题。这对我有用,也许对你也有用。

也可以为所有字符串变量制作日期时间(不知道您的数据库):

$atitle = '"'.$_POST['actitle'].'"';

或者代替“做”。

希望对你有帮助^^

【讨论】:

  • 很抱歉再次打扰您。除电子邮件字段外,一切正常。无法将电子邮件插入 MySQL 表。
  • 您数据库中的字段 email 是 VARCHAR(45) 字段吗?
  • 是...VARCHAR(45)
  • 也许电子邮件地址超过 45 个字符?你做了这个? $amail = '"'.$_POST['amail'].'"';也许尝试制作 VARCHAR(60)
  • 我尝试增加 MySQL 表中 email 列的长度,但仍然在表中得到一个空白字段,而其他字段被插入而没有任何错误。
猜你喜欢
  • 2020-08-29
  • 2014-06-17
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多