【问题标题】:$_POST merging array and storing in only one column$_POST 合并数组并仅存储在一列中
【发布时间】:2012-12-19 22:49:12
【问题描述】:

我有一个表单,它发布到一个 php 处理程序,它将表单中的数据保存到数据库中。 我认为保存功能不是问题。我认为这是$_POST 合并表单数据的问题。当我查看 sql 表时,数据在那里,但所有值都出现在一列中,因此变量从表单中正确传递。

这是我的代码:

$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$com_dis = isset($_POST['comment']) ? $_POST['comment'] : '';
$id = isset($_POST['id']) ? $_POST['id'] : '';

$lowercase = strtolower($email);
$image = md5( $lowercase );

try{
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = 'INSERT INTO comments ( com_name, com_email, com_dis, post_id_fk ) VALUES ( :name, :email, :com_dis, :id )';
    $st = $conn->prepare ( $sql );
$st->bindValue( ":name", $name, PDO::PARAM_STR );
    $st->bindValue( ":email", $email, PDO::PARAM_STR );
    $st->bindValue( ":com_dis", $com_dis, PDO::PARAM_STR );
    $st->bindValue( ":id", $id, PDO::PARAM_INT );
    $st->execute();
    $conn = null;
}catch(PDOException $e ){
    echo "QUERY FAILED" . $e->getMessage();
};

如果我对所有变量都使用$id = $_POST['id'],则会引发未识别的索引错误。当我打印$_POST 时,它会显示一个数组,其中该数组称为名称,其内容只是其余变量,它们与存储在表中的数据相同。

如何让数据正确保存或阻止$_POST 合并变量?保存在正确的列中?

这是我的表格:

<form action="#" method="post">
    <input type="hidden" name="id" id="id" value="<?php echo $id; ?>"/><br />
    <span class="titles">Name</span><span class="star">*</span><br /><input type="text" name="name" id="name"/><br />

    <span class="titles">Email</span><span class="star">*</span><br /><input type="text" name="email" id="email"/><br />

    Comment<br /><textarea name="comment" id="comment"></textarea><br />

    <input type="submit" class="submit" value=" Submit Comment " />
</form>

表单通过 AJAX 传递:

$(function() {

    $(".submit").click(function() {

        name = $("#name").val();
        var email = $("#email").val();
        var comment = $("#comment").val();
        var id = $("#id").val();
        var dataString = 'name='+ name + 'email=' + email + 'comment=' + comment + 'id=' + id;

        if(name=='' || email=='' || comment==''){
            alert('Please Give Valid Details');
        }

        else{
            $("#flash").show();
            $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle">&nbsp;<span class="loading">Loading Comment...</span>');

            $.ajax({
                type: "POST",
                url: "commentajax.php",
                data: dataString,
                cache: false,
                success: function(html){

                    $("ol#update").append(html);
                    $("ol#update li:last").fadeIn("slow");
                    document.getElementById('email').value='';
                    document.getElementById('name').value='';
                    document.getElementById('comment').value='';
                    $("#name").focus();

                    $("#flash").hide();
            }
        });
    }
return false;
});

【问题讨论】:

  • 如果您认为这是问题所在,如何显示var_dump($_POST) 显示的内容?我们无法猜测您的表单是什么样子,或者数据是如何传递的。
  • 你是什么意思它只保存在一列中 - 只保存一列或所有值分别出现在一列而不是它们自己的列中?如果只保存一列,是哪一列?
  • 当我转储变量时,结果是 'array(1) { ["name"]=> string(28) "asdemail=asdcomment=asdid=38" }'
  • 所有值都出现在一列中
  • 你的ajax怎么样?可以粘贴一下吗

标签: php ajax database


【解决方案1】:

你需要连接你的数据字符串:

var dataString = 'name='+ name + 'email=' + email + 'comment=' + comment + 'id=' + id;

应该是

var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment + '&id=' + id;

【讨论】:

  • 哦,看在上帝的份上,我刚刚浪费了 4 个小时的时间来寻找问题,结果发现我找错了树。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 2013-11-16
  • 1970-01-01
  • 2012-09-23
  • 2022-11-30
  • 1970-01-01
相关资源
最近更新 更多