I’m running into an issue with my code where the PHP portion of it works, the mysql portion works, and I’m pretty sure the html portion works but I can’t put it all together for some reason. If I had to take a guess the html is wrong or I’m missing something.
Everything in the processUser function works if I put in hard inputs and not ones via the html form. Does anyone have any idea? Sorry if this is a stupid fix any help is appreciated thank you!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1> Application Form</h1>
<h2> Requirements </h2>
<p>Please include name to make it easier to add (Not saved once admitted)</p>
<p>Password needs the following: 10+ Char, 1 Special Char (Include one of the following !#$%^&*_+|:,.?")</p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="fName" title="Enter your desired username (No special Chars)">
Email: <input type="email" name="fEmail" title="Enter desired Email">
Password: <input type="password" name="fPass" title="Enter desired password">
Extra Info: <input type="text" name="fExtra">
<input type="submit">
</form>
<?php
require_once "connect.php";
require_once "sanitize.php";
require_once "log.php";
require_once "goat.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Configure file for MySQL change to call a function
$name = htmlspecialchars($_POST['fName']);
$email = htmlspecialchars($_POST['fEmail']);
$pass = htmlspecialchars($_POST['fPass']);
$extra = htmlspecialchars($_POST['fExtra']);
processUser($_POST['fName'], $_POST['fEmail'], $_POST['fPass'], $_POST['fExtra']);
}
function processUser($name, $email, $pass, $extra){
// Implement checks from sanitatize.php here
$usrCheck = sanitizeUser($name,1);
$emailCheck = sanitizeEmail($email,1);
$passCheck = sanitizePass($pass, $name);
if($usrCheck && $emailCheck && $passCheck){
// Add Salting for password
$pass = salty($pass);
$filePath = "../../Issues/AboutUser.txt";
if (file_exists($filePath) && is_readable($filePath)) {
$tmpFile = fopen($filePath, "a");
if ($tmpFile){
$fileSize = count(file($filePath));
$text = $fileSize+1 . ")" . $name . " " . $email . " " . $extra . "n";
fwrite($tmpFile, $text);
fclose($tmpFile);
logs("Successfully added" . $name . "to the list. Waiting to be added");
echo '<p> Added user </p>';
echo '<div class="success-message">Successfuly added.You will be reached out to when added or message Cam for assistance</div>';
//Add them to the database
addInfo($name,$email,$pass,0,0);
}else{echo '<p> failed check 5 </p>';} //'<div class="error-message"Failed to add reach out to Cam</div>';}
}else{echo '<p> Failed check 4 </p>';} //'<div class="error-message"Failed to read file reach out to Cam</div>';}
}
else{
if(!$usrCheck){ echo '<p>Failed check 1 </p>';} //'<div class="error-message"Failed Username check, try again</div>';}
if(!$emailCheck){ echo '<p> failed check 2 </p>';} //'<div class="error-message"Failed Email check, try again</div>';}
if(!$passCheck){ echo '<p> Failed check 3 </p>';} //'<div class="error-message"Failed Password check, try again</div>';}
}
}
//processUser("Glowstick016","glowstick016@gmail","scooby016!!","etherhe");
?>
</body>
</html>
This is the code for addInfo() also
function addInfo($username, $email, $pass, $admin, $rights){
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$DB_SERVER = 'localhost:3306';
$DB_USERNAME = 'GlowWeb';
$DB_PASSWORD = REDACTED;
$DB_NAME = 'GlowWeb';
/* Attempt to connect to MySQL database */
$link = new mysqli($DB_SERVER, $DB_USERNAME, $DB_PASSWORD, $DB_NAME);
// Check connection
if($link ->connect_error){
die("ERROR: Could not connect. " . mysqli_connect_error());
logs("ERROR: Could not connect" . mysqli_connect_error());
}
//User Info
$sql = "INSERT INTO Users (Usr, EMAIL, Password, Admin, Rights) VALUES ('$username', '$email', '$pass', '$admin', $rights)";
//Checking if it worked
if ($link->query($sql) === TRUE) {
echo "New user added successfully";
} else {
echo "Error: " . $sql . "<br>" . $link->error;
logs("Error: " . $sql . "<br>" . $link->error);
}
$link->close();
}
I basically want to enter info into the form which then get’s checked for sanitization then inserted into my mysql database. It works when done with processUser() but not when done via html form
Cameron Kennedy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.