【发布时间】:2019-07-30 11:44:14
【问题描述】:
我的 PHP 查询需要一些帮助。我实际上是让用户有机会在他们登录后更新他们自己的详细信息。表单:
<div class="grid-2">
<p><b>UPDATE MY DETAILS</b></p>
<form action ="includes/update.inc.php" method ="post">
<label>S.Name</label>
<input name="update-surname" type="text" placeholder="Enter new surname...">
<label>Address</label>
<input name="update-houseno" type="text" placeholder="Enter house no' or name...">
<input name="update-ln1" type="text" placeholder="1st Line of Address...">
<input name="update-town" type="text" placeholder="Town...">
<input name="update-county" type="text" placeholder="County...">
<input name="update-postcode" type="text" placeholder="Postcode...">
<label>Contact Number</label>
<input name="update-number" type="text" placeholder="Contact Number...">
<label>Email</label>
<input name="update-email" type="text" placeholder="Email...">
<input type="submit" name="update-details" value="Update">
</form>
</div>
我目前拥有的 php 代码,如果用户没有在框中输入任何内容,它会使用空白输入更新数据库(我不希望发生这种情况),如果没有输入我不想要触摸表格中的那个字段。
<?php
// Here we check whether the user got to this page by clicking the proper button.
if (isset($_POST['update-details'])) {
require 'dbh.inc.php';
// We grab all the data which we passed from the signup form so we can use it later.
$surname = $_POST['update-surname'];
$houseno = $_POST['update-houseno'];
$ln1 = $_POST['update-ln1'];
$town = $_POST['update-town'];
$county = $_POST['update-county'];
$postcode = $_POST['update-postcode'];
$email = $_POST['update-email'];
$number = $_POST['update-number'];
// We validate the updated email is correct if email has been updated.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../after-login.php?error=invalidmail=");
exit();
}
$query = "UPDATE `tblMember` SET `fldSName` = '$surname', `fldTelNum` = '$number', `fld1stLnAddress` = '$houseno', `fld2ndLnAddress` = '$ln1', `fld3rdLnAddress` = '$town', `fldCounty` = '$county', `fldPostcode` = '$postcode', `fldEmailAddress` = '$email' WHERE `tblMember`.`fldMemberID` = 1";
$result = $conn->query($query) or die ("error");
}
?>
一旦加载了 php 表单,网页就会消失,并且不会停留在当前网页上。
所以需要两件事,帮助正确查询和帮助页面变为空白而不停留在网页上。
请注意,我知道这很容易受到注入攻击,我只是想让它在物理上正常工作,然后再尝试了解我如何做准备好的语句。
谢谢!
【问题讨论】:
-
关于你的第二部分(所以页面不会刷新或重定向)你需要ajax请求。
-
嗨@pr1nc3,所以页面变为空白,并且在URL中它确实从
after-login.php更改为update.inc.php,我将在查询中将标题直接放在after-login.php上页面? -
只需将标题放在您的页面中,这样它就知道提交后要做什么。您不能将标题放在查询中,只是在最后做
-
您可以将具有
value属性的当前值插入到表单输入中,而不是放置空白字段。这样做的好处是可以通知用户当前值是什么,并且您不需要更改查询。对于空白页,更新成功后尝试做header('Location: ../after-login.php') -
@JoseFG 他/她的查询是正确的
标签: php html phpmyadmin