【问题标题】:Connecting to MySQL(WAMP) database with PHP使用 PHP 连接到 MySQL(WAMP) 数据库
【发布时间】: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


【解决方案1】:

用双引号代替单引号将您的语句括起来。

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

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

 整个ext/mysql PHP 扩展,它提供了以mysql_ 为前缀的所有函数,是officially deprecated as of PHP v5.5.0,将来会被删除。考虑切换到mysqliPDO

【讨论】:

  • 同时有两个相同的答案。好笑。
  • 在您使用 Amal 的答案之后,请考虑一下如何保护您的脚本。现在它很弱。
  • 新错误。注意:未定义变量:第 5 行 C:\Program Files\wamp\www\testwebpage\Model\getinformation.php 中的人员
  • @jordankoal:你在做$fname = $people['fname'];。你需要$fname = $_POST['fname'];。对所有类似的语句都这样做。
  • 我还是有点新,但是当我没有从数据库以外的任何地方获取 $_POST 数据时,为什么还要使用它呢?那个页面是用来打开数据库,获取people表的fname字段中的记录。
【解决方案2】:

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

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

【讨论】:

  • 同时有两个相同的答案。好笑。
【解决方案3】:

我建议您使用 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

希望对你有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2010-10-18
    • 2011-02-11
    相关资源
    最近更新 更多