mysqli查询不会在数据库中插入行

时间:2015-01-24 11:28:29

标签: php mysqli

希望你们可以帮助我,我一直在试图找出似乎是我的错误但无济于事,这是我的代码:

**This is my index.php**

<?php
include('config.php');
if($_POST['submit']=='Borrow')
{   
    $mysqli->query("INSERT INTO `borrowersprofile`(`lastname`, `firstname`, `middlename`) VALUES(
                            '".$_POST['lastname']."',
                            '".$_POST['firstname']."',
                            '".$_POST['middlename']."',
                            NOW())");

    header("Location: index.php");
    exit;
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form method="post" action="">
<label for="lastname">Lastname:</label>
<input name="lastname" type="text" id="lastname" style="width:120px;"/>

<label for="firstname">Firstname:</label>
<input name="firstname" type="text" id="firstname" style="width:120px;"/>

<label for="middlename">M.I:</label>
<input name="middlename" type="text" id="middlename" style="width:35px;"/><br />
<input type="submit" name="submit" value="Borrow" />

</form>
</body>
</html>

这是config.php

<?php 
$db_username = 'root';
$db_password = '';
$db_name = 'bsystem';
$db_host = 'localhost';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
?>

我一直在尝试检查甚至尝试重新制作数据库,但它仍然没有添加数据,顺便说一句,这是一个学校项目所以安全性并不重要,希望你们能帮助我!感谢。

2 个答案:

答案 0 :(得分:0)

正如@ user328指出

1)假设您的表格IDprimary keyAUTO_INCREMENT,因此您无需在查询中提及ID列。 Sql会自动为您插入。

2)您需要提及列名称保存now()返回的值的位置。

if($_POST['submit']=='Borrow')
{   
$mysqli->query("INSERT INTO `borrowersprofile`(`lastname`, `firstname`, `middlename`, `DATE`) VALUES(
                        '".$_POST['lastname']."',
                        '".$_POST['firstname']."',
                        '".$_POST['middlename']."',
                        NOW())");

header("Location: index.php");
exit;

}

答案 1 :(得分:0)

尝试在数据库中创建此表,然后使用下面的脚本。

sql table:

CREATE TABLE `borrowersprofile` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`lastname` VARCHAR(100) NOT NULL,
`firstname` VARCHAR(100) NOT NULL,
`middlename` VARCHAR(100) NOT NULL,
`datetime` DATETIME NOT NULL
)ENGINE=InnoDB;

的index.php

<?php
require_once('./config.php');

if(!empty($_POST['lastname']) AND !empty($_POST['firstname']) AND !empty($_POST['middlename'])){

   $lastname = $_POST['lastname'];
   $firstname = $_POST['firstname'];
   $middlename = $_POST['middlename'];

   if ($stmtint = $mysqli->prepare("INSERT INTO `borrowersprofile`( `lastname`, `firstname`, `middlename`, `datetime`) VALUES(?, ?, ?, NOW())") {

       $stmtint->bind_param("sss", $lastname, $firstname, $middlename);

       if ($stmtint->execute()) {
           $stmtint->close();
           echo "User saved successfully!";           
       }else{
           die("Error Message:".$mysqli->error);
       }

       header("Location: index.php");
       exit;
    }  
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form method="post" action="">
<label for="lastname">Lastname:</label>
<input name="lastname" type="text" id="lastname" style="width:120px;"/>

<label for="firstname">Firstname:</label>
<input name="firstname" type="text" id="firstname" style="width:120px;"/>

<label for="middlename">M.I:</label>
<input name="middlename" type="text" id="middlename" style="width:35px;"/><br />
<input type="submit" name="submit" value="borrow" />

</form>
</body>

的config.php

<?php
$db_username = 'root';
$db_password = '';
$db_name = 'bsystem';
$db_host = 'localhost';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}
?>