问题描述
< div class =col-md-6>
< h4>更新联络详情< / h4>
< form action =method =postname =contactid =contact>
< div class =form-group>
< label> *地址:< / label>
< input type =textclass =form-controlid =address_line_1name =address_line_1value =< ;? echo $ address_line_1;?> placeholder =地址行1 ...required =必需>
< input type =textclass =form-controlid =address_line_2name =address_line_2value =< ;? echo $ address_line_2;?> placeholder =地址行2 ...>
< label> *邮政编码:< / label>
< input type =textclass =form-controlid =postcodename =postcodevalue =< ;? echo $ postcode;?> placeholder =邮编required =必填>
< / div>
< div class =form-group>
< label> *电话号码:< / label>
< input type =numberclass =form-controlid =contact_numbername =contact_numbervalue =< ;? echo $ contact_number;?> placeholder =电话号码required =必需>
< / div>
< div class =form-group>
< input type =hiddenclass =form-controlid =useridname =useridvalue =< ;? echo $ userid;?>>
< button type =submitid =contactname =contactclass =btn btn-primary btn-sm>保存详情< / button>
< / div>
< / div>
运行至此脚本的文件:
<$ p $($ _ POST ['contact'])){
$ address_line_1 = str_replace(',\\\'',$ _ POST ['address_line_1' ]);
$ address_line_2 = str_replace(',\\',$ _ POST ['address_line_2']);
$ postcode = str_replace(',\\',$ _ POST ['postcode']);
$ contact_number = $ _POST ['contact_number'];
$ uid = $ _POST ['userid'];
mysqli_query($ conn,UPDATE ap_users SET address_line_1 ='$ address_line_1',address_line_2 ='$ address_line_2',postcode ='$ postcode',$ contact_number ='$ contact_number'WHERE user_id ='$ UID');
}
非常基本,我知道 - 当我发送表单时,变量是在 echo $ address_line_1
这样的页面上回显,都会改变为新的结果,但是当我重新加载页面时,它们会返回到旧结果。看起来,当我发送表单时,MySQL数据库没有被更新,我不太清楚为什么?
根据OP的要求
添加或 die(mysqli_error($ conn))
到 mysqli_query()
来查看是否有错误。
更新时也最好使用 affected_rows()
。
此外,您正在使用 if(isset($ _ POST ['contact'])){
并且具有表单和按钮的2个名称属性。
从< form>
删除一个。 < form>
上的名称属性仅适用于使用jQuery / Ajax的情况。
另外,如果您的表单和PHP / SQL位于同一个文件中,请使用标题重定向到同一页面,并确保您不是 *
在标题之前输出。并确保短标签已启用。
参考文献:
*
您现在的代码可以打开。使用或,其中包含。
I have this form:
<div class="col-md-6">
<h4>Update Contact Details</h4>
<form action="" method="post" name="contact" id="contact">
<div class="form-group">
<label>*Address:</label>
<input type="text" class="form-control" id="address_line_1" name="address_line_1" value="<? echo $address_line_1; ?>" placeholder="Address Line 1..." required="required">
<input type="text" class="form-control" id="address_line_2" name="address_line_2" value="<? echo $address_line_2; ?>" placeholder="Address Line 2...">
<label>*Postcode:</label>
<input type="text" class="form-control" id="postcode" name="postcode" value="<? echo $postcode; ?>" placeholder="Postcode" required="required">
</div>
<div class="form-group">
<label>*Phone Number:</label>
<input type="number" class="form-control" id="contact_number" name="contact_number" value="<? echo $contact_number; ?>" placeholder="Phone Number" required="required">
</div>
<div class="form-group">
<input type="hidden" class="form-control" id="userid" name="userid" value="<? echo $userid; ?>">
<button type="submit" id="contact" name="contact" class="btn btn-primary btn-sm">Save Details</button>
</div>
</div>
Which runs to this script:
if(isset($_POST['contact'])){
$address_line_1 = str_replace("'","\\'",$_POST['address_line_1']);
$address_line_2 = str_replace("'","\\'",$_POST['address_line_2']);
$postcode = str_replace("'","\\'",$_POST['postcode']);
$contact_number = $_POST['contact_number'];
$uid = $_POST['userid'];
mysqli_query($conn, "UPDATE ap_users SET address_line_1 = '$address_line_1', address_line_2 = '$address_line_2', postcode = '$postcode', $contact_number = '$contact_number' WHERE user_id = '$uid'");
}
Very basic, I know - when I send the form, the variables which are echoed on the page such as echo $address_line_1
all change to the new results, although when I reload the page, they return to the old results. It appears that the MySQL database isn't being updated when I send the form and I'm not too sure why?
As requested by the OP.
Add or die(mysqli_error($conn))
to mysqli_query()
to see if errors come of it.
It's also best to use affected_rows()
on update also.
Also, you are using if(isset($_POST['contact'])){
and have 2x name attributes for the form and the button.
Remove the one from <form>
. Name attribute on <form>
only works if using jQuery/Ajax.
Plus, if your form and PHP/SQL are in the same file, use a header to redirect to the same page and make sure you're not *
outputting before header. and make sure short tags are enabled.
References:
- http://php.net/manual/en/mysqli.affected-rows.php
- http://php.net/manual/en/mysqli.error.php
- http://php.net/manual/en/function.header.php
*
How to fix "Headers already sent" error in PHP
Your present code is open to SQL injection. Use mysqli_*
with prepared statements, or PDO with prepared statements.
这篇关于HTML表单改变echo'ed变量,但不是数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!