gpt4 book ai didi

PHP 更新用户帐户详细信息未显示错误但帐户详细信息未更新

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:27 25 4
gpt4 key购买 nike

概述:我正在为学习目的创建一个虚拟网站,因此它的功能主义者是基本的,安全性不在议程 atm 上。

实际问题:

好的,我的应用程序登录了一个用户,如果需要,该用户可以选择编辑他/她的帐户。所以我继续创建了 PHP 脚本来处理这个问题,但它没有。当我单击编辑帐户按钮时,没有弹出任何错误,但同时当我检查 MySQL 数据库时,没有发生任何更改。

EditAccountForm.php 文件:

<?php

include('connect_mysql.php');


if(isset($_POST['editAccount'])){

$Newusername = $_GET['username'];
$Newpassword = $_POST['password'];
$Newfirstname = $_POST['first_name'];
$Newlastname = $_POST['last_name'];
$Newemail = $_POST['email'];


if($Newusername != $username)
{
$q1 = ("UPDATE users SET username=$Newusername WHERE username=$username");
}
else if(!mysql_query($q1)){

echo "MySQL ERROR: " . mysql_error() . "" . $sql;
}
///////////////////////////////////////////////////////////////

if($Newpassword != $password)
{
$q2 = ("UPDATE users SET password=$Newpassword WHERE password=$password");
}
else if(!mysql_query($q2)){

echo "MySQL ERROR: " . mysql_error() . "" . $sq2;
}
///////////////////////////////////////////////////////////

if($Newfirstname != $firstname)
{
$q3 = ("UPDATE users SET first_name=$Newfirstname WHERE first_name=$firstname");
}
else if(!mysql_query($q3)){

echo "MySQL ERROR: " . mysql_error() . "" . $sq3;
}
///////////////////////////////////////////////////////////////

if($Newlastname != $lastname)
{
$q4 = ("UPDATE users SET last_name=$Newlastname WHERE last_name=$lastname");
}
else if(!mysql_query($q4)){

echo "MySQL ERROR: " . mysql_error() . "" . $sq4;
}
///////////////////////////////////////////////////////////////

if($Newemail != $email)
{
$q5 = ("UPDATE users SET username=$Newemail WHERE email=$email");
}
else if(!mysql_query($q5)){

echo "MySQL ERROR: " . mysql_error() . "" . $sq5;
}



}

?>

userEditAccount.php:

<html>
<head>

<title>Edit Account</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div id="wrapper">
<header><h1>E-Shop</h1></header>


<article>
<h1>Welcome</h1>

<h1>Edit Account</h1>

<div id="login">

<ul id="login">

<form method="post" name="editAccount" action="userEditAccount.php" >
<fieldset>
<legend>Fill in the form</legend>

<label>Select Username : <input type="text" name="username" /></label>
<label>Password : <input type="password" name="password" /></label>
<label>Enter First Name : <input type="text" name="first_name" /></label>
<label>Enter Last Name : <input type="text" name="last_name" /></label>
<label>Enter E-mail Address: <input type="text" name="email" /></label>
</fieldset>
<br />

<input name="Editsubmited" type="submit" submit="submit" value="Edit Account" class="button">

</form>

<?

echo $newrecord;
?>


</div>
<form action="userhome.php" method="post">
<div id="login">
<ul id="login">
<li>
<input type="submit" value="back" onclick="index.php" class="button">
</li>
</ul>
</div>



</article>
<aside>
</aside>

<div id="footer">This is my site i Made coppyrights 2013 Tomazi</div>
</div>

</body>
</html>

此外:

我试图摆弄在网上看到的代码,但不幸的是,我为这个脚本编写的代码在我看来是最好的解决方案,也是对我有意义的解决方案。

所以我别无选择,只能转向这个网站寻找答案,谁能看出这整件事哪里出了问题......?

“编辑帐户”页面的图片: enter image description here

如 Conect_mysql.php 所问:

<?php 

$db_hoast = "127.0.0.1";
$db_username = "root";
$db_password = "";
$db_name = "eshop";

$con = mysql_connect("$db_hoast","$db_username","$db_password");
if(!$con)
{
die("Could not connect to DATABASE");
}
$db = mysql_select_db("$db_name");
if(!$db)
{
die("No database");
}

?>

最佳答案

UPDATE 语句的问题是值没有用单引号引起来。它们是字符串文字,应该被换行。

$q1 = "UPDATE users SET username='$Newusername' WHERE username='$username'";

为了显示错误,

if($Newfirstname != $firstname)
{
$q1 = "UPDATE users SET username='$Newusername' WHERE username='$username'";
$result = mysql_query($q1);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
}

您的逻辑 UPDATES 也是错误的。这会导致您更新符合条件的记录。

作为旁注,查询易受 SQL Injection 攻击如果变量的值(s) 来自外部。请查看下面的文章,了解如何预防它。通过使用 PreparedStatements,您可以摆脱在值周围使用单引号。

关于PHP 更新用户帐户详细信息未显示错误但帐户详细信息未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15149395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com