【问题标题】:PDO not insert some values but insert othersPDO 不插入某些值,而是插入其他值
【发布时间】:2017-04-24 19:31:44
【问题描述】:

这就是我所拥有的

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php 

include_once("../conexao.php");
include_once("../check_session.php");
include_once("../set.php");
$username = $_SESSION["username"];


 $profileimage =  $username.'.png';
 //echo $profileimage;

//assign post vars 

$post_name = $_POST['loginname'];
$post_surname = $_POST['loginsurname'];
$post_description = $_POST['profiledescription'];
$post_local = $_POST['local'];
$post_streetnumber = $_POST['streetnumber'];
$post_streetaddress = $_POST['streetaddress'];
$post_city = $_POST['city'];
$post_state = $_POST['state'];
$post_postalcode = $_POST['postalcode'];
$post_country = $_POST['country'];
$post_addressreference = $_POST['addressreference'];

//variavel $conexao_pdo

$sql1 = "SELECT * FROM user WHERE username = :usr";

                    //db column and value
        $stmt1 = $conexao_pdo->prepare($sql1);  
        //where clause                                 
        $stmt1->bindParam(':usr', $username);
        $stmt1->execute();  
        while ($linha = $stmt1->fetch(PDO::FETCH_OBJ)) {
  //echo $linha->name . ' - ' . $linha->email;
  $col_name = $linha->name;
  $col_surname = $linha->surname;
  //$col_email = $linha->email;
  $col_description = $linha->description;
  //$col_profileimage = $linha->profileimage;
  $col_local = $linha->local;
  $col_streetnumber = $linha->streetnumber;
  $col_streetaddress = $linha->streetaddress;
  $col_city = $linha->city;
  $col_state = $linha->state;
  $col_postalcode = $linha->postalcode;
  $col_country = $linha->country;
  $col_addressreference = $linha->addressreference;
}

$stringsize = strlen($post_description);

if(empty($post_name)){ $name = $col_name;  } else { $name = $post_name; }
if(empty($post_surname)){ $surname = $col_surname;  } else { $surname = $post_surname; }


if(!empty($post_description) && ($stringsize <= 150)){ $description = $post_description;  } else { $description = $col_description; }



if(empty($post_local)){ $local =  $col_local;  } else { $local = $post_local; }
if(empty($post_streetnumber)){ $streetnumber =  $col_streetnumber;  } else { $streetnumber = $post_streetnumber; }
if(empty($post_streetaddress)){ $streetaddress =  $col_streetaddress;  } else { $streetaddress = $post_streetaddress; }
if(empty($post_city)){ $city =  $col_city;  } else { $city = $post_city; }
if(empty($post_state)){ $state =  $col_state;  } else { $state = $post_state; }
if(empty($post_postalcode)){ $postalcode =  $col_postalcode;  } else { $postalcode = $post_postalcode; }
if(empty($post_country)){ $country =  $col_country;  } else { $country = $post_country; }
if(empty($post_addressreference)){ $addressreference =  $col_addressreference;  } else { $addressreference = $post_addressreference; }



$sql = "UPDATE user SET name = :name, 
            surname = :surname, 
            description = :description,  
            profileimage = :profileimage,  
            local = :local,
            streetnumber = :streetnumber,
            streetaddress = :streetaddress, 
            city = :city, 
            state = :state, 
            postalcode = :postalcode, 
            country = :country, 
            addressreference = :addressreference
            WHERE username = :username";

            //db column and value 
$stmt = $conexao_pdo->prepare($sql);  
//where clause                                 
$stmt->bindParam(':username', $username);  
$stmt->bindParam(':name', $name);    
$stmt->bindParam(':surname', $surname);
$stmt->bindParam(':description', $description); 
$stmt->bindParam(':profileimage', $profileimage); 
$stmt->bindParam(':local', $local); 
$stmt->bindParam(':streetnumber', $streetnumber); 
$stmt->bindParam(':streetaddress', $streetaddress); 
$stmt->bindParam(':city', $city); 
$stmt->bindParam(':state', $state); 
$stmt->bindParam(':postalcode', $postalcode); 
$stmt->bindParam(':country', $country); 
$stmt->bindParam(':addressreference', $addressreference); 

