这是我想要做的。
我有一个 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
我知道我可以继续将索引中的变量传递给提交的信息,然后传递给获取信息,但我正试图掌握连接到数据库的方法。
我想让用户输入数据,将数据发送到另一个页面,然后将该数据放入表中,然后重定向到新页面,并将表中的信息转储回变量中,以便我可以将它们放在屏幕上。