3

我必须通过“表单发布”发布一个值并将其插入表中,这是我在两个文件中的代码:

<html>
<body>
<table>
<form  enctype="multipart/form-data" action="<?php $_SERVER["DOCUMENT_ROOT"] ?>  /contents/ad_posting_process_4.php" method="post">
<?php $cat_no = "101010"; ?>
<input type=hidden id="category" value=" <?php echo $cat_no; ?> ">  
<tr> <td>Sub Category: </td><td>    <input type=text id="sub_category" > </td>
<tr><td></td> <td><input type="submit" name="action" value="Post"></td></tr></tr>
</form>
</body></html>

这里是 ad_posting_4.php

<?php session_start();
include($_SERVER["DOCUMENT_ROOT"]."/includes/conn.php");
$category = mysql_real_escape_string($_POST['category']);
$sub_category = mysql_real_escape_string($_POST['sub_category']);
echo "category=". $category; 
echo "sub_category=". $sub_category; ?>

没有通过邮寄发送的价值。

我哪里错了?

问候:

4

3 回答 3

9

您需要使用该name属性:

<input type="text" name="category" />
<input type="text" name="sub_category" />
于 2012-10-04T14:19:44.993 回答
7

输入type需要用引号括起来'并且还具有name属性,而不是id.

<input type='hidden' name="category" value=" <?php echo $cat_no; ?> " />  
<tr> <td>Sub Category: </td>
<td><input type='text' name="sub_category" > </td>
于 2012-10-04T14:18:47.373 回答
0

我最近在自己的网站上做了一些非常相似的事情,并得到了这个社区的帮助。在 HTML 方面,我创建了一个标准表单并给每个输入一个“名称”。例如,假设您正在尝试捕获城市和州:

<html>
<body>
<form>
<tr>
<td>State: </td><td> <input type="text" style="border:1px solid #000000" name="state" /></td>
<td>City</td><td><input type="text" style="border:1px solid #000000" name="city" /></td>
</tr>
</form>
</body>
</html>

然后设置一个 mySQL 数据库,其中包含一个名为“state”的列和一个名为“city”的列。接下来,使用 PHP 将表单中的数据插入到数据库中。我是 PHP 新手,但据我了解,使用 PDO 比使用旧的 mysql 命令更安全。

$dbtype     = "mysql";
$dbhost         = "localhost";
$dbname     = "name";
$dbuser     = "user";
$dbpass     = "pass";

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '[Insert Name of your table here]'";
$q = $conn->prepare($sql);
$q->execute();
$columns = $q->fetchAll(PDO::FETCH_COLUMN, 0);

$cols = array();
foreach ($_POST as $key=>$value)
{
    // if a field is passed in that doesn't exist in the table, remove it.  The name of the input that is removed will be echoed so you can debug.  Remove echo if you go to production.
    if (!in_array($key, $columns)) {
        unset($_POST[$key]);
echo $key;
    }
}
$cols = array_keys($_POST);
$sql = "INSERT INTO Facilities(". implode(", ", $cols) .") VALUES (:". implode(", :", $cols) .")";
$q = $conn->prepare($sql);
array_walk($_POST, "addColons");
$q->execute($_POST);

function addColons($value, &$key)
{
    $key = ":{$key}";
}

这对我来说效果很好。请注意,它只能匹配具有完全相同名称的列的 HTML 表单输入。就我而言,我想创建 100 多个输入,所以这更容易。如果您正在处理 5-10,则手动插入特定变量可能会更容易。

于 2012-10-04T14:49:48.590 回答