$stmt->execute();
//sucess 
    echo "<span style='color: green;' class='fa fa-check fa-3x' aria-hidden='true'></span> "; 


 ?>

如果输入字段为空,我将尝试执行此操作字段

我在表单页面中通过 jQuery 一次发送两个表单

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <button class="btn btn-primary" id="sendforms">submit</button>
             <div id="result"> </div>
            <script type="text/javascript">
            $(document).ready(function () {
              $("#sendforms").click(function() {
                       var combinedFormData = $("#form1,#form3").serialize();
                     $.post(
                            "ok.php",
                            combinedFormData
                     ).done(function(data) {
                            //alert("Successfully submitted!");
                            $("#result").html(data);
                     }).fail(function () {
                              //alert("Error submitting forms!");
                     })
              });
            });
        </script>

如果我回显$_POST['vars'],一切正常;

问题是 pdo 没有用 id="form3" 更新第二个表单我不知道为什么我知道这不是 post 问题,因为我测试过它,这是一个 PDO 问题。

Apache 上一个错误日志:

Access: `127.0.0.1 - - [24/Apr/2017:16:48:48 -0300] "-" 408 - "-" "-"`

错误

[Mon Apr 24 16:35:23.680683 2017] [:error] [pid 6096:tid 1764] [client 127.0.0.1:51396] PHP Notice:  Undefined index: addressreference

由于某种原因,帖子索引名称显示为错误

在通过 jQuery 发帖时,我得到了这样的网址:index.php?loginname=&amp;loginsurname=&amp;profiledescription= 非常奇怪,因为这是一个帖子请求,但 jquery 应该在 ok.php 页面中发布,但它会在发布后的 url 中显示此参数

这是我执行 ajax 调用时的图像

我认为这可能是 PDO 查询限制大小的问题,有没有办法可以增加它?

根据我在使用 pdo 在 db 中插入复选框值之前的经验,在这个问题中并非如此,但可以帮助我们找出 PDO 的问题。

当我从 PDO 检索错误信息时

echo $conexao_pdo->errorInfo();

它返回给我Array

使用 print_r

Array ( [0] => 00000 [1] => [2] => ) 

使用 var_dump

array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } 

我检查它是否成功执行,它是

if ($stmt->execute()) {
  $_SESSION['result'] = "ok";
} else {

    $_SESSION['result'] = "not ok";
}

结果正常,但没有保存到数据库中

【问题讨论】:

  • 那么你得到了什么错误信息?
  • 无错误信息
  • 检查您的错误日志。
  • 如何查看错误日志?
  • $conexao_pdo-&gt;errorInfo(); $stmt-&gt;execute() 语句之后的输出是什么?

标签: php mysql pdo


【解决方案1】:

最后我解决了这个问题,我有 3 个带有 id 的表单:#form1, form2, #form3 但我只通过 jquery 发送 2 个表单

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <button class="btn btn-primary" id="sendforms">submit</button>
             <div id="result"> </div>
            <script type="text/javascript">
            $(document).ready(function () {
              $("#sendforms").click(function() {
                       var combinedFormData = $("#form1,#form3").serialize();
                     $.post(
                            "ok.php",
                            combinedFormData
                     ).done(function(data) {
                            //alert("Successfully submitted!");
                            $("#result").html(data);
                     }).fail(function () {
                              //alert("Error submitting forms!");
                     })
              });
            });
        </script>

html中表单的顺序为:

<form id="form1"><!--form content here --></form>

这是第二种形式的错误

<form id="form2"><!--form content here --> 

表格 3

<form id="form3"><!--form content here --></form>

第二种形式没有关闭标签但是很奇怪 jquery.serialize 这样做是因为与 #form2 无关 使用其他表单和 jquery 代码,jquery 仅发送 #form1#form3 所以 #form2 与表单无关,被序列化并发布,我无法想象这是问题所在但 现在我发现了这个 jquery bug 并获得了更多经验。

感谢大家的帮助,如果有人遇到同样的问题,请 参考这个问题。

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2016-09-29
    • 2014-01-25
    相关资源
    最近更新 更多