【发布时间】:2017-07-19 23:06:21
【问题描述】:
我需要一些关于我的 php 脚本的帮助,并让它在 Wordpress 中运行。 我已经安装了一个可以运行我的 php 脚本的插件。但是当我在字段中输入数据并按提交时,表单会清除并且没有任何内容添加到数据库中。我哪里错了?
首先使用我的 HTML 如下:
<form method="post">
<table style="height: 149px;" width="220">
<tbody>
<tr>
<td style="width: 102px;">Medlemsnr:</td>
<td style="width: 102px;"><input name="stor_medlemsnr" type="smallint" />
</td>
</tr>
<tr>
<td style="width: 102px;">Navn:</td>
<td style="width: 102px;"><input name="stor_navn" type="text" /></td>
</tr>
<tr>
<td style="width: 102px;">Båd:</td>
<td style="width: 102px;"><input name="stor_baad" type="text" /></td>
</tr>
<tr>
<td style="width: 102px;">Dato:</td>
<td style="width: 102px;"><input name="stor_date" type="date" /></td>
</tr>
<tr>
<td style="width: 102px;">Art:</td>
<td style="width: 102px;"><input name="stor_art" type="text" /></td>
</tr>
<tr>
<td style="width: 102px;">Kg:</td>
<td style="width: 102px;"><input name="stor_kg" type="decimal(11,0)" /></td>
</tr>
</tbody>
</table>
<input type="Submit" />
[xyz-ips snippet="insert"]
上面的“插入”调用了下面的php:
$servername = "localhost";
$username = "******dk_user";
$password = "********";
$dbname = "******dk_storfanger2017";
$con = mysql_connect(localhost,$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db( $dbname, $con);
$sql="INSERT INTO `******dk_storfanger2017`.`stor_2017` (`2017_medlemsnr`,
`2017_navn`, `2017_dato`, `2017_baad`, `2017_art`, `2017_kg`)
VALUES($_POST[stor_medlemsnr],$_POST[stor_navn],$_POST[stor_baad],
$_POST[stor_date],$_POST[stor_art],$_POST[stor_kg])";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
我希望有人能够为我指出正确的方向,让它发挥作用。
我也尝试将它构建到 insert.php 中,但 wordpress 不会接受该页面,并在尝试访问它时给我一个 HTTP 500 错误。
【问题讨论】:
-
对于初学者来说,使用 PDO 和准备好的语句 php.net/manual/en/pdo.prepare.php 您当前的方式,即使您的格式正确,也是不安全的,并且容易受到 sql 注入的影响
-
我会尝试,但因为它对我来说相当新。我不完全确定如何处理它,让它发挥作用。