php - 填充了 $_POST 变量,但没有在 mysql 数据库中发布

标签 php mysql

我有一个带有文件上传部分的基本表单。当我提交表单时,没有任何内容提交给数据库。当我使用 x-debug 进行调试时,我可以看到 $_POST 变量都已填充且正确。

这是表格:

<form id="classifiedsForm" enctype="multipart/form-data" action="{$self}" method="post" autocomplete="off">
        <fieldset>
            <label>Basic Details</label>
            <section>
                <label for="headline">Headline</label>
                <div><input type="text" id="headline"  name="headline" required title="A headline for your ad">
                </div>
            </section>
            <section><label for="img">Add an image<br><span>Image should be 300x300px and jpg or png. Don't worry. We do the curvy corners thing.</span></label>
                <div>
                    <input type="file" id="img" name="img">
                </div>
            </section>
            <section>
                <label for="description">Description</label>
                <div><input type="text" id="description"  name="description" required title="A description for your ad">
                </div>
            </section>
            <section>
                <label for="contact">Contact</label>
                <div><input type="text" id="contact"  name="contact" required title="A contact email address">
                </div>
            </section>
            <section>
                <label for="category">Category</label>
                <div>
                    <select name="category" id="country">
                        <optgroup label="Category">
                            {foreach item=c from=$categories}
                                <option name="category" value="{$c.name}">{$c.name}</option>
                            {/foreach}
                        </optgroup>
                    </select>
                </div>
            </section>
            <section>
                <label for="buySell">Sign up to newsletter?</label>
                <div>
                    <input type="radio" id="yes_radio" name="buySell" value="1"><label>Buy</label>
                    <input type="radio" id="no_radio" name="buySell" value="0"><label>Sell</label>
                </div>
            </section>
            <section>
                <div>
                    <button name="submit" class="submit" value="update" type="submit">Update</button>
                </div>
            </section>
        </fieldset>
    </form>

这是 Controller :

include '../common.php';

session_start();

$userID = $_SESSION['email']['id'];


if(empty($_SESSION['email']))
{
header("Location: ../login.php");
die("Redirecting to login.php");
}

$title = 'Your Profile';


//CATEGORIES QUERY

try
{
$sql = "SELECT * FROM `categories` ORDER BY `name` ASC";

$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching classifieds: ' . $e->getMessage();
include '../includes/error.html.php';
exit();
}

foreach ($result as $row)
{
$categories[] = array(
    'id' => $row['id'],
    'name' => $row['name']);
}

if ($_SERVER['REQUEST_METHOD'] == "POST"){
try
{
    $sql = "INSERT INTO `classifieds` SET
    `headline` = :headline,
    `description` = :description,
    `contact` = :contact,
    `buySell` = :buySell,
    `category` = :category,
    `user_id` = $userID";

    $s = $pdo->prepare($sql);
    $s->bindValue(':headline', $_POST['headline']);
    $s->bindValue(':description', $_POST['description']);
    $s->bindValue(':contact', $_POST['contact']);
    $s->bindValue(':buySell', $_POST['buySell']);
    $s->bindValue(':category',$_POST['category']);
    $s->bindValue(':userID', $userID);
    $s->execute();
}
catch (PDOException $e)
{
    $error = 'Error adding advert.';
    include '../includes/error.html.php';
    exit();
 }
}

$smarty->assign('title', $title);
$smarty->assign('categories', $categories);
$smarty->assign('userID', $userID);
$smarty->display('add-classifieds.tpl');

和 mysql 表:

CREATE TABLE `classifieds` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `headline` varchar(255) NOT NULL,
  `img` varchar(255) DEFAULT NULL,
  `description` varchar(255) NOT NULL,
  `contact` varchar(255) NOT NULL,
  `buySell` int(1) NOT NULL,
  `category` varchar(255) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1;

非常感谢您对此提供的任何帮助。谢谢。

最佳答案

$sql = "INSERT INTO `classifieds` SET
`headline` = :headline,
`description` = :description,
`contact` = :contact,
`buySell` = :buySell,
`category` = :category,
`user_id` = $userID";

最后一行不正确,应该是:userID。不过你应该会得到一个错误(Number of variables doesn't match number of parameters in prepared statement)。

在开发模式下,您应该回显异常消息:

echo $e->getMessage();

这会立即引导您找到这个解决方案。

关于php - 填充了 $_POST 变量,但没有在 mysql 数据库中发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16394793/

相关文章:

php - PHP 密码的安全散列和盐

php - 无法通过 php 表单注册新用户

php - 在 MySQL 中格式化日期

php - 如何检查 preg_match() 是否为 False 而不是 True?

php - 使用 GrumPHP 运行 PHP CS 修复程序

MySQL 数据透视表/交叉表

mysql - 如何在VB.NET中等待MySQL更新?

mysql - 我应该将字段 PRICE 作为 int 还是作为 float int 存储在数据库中?

php - 在 Laravel 中编辑具有多对多关系的表

php - 存储查询结果的记录ID以便进一步浏览?