通过Web表单将数据插入数据库

时间:2013-12-01 02:44:17

标签: php html mysql database

我已经找到了我的问题的答案,并且看到所有编程变化我似乎无法解决我的问题。我创建了一个实际连接到我的数据库的php文件。但是,当我尝试通过我的php网页向我的数据库提交数据时,它将无法通过。当我尝试将数据从我的数据库显示到网页时,也会发生同样的情况。看到它实际连接到数据库,我不确定是什么问题。任何帮助表示赞赏,当你回答时尽量让我愚蠢。此外,我已经三次检查我的数据库名称和表名称,以确保它们与我的编码匹配。这是我的代码:

连接数据库:

<?php

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PSWD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'art database');

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);

?>

我的表格将数据插入我的数据库:

<?php

if (isset($_POST['submitted'])) {

    include('connect-mysql.php');

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $sqlinsert = "INSERT INTO users (first name, last name) VALUES ('$fname','$lname')";

    if (!mysqli_query($dbcon, $sqlinsert)) {
        die('error inserting new record');
        } //end of nested if

        $newrecord = "1 record added to the database";


} // end of the main if statement
?>

<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>


<hl>Insert Data into DB</hl>

<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true"/>
<fieldset>
    <legend>New People</legend>
    <label>First Name:<input type="text" name="fname" /></label>
    <label>Last Name:<input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?php
echo $newrecord;
?>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

它不起作用的原因是你的列/查询中有空格。

INSERT INTO users (first name, last name)

将它们包装成这样的反引号:

INSERT INTO users (`first name`, `last name`)

不建议在列名或表中使用空格。

尝试使用下划线,或删除空格并对数据库中的列进行相应的更改(如果这样做)。

您还应该考虑使用:

('" . $fname . "','" . $lname . "')

而不是('$fname','$lname')

我也在质疑这个=&gt; DEFINE ('DB_NAME', 'art database');

artdatabase之间有空格。如果是这种情况,实际上是您为数据库提供的名称,请将其重命名为art_database并改为使用DEFINE ('DB_NAME', 'art_database');

并使用以下内容进行额外保护:

$fname = mysqli_real_escape_string($dbcon,trim($_POST['fname']));
$lname = mysqli_real_escape_string($dbcon,trim($_POST['lname']));

阅读有关保护的有趣文章:


编辑:(选项)

选项1,共2个文件:

首先,将您的列重命名为firstnamelastname并使用以下代码并命名您的文件insert-data.php

数据库查询文件(insert-data.php)

<?php

if (isset($_POST['submit'])) {

    include('connect-mysql.php');

$fname = mysqli_real_escape_string($dbcon,trim($_POST['fname']));
$lname = mysqli_real_escape_string($dbcon,trim($_POST['lname']));

    $sqlinsert = "INSERT INTO `users` (firstname, lastname) VALUES ('" . $fname . "','" . $lname . "')";

    if (!mysqli_query($dbcon, $sqlinsert)) {
        die('error inserting new record');
        } //end of nested if

        echo "1 record added to the database";


} // end of the main if statement
?>

然后在单独的文件中,您的HTML表单;将其命名为db_form.php,例如:

HTML表单(db_form.php)

<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<hl>Insert Data into DB</hl>

<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true"/>
<fieldset>
    <legend>New People</legend>
    <label>First Name:<input type="text" name="fname" /></label>
    <label>Last Name:<input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" name="submit" value="add new person" />
</form>
</body>
</html>

新编辑 - 选项2,全部在一个文件中:

在一个页面中使用它,没有添加任何其他内容:

<?php

if (isset($_POST['submit'])) {

if(empty($_POST['fname'])) {
die("Fill in the first name field.");
}

if(empty($_POST['lname'])) {
die("Fill in the last name field.");
}

    include('connect-mysql.php');

$fname = mysqli_real_escape_string($dbcon,trim($_POST['fname']));
$lname = mysqli_real_escape_string($dbcon,trim($_POST['lname']));

    $sqlinsert = "INSERT INTO `users` (firstname, lastname) VALUES ('" . $fname . "','" . $lname . "')";

    if (!mysqli_query($dbcon, $sqlinsert)) {
        die('error inserting new record');
        } //end of nested if

        echo "1 record added to the database";


} // end of the main if statement
?>

<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>


<hl>Insert Data into DB</hl>

<form method="post" action="">
<fieldset>
    <legend>New People</legend>
    <label>First Name:<input type="text" name="fname" /></label>
    <label>Last Name:<input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" name="submit" value="add new person" />
</form>
<?php
echo $newrecord;
?>

</body>
</html>

答案 1 :(得分:0)

我做了一些改动,这对我来说很好 如果数据已经在数据库中,我可以忽略 你可以尝试这个

<?php

   if (isset($_POST['submit'])) {

  include('db.inc.php');

   $fname = mysqli_real_escape_string($dbcon,trim($_POST['fname']));
   $lname = mysqli_real_escape_string($dbcon,trim($_POST['lname']));

   // $sqlinsert = "INSERT INTO `user` (firstname, lastname) VALUES ('" . $fname . "','" . $lname . "')";
   $sqlinsert = "INSERT IGNORE INTO `dbname`.`user` (`fname`, `lname`) VALUES ( '$fname', '$lname')";

   if (!mysqli_query($dbcon, $sqlinsert)) {
    die('error inserting new record');
    } //end of nested if

    echo "1 record added to the database";


   } // end of the main if statement
   ?>

其中db.inc.php是用于连接数据库的同一目录中的不同文件

<?php

  $dbcon=mysqli_connect("localhost","dbuser","yourpassword","dbname");
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  ?>