我对PHP有点新,并尝试将其解决但我有这个未定义的错误
我有一个名为index的PHP文件,上面有2个表单,一个表单是搜索用户
提交表单时,var $ foundname将设置为第一个表单中的名称,该表单有效,
第二个表单显示结果,并允许管理员使用var $ foundname更改用户名,以便将旧用户名更改为新用户名
我得到未定义变量的地方:找到名字 这个var也回声空假设是因为错误任何帮助都会如此感激我已经包含了我的PHP抱歉它太乱了,还是学习 如果我做了错误的代码块我很抱歉从来没有使用堆栈之前我也不想现在使用ajax但这将是一个更进一步的日期。 非常感谢在此期间的帮助:)
<?php
//lets include our auth.php to check if the user is logged in.
include("../auth.php");
include("../admincheck.php");
include("../db.php");
//Now lets include our rank check to see if the member has permission to this page.
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://localhost/web/main.css"/>
<title>Admin-Panel</title>
</head>
<body>
<h1>Admin-Panel</h1>
<h1><p>Welcome <?php echo $_SESSION['user_name']; ?>!</p></h1>
<hr>
<!-- EDIT A USER FEATURE -->
<h1>Edit A User-Account</h1>
<div class="form">
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="searchform">
<label>search for: </label>
<input type="text" name="name">
<input type="submit" name="search" value="Search">
</form>
</div>
<hr>
<?php
//if the search box is submitted
//let's check if the form was submitted
// THIS SECTION WORKS.
if(isset($_POST['search'])){
if(isset($_POST['name'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
//Now lets assing $foundname with the input form data named name
$foundname = $_POST['name'];
//lets query the table
$sql="SELECT username FROM users WHERE username LIKE '%" . $foundname . "%'";
//lets run the query against the mysql function
$result=mysqli_query($con, $sql);
//we now make a while loop to loop though the result
while($row=mysqli_fetch_array($result)){
$username = $row['username'];
//end of search submitted check
?>
<div class="form">
<form method = "POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="updateusr">
<h1 class="accountresult">Results:</h1>
<label>name:</label>
<input type="text" name="userr" value="<?php echo $row['username'] ?>"/>
<input type="submit" name="updatename" value="Update">
</form>
</div>
<!-- edit forum -->
<?php
}
}else{
echo "<p>Please enter a search query</p>";
}
}
}
//sets the new username by getting old the entered/found userbane from $foundname
if(isset($_POST['updatename']) && !empty($_POST['updatename'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['userr'])){
$namee = $_POST['userr'];
$sqlupdate = "UPDATE `users` SET `username` = '$namee' WHERE `users`.`username` = '". $foundname ."'";
echo $sqlupdate;
if($sqlupdate){
echo "Done";
}else{
echo "failed";
}
}
}
?>
<!-- END OF USER EDIT -->
</body>
</html>
为什么标记为欺骗你给我的帖子在我的情况下没有帮助,这就是为什么我开了一个?
答案 0 :(得分:0)
$foundname
中的错误未定义。您定义$namee
并且更新名称未定义
你写这个查询
$ sqlupdate =&#34;更新
users
设置username
=&#39; $ namee&#39;哪里users
。username
=&#39;&#34;。 $ foundname。&#34;&#39;&#34;;
但请注意定义$foundname
我已经解决了这个问题。你可以尝试下面的代码
//sets the new username by getting old the entered/found userbane from $foundname
if(isset($_POST['updatename']) && !empty($_POST['updatename'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['userr'])){
$namee = $_POST['userr'];
//above you define $namee not $foundname
$updatename = $_POST['updatename']
//above i have add new line code
$sqlupdate = "UPDATE `users` SET `username` = '$namee' WHERE `users`.`username` = '". $updatename."'";
$result=mysqli_query($con, $sqlupdate);
echo $sqlupdate;
if($sqlupdate){
echo "Done";
}else{
echo "failed";
}
}
}