0

I am a little new to PHP and trying to pick it up but I have this undefined error

I have one PHP file called index with 2 forms on it, one form is to search a user

when the form is submitted the var $foundname will be set to the name from the first form, which works,

the second form shows the results and allows the admin to change the username using the var $foundname to get the old username to change to the new one

that where i get the undefined variable: found name this var also echos empty assuming that's because of the error any help would be so grateful i have included my PHP sorry its so messy, still learn if i did the code block wrong i am very sorry never uses stack before i also don't wanna use ajax right now but that will be something for a further date. thanks so much for the help in the meantime :)

<?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:&nbsp</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>

Why marked as dupe the post you gave me did not help in my case thats why i opened one ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ray
  • 13
  • 4

1 Answers1

0

error in this code $foundname is not define. you define $namee and update name is not define

you write this query

$sqlupdate = "UPDATE users SET username = '$namee' WHERE users.username = '". $foundname ."'";

but you note define $foundname

i have fixed the issue. you can try below code

//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";
     }
    }
}
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • this dose fix the issue of undefined but not how i want, this way will add update into the query witch i dont want i need the var from $foundname witch is set and workng further up – Ray Oct 31 '17 at 11:38
  • i still have not managed to solve my issue the reply above did not help in my case, if anyone has any other help to solve my issue would be appreciated – Ray Nov 01 '17 at 10:00