HTML表单似乎没有发布到PHP
你好,谢谢你抽出时间 不管我做了多少研究,我都找不到一个已经讨论过的解决这个问题的方法 问题是提交按钮自动重定向,似乎没有发布表单。在我将MySQL函数转换为MySQLi之前,它一直有效。除了网站的这一部分,其他一切都正常工作 HTML表单(myaccount.inc.php):HTML表单似乎没有发布到PHP,php,html,forms,Php,Html,Forms,你好,谢谢你抽出时间 不管我做了多少研究,我都找不到一个已经讨论过的解决这个问题的方法 问题是提交按钮自动重定向,似乎没有发布表单。在我将MySQL函数转换为MySQLi之前,它一直有效。除了网站的这一部分,其他一切都正常工作 HTML表单(myaccount.inc.php): <div id="change-password"> <form class="clearfix" action="" method="post"> <div>&
<div id="change-password">
<form class="clearfix" action="" method="post">
<div><span class="he1">Change Password</span></div>
<div>
<?php include_once 'controllers/accountController.php'; ?>
</div>
<div><label class="fieldlabel" for="password">Current Password:</label></div>
<input type="password" name="password" id="password" size="23" /><br />
<div><label class="fieldlabel" for="passwordnew1">New Password:</label></div>
<input type="password" name="passwordnew1" id="passwordnew1" size="23" /><br />
<div><label class="fieldlabel" for="passwordnew2">Confirm New Password:</label></div>
<input type="password" name="passwordnew2" id="passwordnew2" size="23" /><br />
<input type="submit" name="submit" value="Change Password" class="bt_changepass" />
</form>
</div>
// Checking whether the Password Change form has been submitted.
if(isset($_POST['submit'])=='Change Password')
{
echo "<br />";
// Get the data from the database.
$sql = $mysqli->query("SELECT * FROM ss_members WHERE usr = '".$_SESSION['usr']."' AND pass = '".md5($_POST['password'])."'");
$row = $sql->fetch_assoc();
// Will hold our errors
$err = array();
if($_POST['password'] == "" || $_POST['passwordnew1'] == "" || $_POST['passwordnew2'] == "")
{
$err[] = 'All the fields must be filled in!';
}
if(!$row['pass'] == md5($_POST['password']) && $_POST['passwordnew1'] != "" && $_POST['passwordnew2'] != "")
{
$err[] = 'Current password is not correct!';
}
if($_POST['passwordnew1'] <> $_POST['passwordnew2'])
{
$err[] = 'New passwords do not match!';
}
if(!count($err))
{
if($row['usr'])
{
// If everything is OK change password.
$stmt = $mysqli->prepare("UPDATE ss_members SET pass = md5(?) WHERE usr = {$_SESSION['usr']}");
$stmt->bind_param('s', $_POST['passwordnew1']);
$stmt->execute();
$stmt->close();
echo "Password has been sucessfully updated!<br />";
}
else
{
$err[]='Something broke!';
}
}
if($err)
{
// Save the error messages in the session.
foreach($err as $error)
{
echo $error . "<br />";
}
}
echo "<br />";
}
修改密码
当前密码:
新密码:
确认新密码:
由于没有更好的术语,这个表单由一些PHP控制
PHP(accountController.PHP):
<div id="change-password">
<form class="clearfix" action="" method="post">
<div><span class="he1">Change Password</span></div>
<div>
<?php include_once 'controllers/accountController.php'; ?>
</div>
<div><label class="fieldlabel" for="password">Current Password:</label></div>
<input type="password" name="password" id="password" size="23" /><br />
<div><label class="fieldlabel" for="passwordnew1">New Password:</label></div>
<input type="password" name="passwordnew1" id="passwordnew1" size="23" /><br />
<div><label class="fieldlabel" for="passwordnew2">Confirm New Password:</label></div>
<input type="password" name="passwordnew2" id="passwordnew2" size="23" /><br />
<input type="submit" name="submit" value="Change Password" class="bt_changepass" />
</form>
</div>
// Checking whether the Password Change form has been submitted.
if(isset($_POST['submit'])=='Change Password')
{
echo "<br />";
// Get the data from the database.
$sql = $mysqli->query("SELECT * FROM ss_members WHERE usr = '".$_SESSION['usr']."' AND pass = '".md5($_POST['password'])."'");
$row = $sql->fetch_assoc();
// Will hold our errors
$err = array();
if($_POST['password'] == "" || $_POST['passwordnew1'] == "" || $_POST['passwordnew2'] == "")
{
$err[] = 'All the fields must be filled in!';
}
if(!$row['pass'] == md5($_POST['password']) && $_POST['passwordnew1'] != "" && $_POST['passwordnew2'] != "")
{
$err[] = 'Current password is not correct!';
}
if($_POST['passwordnew1'] <> $_POST['passwordnew2'])
{
$err[] = 'New passwords do not match!';
}
if(!count($err))
{
if($row['usr'])
{
// If everything is OK change password.
$stmt = $mysqli->prepare("UPDATE ss_members SET pass = md5(?) WHERE usr = {$_SESSION['usr']}");
$stmt->bind_param('s', $_POST['passwordnew1']);
$stmt->execute();
$stmt->close();
echo "Password has been sucessfully updated!<br />";
}
else
{
$err[]='Something broke!';
}
}
if($err)
{
// Save the error messages in the session.
foreach($err as $error)
{
echo $error . "<br />";
}
}
echo "<br />";
}
//检查密码更改表是否已提交。
如果(isset($\u POST['submit'])=='Change Password')
{
回声“
”;
//从数据库中获取数据。
$sql=$mysqli->query(“从usr='”、“$\u会话['usr']”和pass='”.md5($\u POST['password'])。“”)的ss_成员中选择*;
$row=$sql->fetch_assoc();
//将保留我们的错误
$err=array();
如果($|u POST['password']==“”|$| u POST['passwordnew1']==“”|$| u POST['passwordnew2']==“”)
{
$err[]=“必须填写所有字段!”;
}
如果(!$row['pass']==md5($\u POST['password'])和&$\u POST['passwordnew1']!=“”&&$\u POST['passwordnew2']!=“”)
{
$err[]=“当前密码不正确!”;
}
如果($_POST['passwordnew1']$_POST['passwordnew2']))
{
$err[]=“新密码不匹配!”;
}
如果(!count($err))
{
如果($row['usr']))
{
//如果一切正常,请更改密码。
$stmt=$mysqli->prepare(“更新ss_成员集pass=md5(?),其中usr={$\u会话['usr']}”);
$stmt->bind_param('s',$\u POST['passwordnew1']);
$stmt->execute();
$stmt->close();
echo“密码已成功更新!
”;
}
其他的
{
$err[]=“有东西坏了!”;
}
}
如果($err)
{
//在会话中保存错误消息。
foreach($err as$error)
{
echo$错误。“
”;
}
}
回声“
”;
}
您的
标记中没有设置操作
,它正在将数据发送到同一文件。i、 例如,myaccount.inc.php
将其更改为:
<form class="clearfix" action="accountController.php" method="post">
试试这个:
将表单操作赋给accountController.php
问题是您正在包括
在发送头之后
您可以移动
到页面顶部,在处理程序部分内,或者您可以将表单提交到
controllers/accountController.php
使用
将您的操作更改为此,因为accountController.php
存在于controllers
文件夹中
<form class="clearfix" action="controllers/accountController.php" method="post">
mysqli是一个类,它的函数查询不是静态的,因此,在使用$mysqli->query
之前,必须声明mysqli类的实例
你应该把
$mysqli=newmysqli('localhost','my_user','my_password','my_db')代码>
以前
$sql = $mysqli->query("SELECT * FROM ss_members WHERE usr = '".$_SESSION['usr']."' AND pass = '".md5($_POST['password'])."'");
因此,正在将数据发送到同一个HTMLH,这肯定会使其成为post,但它似乎在accountController上出错。致命错误:在线调用:\wamp\www\path\controllers\accountController.php中的非对象上的成员函数query()9@JoshMiller-您忘记创建mysqli
类的对象(new mysqli..
)。@Rikesh该对象是在includes/connect.php中创建的,它包含在myaccount.inc.php的父文件中。其他页面使用它很好,这让我很困惑。@JoshMiller-我不确定,但你能通过将include_once
更改为include
@IvanIvković来检查一下吗:解释为什么不能?因为空操作仍然会将表单发送到当前查看页面。我认为accountController.php不是一个网页,它是一个脚本,是网页的一部分。@IvanIvković:我知道,我的答案是改变动作。“我认为accountController.php不是一个网页,它是一个脚本,它是网页的一部分。”=>他没有提到任何关于网页的事情。你的代码看起来也有问题。首先必须采取行动。另一件事是你的代码容易受到XSR攻击。请将验证过程移到SQL语句上方。在这种情况下使用PDO而不是mysql(Select语句)也是值得赞赏的。使用PlaceHolder而不是direct request变量也将提高代码的完整性。accountController.php是网页还是包含?$mysqli定义在另一个文件中,该文件包含在父文件中。html表单和accountController都可以访问数据库,并通过mysql_ping()进行验证。它没有跟随标题保持在同一页面上,而是直接重定向到index.myaccount.inc.php是实际的网页吗?或者它只是包含在另一个页面中?它包含在另一个页面play.php中。然后您可以在play.php上检查重定向。因为没有一个没有操作的表单只会将提交重定向到同一个页面,如果您将accountController.php作为操作,那么在执行后就不会重定向到任何页面。