以下是我要做的。
我有一个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');
?>
这是型号.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
我知道我可以不断地将变量从索引传递到提交的信息,然后再传递到getinformation,但我正在努力掌握连接数据库的方法。
我想让用户输入数据,将数据发送到另一个页面,然后将数据放入表中,然后重定向到新页面,并将表中的信息转储回变量中,这样我就可以将它们放在屏幕上。
用双引号而不是单引号包装您的语句。
$insertRow = "INSERT INTO people (fname, lname, age, city)
VALUES('$fname', '$lname', '$age', '$city')";
在它的当前状态下,您的查询是未跳过的,并且容易受到SQL注入的攻击。您应该考虑使用mysql_real_escape_string()
或使用类似的方法对它们进行转义。
整个ext/mysql
PHP扩展提供了所有以前缀mysql_
命名的函数,从PHP v.5.0起,它被正式弃用,并将在将来被删除。考虑切换到mysqli或PDO
我要说的是,您的插入代码中有一个引号错误。使用转义符、双引号或其他机制将带引号的变量与带引号的字符串分开,如下所示:
$insertRow = "INSERT INTO people(fname, lname, age, city) VALUES('$fname', '$lname', '$age', '$city')";
我建议您使用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
希望对你有所帮助!