【问题标题】:using a table name by php in a submit form在提交表单中使用 php 的表名
【发布时间】:2013-10-29 02:52:55
【问题描述】:

我希望用户在 mysql 数据库中选择一个表名,然后我可以通过字段内表单的文本框添加数据。 我使用 PHP $_POST[ ] 将表名发送到添加表单。但以添加形式;当我提交添加按钮时;表名已丢失。 我进行了很多搜索并测试了会话和常量来保存表名值;但 没有得到想要的结果。

任何帮助将不胜感激。

这里是选择表名的页面代码:

<form action="change_products.php" method="post" name="select_table">
<select name="edit_products" >
<option <?php if($_POST['edit_products'] == "cabin") echo "selected='selected'"; ?> value="cabin">Cabin case</option>
<option <?php if($_POST['edit_products'] == "cabin_door") echo "selected='selected'"; ?>   value="cabin_door">Cabin door</option>
</select>
<input type="submit" value="OK">
</form>

这是“change_products.php”页面中添加表单的代码。

<?
include('functions.php');  //the database info
define('selected_table', $_POST['edit_products']);
?>

<form method="post">

<table>

    <tr>
    <td>Mark:</td>
    <td><input type="text" name="mark" /></td>
    </tr>


    <tr>    
    <td><input type="submit" name="add" value="New" /></td>
    </tr>
</table>




<?
if (isset($_POST['add']))
{
$mark=$_POST['mark'];
mysql_query("INSERT INTO ".selected_table." (mark) VALUES ('$mark')"); 
}
?>

</form>

【问题讨论】:

  • 旁注: 停止使用已弃用的 mysql_* 函数。请改用 MySQLi 或 PDO。
  • 旁注:您的代码受到 SQL 注入攻击,因为您直接允许在查询中插入 POST 值。
  • 这是非常危险的错误代码。永远,永远将未转义的 $_GET$_POST 数据直接放在查询中。
  • @ShivanRaptor;我尝试使用if(isset($_POST['add'])) 来运行查询;但主要问题是单击添加按钮会丢失表名。 MySQLi 或 PDO 是否适用于解决问题?你能指导我更多吗?在此先感谢...
  • MySQL 、 MySQLi 、 PDO 是 MySQL 数据库的 PHP 库。它们的工作方式相似。后两者提供了比不推荐使用的第一个更多的功能。因此,它们不适用于解决问题。

标签: mysql post tablename


【解决方案1】:

您没有设置$_POST['add'],因此change_products.php 中的最后一部分PHP 代码不会运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2018-04-03
    • 2012-01-10
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多