在同一页面中通过html表单将数据插入数据库

时间:2014-04-23 07:28:43

标签: php html mysql

<!doctype html>
<html>
<head>
<title>Lab03</title>
</head>
<form id="signin" action="lab_03.php" method="post">
Name: <input type="text" name="name">
<br />
First Name: <input type="text" name="fn">
<br />
SID: <input type="text" name="sid">
<br />
Email Address: <input type="text" name="email">
<input type="submit" value="Submit">
</form>

<?php
include ("connection.php");

mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES ($POST_[name], $POST_[fn], $POST_[sid], $POST_[email]");



?>
<body>
</body>
</html>

我想通过html表单将数据插入数据库。但我不想制作另一个文件来插入数据。我上面的代码给了我以下错误。 enter image description here

7 个答案:

答案 0 :(得分:2)

将您的查询部分更改为此部分:

mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES ('".$_POST['name']."', '".$_POST['fn']."',". $_POST['sid'].", '".$_POST['email']."'");

答案 1 :(得分:2)

您的查询应该是这样的:

mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES ('".$_POST['name']."', '".$_POST['fn']."',". $_POST['sid'].", '".$_POST['email']."'");

答案 2 :(得分:0)

这是一个很好的方法:

mysqli_query(
    $con,
    "INSERT INTO lab_03 (
        name, 
        fname, 
        sid, 
        email
    ) 
    VALUES (
        '{$_POST['name']}',
        '{$_POST['fn']}',
        '{$_POST['sid']}',
        '{$_POST['email']}'
    "
);

要确保它有效,请删除{$_POST['something']}周围的单引号,如果数据库中的字段是整数(或其他任何不需要引号的字段)。

另外,请记住,目前您的代码很容易受到SQL注入的攻击,因为您没有对输入数据进行清理。请查看this question,了解如何预防。

答案 3 :(得分:0)

使用此answer作为参考,我想指出代码中的一个主要缺陷。

您需要检查$_POST变量是否存在,否则它仍然会抛出错误。

这样说:

if(isset($_POST['name'])) {
    mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES ('".$_POST['name']."', '".$_POST['fn']."',". $_POST['sid'].", '".$_POST['email']."'");
}

另外,我建议您在表单前调用PHP代码,这样就可以了。

答案 4 :(得分:0)

用这种方式,你的错误不会出现。

<!doctype html>
<html>
<head><title>Lab03</title></head>
<form id="signin" action="" method="post">
Name: <input type="text" name="name">
<br />
First Name: <input type="text" name="fn">
<br />
SID: <input type="text" name="sid">
<br />
Email Address: <input type="text" name="email">
<input type="submit" value="Submit">
</form>

<?php
if(isset($_POST)) {
include ("connection.php");
mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES ('".$_POST['name']."', '".$_POST['fn']."', '".$_POST['sid']."', '".$_POST['email']."'");
}


?>
<body>
</body>
</html>

答案 5 :(得分:0)

使用此:

<!doctype html>
<html>
<head>
  <title>Lab03</title>
</head>
<body>
  <form id="signin" action="lab_03.php" method="post">
    Name: <input type="text" name="name"><br />
    First Name: <input type="text" name="fn"><br />
    SID: <input type="text" name="sid"><br />
    Email Address: <input type="text" name="email">
    <input type="submit" value="Submit" name="submit">
  </form>
<?php
  if (isset($_POST['submit'])) {
    include ("connection.php");
    $con = mysqli_connection('server', 'user', 'password', 'db');
    if (mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES  ({$_POST['name']}, {$_POST['fn']}, {$POST['sid']}, {$_POST[email]}") === true) {
      echo "OK";
    }
  }

?>
</body>
</html>

答案 6 :(得分:0)

将所有PHP代码放在HTML之上,并且您使用了错误的变量来获取POST值。它应该是$_POST而不是$POST_

最好使用mysqli_real_escape_string来转义可能位于POST数据值中的特殊字符

<?php
include ("connection.php");

mysqli_query($con,"INSERT INTO lab_03 (name, fname, sid, email) VALUES ('".mysqli_real_escape_string($con, $_POST['name'])."', '".mysqli_real_escape_string($con, $_POST['fn'])."', '".mysqli_real_escape_string($con, $_POST['sid'])."', '".mysqli_real_escape_string($con, $_POST['email'])."'");
?>
相关问题