-1

I'm trying to select a PHP variable from a database insert it into an html form input. I guess my question is how do you store the query into a variable and then call that variable in an html form? Also, the form is located on a separate page from the form action file. Why is it undefined if it's defined in the PHP file? The desired output is when I load the html page the value from the database for nickname auto-fills that field of the form.

error:
Notice: Undefined variable: Nickname in C:\xampp\htdocs\Client-Projects\Crossfire\templates\CoinSubmission.html on line 45

CoinSubmission.html

<form autocomplete="off" action="AdminCoinSub_Code.php" method="POST">
    <p>
        <input type="text" name="Nickname" id="Nickname" value="<?php echo htmlspecialchars($Nickname); ?>" />
    </p>
</form>

AdminCoinSub_Code.php

<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO coin (ProfileID, Store, Position, 
        Nickname, ContactNumber, MachineCount, CutOffDate, Coins, location, LastSubmission, Rank) 
        VALUES (:ProfileID, :Store,:Position, :Nickname,:ContactNumber,:MachineCount,:CutOffDate, :Coins,:location,:LastSubmission,:Rank)");

    $stmt->bindParam(':ProfileID', $_POST['ProfileID']);
    $stmt->bindParam(':Store', $_POST['Store']);
    $stmt->bindParam(':Position', $_POST['Position']);
    $stmt->bindParam(':Nickname', $_POST['Nickname']);
    $stmt->bindParam(':ContactNumber', $_POST['ContactNumber']);
    $stmt->bindParam(':MachineCount', $_POST['MachineCount']);
    $stmt->bindParam(':CutOffDate', $_POST['CutOffDate']);
    $stmt->bindParam(':Coins', $_POST['Coins']);
    $stmt->bindParam(':location', $_POST['location']);
    $stmt->bindParam(':LastSubmission', $_POST['LastSubmission']);
    $stmt->bindParam(':Rank', $_POST['Rank']);

    $stmt->execute();

      echo "Success";
    }
catch(PDOException $e)
    {
      echo "Error: " . $e->getMessage();
    }
$conn = null;
}

$conn=mysqli_connect($servername,$username,$password,$dbname);
if (mysqli_connect_errno($conn))
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT `Nickname` FROM `adminlogin` WHERE `ProfileID` = ':ProfileID'";
$Nickname = $conn->query($query);  // This is where the query is executed
$fetcher = $Nickname->fetch_assoc();
while($row = mysqli_fetch_array($Nickname))
if (mysqli_num_rows($Nickname) > 0) {
    echo 'User name exists in the table.';
} else {
    echo 'User name does not exist in the table.';
}

?>
Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
Triptonix
  • 11
  • 5
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Dharman Jun 25 '19 at 23:41

2 Answers2

1

First of all, your html page should have the extension .php and not .html, that way it can interpret your php code inside the html file, don't worry this wont break the html.

why is it undefined if it's defined in the php file.

It's because each php script run separately unless you hook them together. I would recommend yo read a bit more about how php works.

For this example to work i would do it it this way.

CoinSubmission.php

<?php //This goes at the top of the file
 include_once('AdminCoinSub_Code.php') //If they are in the same dir else you will need to set the path properly. 
?>

<form autocomplete="off" action="AdminCoinSub_Code.php" method="POST">
 <p>
  <input type="text" name="Nickname" id="Nickname" value="<?php echo 
   htmlspecialchars($Nickname); ?>">
 </p>
</form>

The include at the top will "paste" your code of AdminCoinSub_Code in the CoinSubmission file and treat it as one file. So the variable will be accesible for it.

Note: My explanation is oversimplified, it ain't exactily how it works, but should get the gist of it.

Alaa Morad answer if also valid, but remember to change the .html to .php

Happy Coding :)

Ellesar
  • 341
  • 1
  • 11
0

That's because $Nickname will not be set if Register Globals is off witch is a normal thing !

Register Globals has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0

so use $_POST

<input type="text" name="Nickname" id="Nickname" value="<?php echo htmlspecialchars($_POST['Nickname']); ?>">
Alaa Morad
  • 445
  • 4
  • 13
  • @Dharman Sorry I mean Register Globals : https://en.wikibooks.org/wiki/PHP_Programming/Configuration:_Register_Globals – Alaa Morad Jun 25 '19 at 23:46
  • 1
    Register globals has been removed from PHP for years, no reason to bring it up. Let it rest in peace. I still don't think this is what they asked about. – Dharman Jun 25 '19 at 23:47
  • 1
    Ok, but keep in mind that OP is sending the POST data to one file and is then trying to get the results in another. This is not a problem with unregistered globals, it is a dire misunderstanding of how PHP variables work. – Dharman Jun 25 '19 at 23:53