1

这是我想要做的。

我有一个 index.php 页面,它接受用户输入,然后将其传输到 infosubmitted.php

    <?php
include('header.php');
?>

<body>
<form action="infosubmitted.php" method="post" id="infosubmitted">
<div id="header">
<h1><strong>Enter In The Data</h1></strong>
</div>
<div id="main">
<table border="0" width="75%">
        <tr>
            <td align="right" width="10%">First Name: </td>
            <td><input type="text" name="fname" id="fname" size="20" /></td>
        </tr>
        <tr>
        <td align="right" width="10%">Last Name: </td>
            <td><input type="text" name="lname" id="lname" size="20" /></td>
        </tr>
        <tr>
        <td align="right" width="10%">Age: </td>
            <td><input type="text" name="age" id="age" size="3" /></td>
        </tr>
        <tr>
        <td align="right" width="10%">City: </td>
            <td><input type="text" name="city" id="city" size="20" /></td>
        </tr>
</table>
<input type="submit" />
</div>
</body>

<?php
include('footer.php');
?>

现在,我正在尝试获取变量并将它们存储在名为 test 的数据库中名为 people 的表中

这是 dbconnect.php

<?php
// Set the database access information as constants 

define('DB_USER', 'root');
define('DB_PASSWORD', 'phpmyadmin');
define('DB_HOST', 'localhost');
define('DB_NAME', 'test');

?>

这是model.php

<?php
// model for the totals of the wreath orders
require_once("dbconnect.php");

// connect to database and check errors
@ $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$connection_error = $dbc->connect_error;
if ($connection_error != null) {
  echo "<p>Error connectiong to database: $connection_error</p>";
  exit();
}
?>

这是 infosubmitted.php

<?php
require_once("model.php");


$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$city = $_POST['city'];

insertIntoTable($fname, $lname, $age, $city);



function insertIntoTable($fname, $lname, $age, $city){
global $dbc;

$insertRow = 'INSERT INTO people(fname, lname, age, city) VALUES('$fname', '$lname', '$age', '$city')';
$result = $dbc->query($insertRow);
header('location: getinformation.php');

}
?>

然后我希望它重定向到 getinformation.php 以将记录从数据库中取回,以便我可以将其存储回变量中。

<?php
include('model.php');
include('header.php');

$fname = $people['fname'];
$lname = $people['lname'];
$age = $people['age'];
$city = $people['city'];
$dbc->close();
?>
<div id="header">
<h1><strong>This is the information you submitted</h1></strong>
</div>
<div id="main">
<table>
    <tr>
        <td>First Name:</td>
        <td><?php echo $fname; ?></td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td><?php echo $lname; ?></td>
    </tr>
    <tr>
        <td>Age:</td>
        <td><?php echo $age; ?></td>
    </tr>
    <tr>
        <td>City:</td>
        <td><?php echo $city; ?></td>
    </tr>
</table>
</div>
<?php
include('footer.php');
?>

我得到错误

解析错误:语法错误,第 17 行 C:\Program Files\wamp\www\testwebpage\Model\infosubmitted.php 中的意外 T_VARIABLE

我知道我可以继续将索引中的变量传递给提交的信息,然后传递给获取信息,但我正试图掌握连接到数据库的方法。

我想让用户输入数据,将数据发送到另一个页面,然后将该数据放入表中,然后重定向到新页面,并将表中的信息转储回变量中,以便我可以将它们放在屏幕上。

4

3 回答 3

1

用双引号而不是单引号将您的语句括起来。

$insertRow = "INSERT INTO people (fname, lname, age, city)
VALUES('$fname', '$lname', '$age', '$city')";

在当前状态下,您的查询未转义并且容易受到 SQL 注入的攻击。您应该考虑使用mysql_real_escape_string()或使用类似方法来逃避它们。

ext/mysql提供所有以前缀命名的函数 的整个PHP 扩展mysql_从 PHP v5.5.0 开始正式弃用,并将在未来被删除。考虑切换到mysqliPDO

于 2013-07-18T13:59:51.530 回答
-1

我要说您的插入代码中有引号错误。使用转义、双引号或其他一些机制将带引号的变量与带引号的字符串分开,如下所示:

$insertRow = "INSERT INTO people(fname, lname, age, city) VALUES('$fname', '$lname', '$age', '$city')";
于 2013-07-18T13:59:51.180 回答
-2

我建议你使用 PDO 连接数据库并执行语句。您永远不会担心您的问题,因为可以在执行之前准备好语句。

在此处查看 PDO 手册http://php.net/manual/en/book.pdo.php

如果您想知道在 PDO 和 MySQLi 之间应该选择哪一个。请阅读以下摘要:

http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

希望对您有所帮助!

于 2014-10-04T04:51:22.130 回答