使用PHP将表单中的数据插入SQLite3数据库

3

我希望创建一个注册表单,然后将数据发布到SQLite数据库文件中。

我的表单如下所示:

<form action="registerprocess_test.php" class="form-horizontal" id=
"register_form" method="post" name="register_form" role="form">
    <h2>Registration Form</h2>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="firstname">First
        Name</label>
        <div class="col-sm-6">
            <input autofocus="" class="form-control" id="firstname" name=
            "firstname" placeholder="First Name" type="text">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="lastname">Last
        Name</label>
        <div class="col-sm-6">
            <input autofocus="" class="form-control" id="lastname" name=
            "lastname" placeholder="Last Name" type="text">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="email">Username</label>
        <div class="col-sm-6">
            <input class="form-control" id="username" name="username"
            placeholder="Username" type="text">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for=
        "password">Password</label>
        <div class="col-sm-6">
            <input class="form-control" id="password" name="password"
            placeholder="Password" type="password">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="country">Country</label>
        <div class="col-sm-6">
            <select class="form-control" id="country" name="country">
                <option>
                    United Kingdom
                </option>
                <option>
                    United States
                </option>
            </select>
        </div>
    </div><!-- /.form-group -->
    <div class="form-group">
        <label class="control-label col-sm-3">Gender</label>
        <div class="col-sm-6">
            <div class="row">
                <div class="col-sm-4">
                    <label class="radio-inline"><input id="femaleRadio"
                    type="radio" value="Female">Female</label>
                </div>
                <div class="col-sm-4">
                    <label class="radio-inline"><input id="maleRadio" name=
                    "gender" type="radio" value="Male">Male</label>
                </div>
            </div>
        </div>
    </div><!-- /.form-group -->
    <div class="form-group">
        <div class="col-sm-6">
            <div class="checkbox"></div>
        </div><!-- /.form-group -->
        <div class="form-group">
            <div class="col-sm-6 col-sm-offset-3">
                <div class="checkbox">
                    <label><input type="checkbox">I accept <a href=
                    "#">Terms & Conditions</a></label>
                </div>
            </div>
        </div><!-- /.form-group -->
        <div class="form-group">
            <div class="col-sm-6 col-sm-offset-3">
                <button class="btn btn-primary btn-block" type=
                "submit">Register</button>
            </div>
        </div>
    </div>
</form>

那么我的PHP代码看起来像这样:
 <?php
  try
{
//open the database
$db = new PDO('sqlite:users.db');


$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$username = $_POST["username"];
$password = $_POST["password"];
$gender = $_POST["gender"];
$country = $_POST["country"];


//Insert record  

$db->exec("INSERT INTO registered_users (firstname, lastname, username, password, gender, country) VALUES ('$firstname', '$lastname', '$username', '$password', '$gender', $country);");

//now output the data to a simple html table...
 print "<table border=1>";
 print "<tr><td>firstname</td><td>lastname</td><td>username</td><td>password</td><td>gender</td><td>country</td></tr>";
$result = $db->query('SELECT * FROM registered_users');
foreach($result as $row)
{
  print "<tr><td>".$row['firstname']."</td>";
  print "<td>".$row['lastname']."</td>";
  print "<td>".$row['username']."</td>";
  print "<td>".$row['password']."</td>";
  print "<td>".$row['gender']."</td>";
  print "<td>".$row['country']."</td>";
}
print "</table>";

$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : ' .$e->getMessage();
}

?>

我知道我已经连接到了数据库,因为在点击提交后,它将数据库中的当前数据显示在表格中。然而,它没有从注册表单中插入数据。我的php代码中是否缺少重要的内容?


我是否也嗅到了SQL注入的味道? - Erwin Moller
不要对查询字符串使用字符串操作!这是一个巨大的安全风险!请改用预处理语句和查询参数。 - Colonel Thirty Two
你的输入也被分成了不同的行,这可能会产生负面影响。 - Funk Forty Niner
谢谢。我按照所述将$country)更改为'$country')");,但仍然没有成功。 - Alex Anderson
如何使用PDO捕获错误? - Alex Anderson
显示剩余7条评论
1个回答

0

我做了一些错误检查,原因是我在表格中有另一个列没有插入。所以我从数据库中删除了这个列,现在它可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接