这就是我所拥有的
<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> ";
?>
这就是我想要做的事情,如果输入字段为空它将使用db列中的当前值更新,但是输入它不是空的它将使用$ _POST更新[&#39; vars&# 39;]分配到字段
我将通过表单页面中的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 =&#34; form3&#34;更新第二个表单。我不知道为什么我知道它不是一个帖子问题,因为我测试了它,它是一个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=&loginsurname=&profiledescription=
非常奇怪,因为它的帖子请求也没有得到jquery应该在ok.php页面中发布但是它显示了这个参数在发帖后的网址
这是执行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";
}
它得到了正常结果,但还没有保存到db
答案 0 :(得分: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并获得了更多经验。
感谢大家尝试提供帮助,如果有人遇到同样的问题请 请参阅这个问题。