Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $\u已填充POST变量,但mysql数据库中未发布任何内容_Php_Mysql - Fatal编程技术网

Php $\u已填充POST变量,但mysql数据库中未发布任何内容

Php $\u已填充POST变量,但mysql数据库中未发布任何内容,php,mysql,Php,Mysql,我有一个带有文件上传部分的基本表单。当我提交表格时,没有向DB提交任何内容。当我使用x-debug进行调试时,我可以看到$\u POST变量都已填充且正确无误 表格如下: <form id="classifiedsForm" enctype="multipart/form-data" action="{$self}" method="post" autocomplete="off"> <fieldset> <label>

我有一个带有文件上传部分的基本表单。当我提交表格时,没有向DB提交任何内容。当我使用x-debug进行调试时,我可以看到$\u 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>
和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
。您应该会从中得到一个错误(
变量的数量与准备语句中的参数数量不匹配

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

echo$e->getMessage()


这会让你立即找到这个解决方案。

夏洛克是正确的,但如果我能添加一些东西,可能会让你的生活更轻松。。。。您可以直接将数组传递给execute调用,而不是单独绑定每个变量。如果您将表单元素命名为与db COL相同的名称,则可以执行以下操作:

//make list of allowed POST form fields/db columns with matching names
$allowable_vars = array(
     'headline'
    ,'description'
    ,'contact'
    ,'buySell'
    ,'category'
);

//array flip turns values into keys
//array intersect key removes any POST values not in the allowed list
//but keeps the values of POST for the keys that do match

$post_vals = array_intersect_key($_POST,array_flip($allowable_vars));

//add any values not coming from POST or that do not match for some reason
$post_vals['user_id'] = $userID;

//prepare as normally would
$s = $pdo->prepare($sql);

//bind and execute at same time
$s->execute($post_vals);

列占位符前的冒号仅在查询中需要,在绑定调用命名中不需要。。。如果您添加了新的表单字段,使用允许的键可以使维护更容易地添加新列,因为您不再需要跟踪单个绑定。

请执行一些基本调试。所有的值是什么,IF是否有效,查询是否执行,是否回显查询并检查其生成内容,运行时是否出现错误,等等。您现在只是转储所有代码并说,“帮助,不起作用,不想投入精力”
//make list of allowed POST form fields/db columns with matching names
$allowable_vars = array(
     'headline'
    ,'description'
    ,'contact'
    ,'buySell'
    ,'category'
);

//array flip turns values into keys
//array intersect key removes any POST values not in the allowed list
//but keeps the values of POST for the keys that do match

$post_vals = array_intersect_key($_POST,array_flip($allowable_vars));

//add any values not coming from POST or that do not match for some reason
$post_vals['user_id'] = $userID;

//prepare as normally would
$s = $pdo->prepare($sql);

//bind and execute at same time
$s->execute($post_vals);