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



我一直在寻找问题的答案,由于所有编程都不尽相同,我似乎无法解决我的问题。我创建了一个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>

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

INSERT INTO users (first name, last name)

用这样的反勾号包裹它们:

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

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

尝试使用下划线,或者删除空格,并对数据库中的列进行适当的更改,如果你这样做的话。

您还应该考虑使用:

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

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

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

在CCD_ 4和CCD_。如果是这种情况,并且实际上是您为DB指定的名称,请将其重命名为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']));

有趣的文章阅读保护:

  • 如何防止PHP中的SQL注入

编辑:(选项)

选项1,在2个文件中:

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

DB查询文件(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>

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

<?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();
  }
  ?>

相关内容

  • 没有找到相关文章

最新更新