1

我不知道我在这里做错了什么,但是我很难尝试将表单中的数据插入数据库。

这是我的桌子

    CREATE TABLE IF NOT EXISTS `accounts` (
  `accid` int(11) NOT NULL AUTO_INCREMENT,
  `acc_number` varchar(50) NOT NULL,
  `cust_name` varchar(255) NOT NULL,
  `cust_tel` varchar(50) NOT NULL,
  `cust_address` varchar(255) NOT NULL,
  `cust_opendate` date NOT NULL,
  `cust_openamount` decimal(13,2) NOT NULL,
  `cust_balance` decimal(13,2) NOT NULL,
  `cust_message` text NOT NULL,
  `cust_openby` varchar(50) NOT NULL,
  PRIMARY KEY (`accid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

$_session['username'] 已设置,并且数据库已包含并正在运行。当我填写输入并单击提交时,我收到消息新帐户已添加。

<?php
session_start();

$success = array();
$errors = array();

include('header.php');

if (isset($_SESSION['username']))
{
    include_once('libs/db-con.php');

    if (isset($_POST['submit']))
    {
        $accNumber = $_POST['accnumber'];
        $custName = $_POST['custname'];
        $tel = $_POST['tel'];
        $addess = $_POST['address'];
        $openDate = $_POST['opendate'];
        $openAmount = $_POST['openamount'];
        $balance = $_POST['balance'];
        $message = $_POST['message'];
        $openBy = $_SESSION['username'];

        if (empty($accNumber) || empty($custName) || empty($tel) || empty($addess) || empty($openDate) || empty($openAmount) || empty($balance) || empty($openBy))
        {
            $errors[] = 'All fields are required';
        }else{
            $query = $pdo->prepare("INSERT INTO accounts(accid, acc_number, cust_name, cust_tel, cust_address, cust_opendate, cust_openamount, cust_balance, cust_message, cust_openby)  VALUE(?,?,?,?,?,?,?,?,?)");
            $query->bindValue(1,$accnumber);
            $query->bindValue(2,$custName);
            $query->bindValue(3,$tel);
            $query->bindValue(4,$address);
            $query->bindValue(5,$openDate);
            $query->bindValue(6,$openAmount);
            $query->bindValue(7,$balance);
            $query->bindValue(8,$message);
            $query->bindValue(9,$openBy);

            $query->execute();

            $success[] = 'New account has been added';

            }
        }



        $query = $pdo->prepare("SELECT max(acc_number) FROM accounts");

                $query->execute();

               while($num = $query->fetchAll()){

                if($num[0] == null){

                $accnumber = "100001";

                }else{

                $accnumber = $num[0]++; 
            }    
        }       
?>

           <div class="today-acc">
            <h3>create new account</h3>
            <div id="form-newacc">
                <form action="" method="post">
                <table>
                    <tr>
                        <td><label for="accnumber">Account No:</label><input type="text" name="accnumber" value="<?php echo $accnumber[0]; ?>"></td>
                        <td><label for="custname">Name:</label><input type="text" name="custname"></td>
                        <td><label for="tel">Tel:  </label><input type="text" name="tel"></td>
                    </tr>
                    <tr>
                        <td><label for="address">Address:</label><input type="text" name="address"></td>
                        <td><label for="opendate">Open date:</label><input type="text" name="opendate" id="datepicker"></td>
                        <td><label for="openamount">Open Amount:</label><input type="text" name="openamount"></td>

                    </tr>
                    <tr>
                        <td><label for="balance">Balance:</label><input type="text" name="balance"></td>
                        <td><label for="message">Message:</label><textarea name="message" id="message"></textarea></td>


                    </tr>

                </table>
                        <input type="submit" name="submit" Value="Create">
                        <input type="reset" name="reset" Value="clear">
                </form>
                <?php foreach ($success as $successes) {
                    echo '<div id="success">'.$successes.'</div>';
                }?>
                <?php foreach ($errors as $error) {
                    echo '<div id="errors">'.$error.'</div>';
                }?>
            </div>
            </div>

<?php

}
else{

        header("location: login.php");
    }
         ?>

我更新了代码。提前致谢

4

2 回答 2

2

您正在尝试插入 10 列:

accid, acc_number, cust_name, cust_tel, cust_address, cust_opendate, cust_openamount, cust_balance, cust_message, cust_openby

但是你只有9个问号:

?,?,?,?,?,?,?,?,?

建议不要使用问号,因为它可能会导致像这样的简单错误,最好做类似的事情

"INSERT INTO accounts(accid, acc_number, cust_name, cust_tel, cust_address, cust_opendate, cust_openamount, cust_balance, cust_message, cust_openby)  VALUE(:accid, :acc_number, :cust_name, :cust_tel, :cust_address, :cust_opendate, :cust_openamount, :cust_balance, :cust_message, :cust_openby)"

如果 accid 确实不应该接收一个值,而是应该接收一个自动递增的值,那么你不应该在查询中提及它。

编辑:

一些想法:

1:你用这个检查用户名的会话是否存在

isset($_SESSION['username']

但其他在哪里?如果用户名没有在任何地方设置,你什么都没有做,尝试输出一些错误,表明用户没有登录。

2:您假设如果执行查询,查询将起作用,您永远无法确定执行的查询是否正确。

相反,如果假设它总是正确的,就像你在这里做的那样:

$success[] = 'New account has been added';

然后你必须检查插入查询返回true还是false,如果执行的查询返回false,你可以返回错误信息。

$inserted = $query->execute();
$inserted ? $success[] = 'New account has been added' : $success[] = 'New account has not been added';

我最后使用的是三元运算符,你应该看看它。

于 2013-08-10T19:30:02.410 回答
-1
     $blahblah = mysql_query("INTO accounts(accid, acc_number, cust_name, cust_tel,   
cust_address, cust_opendate, cust_openamount, cust_balance, cust_message, cust_openby)VALUE(?,?,?,?,?,?,?,?,?,?)") or die(mysql_error());

您要插入 9 列而不是 10 列!

编辑

 $blahblah = mysql_query("INTO accounts VALUE('','?','?','?','?','?','?','?','?','?')") or die(mysql_error());  
  `accid` int(11) NOT NULL AUTO_INCREMENT,

ID 必须为 EMPTY,因为它是由您的数据库自动插入的。!并且您还将 Char 插入 Int 列。

于 2013-08-10T19:33:52.283 回答