【发布时间】:2013-07-18 13:51:42
【问题描述】:
这就是我想要做的。
我有一个 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
我知道我可以继续将变量从索引传递到提交的信息,然后传递到获取信息,但我正试图掌握连接到数据库的方法。
我想让用户输入数据,将数据发送到另一个页面,然后将该数据放入表中,然后重定向到新页面,并将表中的信息转储回变量中,以便我可以将它们放在屏幕。
【问题讨论】:
-
所有文件都位于同一个文件夹中。
-
在编写更多 SQL 代码之前,您必须阅读 proper SQL escaping 以避免严重的 SQL injection bugs。您应该从不直接在查询字符串中包含用户数据。使用parameterized queries 是每次都正确执行此操作的最安全方法。
-
你似乎是 PHP 新手。我建议您在开始编码之前正确学习 PHP。
-
@tadman 好的,我会继续阅读的
-
就像我说的,这只是一个测试。这不是现场直播。这仅供我使用。我知道在服务器和页面上进行验证,但我只是没有在此示例中设置验证。我是 PHP 的新手,我们没有在我的课程中广泛介绍它。我感谢所有的答案。
标签: php mysql database wamp wampserver