For the last couple of days I have been having an issue where my PHP code will not get the text from a form text box that has worked for 7 or 8 years. Has anyone else has this issue or is there something that may need to be running that isn't?
My PHP form is a username textbox and password textbox and submit button
this is the code that is going to the die because the username isn't being set:
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
} else {
die('User is blank'); //$username = "";
}
this error is going to my error_log
PHP Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 4 in /home/
I know I know... no mysqli
form html:
<form method="post" onsubmit="return vloginform(this);"
action="https://www..com/php_include/processlogin.php"
name="loginform">
<div id="pleft">
<div class="uandp">Username: </div><div class="inputbox"><input type="text"
name="username" maxlength="30" class="buttonsboxes" style="width: 180px;" />
</div>
<div class="uandp">Password: </div><div class="inputbox"><input
type="password" name="password" maxlength="30" class="buttonsboxes"
style="width: 180px;" /></div>
</div>
<div id="pright">
<p><input class="buttonsboxes" type="submit" value="login" name="submit"
id="submit" /><input class="buttonsboxes" type="reset" value="reset"
name="reset" id="reset" /></p>
</div>
Not sure how you are fetching the records but using mysql_fetch_array instead of manually fetching every column value will fix this issue. Try analysing the result before fetching it. If result is empty, skip fetching.
Something like this:
$result = mysql_query("SELECT * FROM tablename");
if (!$result || !mysql_num_rows($result)) {
die('No records.');
}
while ($row = mysql_fetch_array($result)) {
// Your code goes here
}
Just use the tag required in the textbox or if you are using the $_POST VARIABLE.
POST:
if(isset($_POST['username'])) {
$username = $_POST['username'];
} else {
die('User is blank'); //$username = "";
}
You might want to use $_REQUEST variable instead of $_SESSION variable because whenever the data is transferred from a form to php page, it is transferred in $_REQUEST variable not in $_SESSION variable.
Use can do something like:
if(isset($_REQUEST['username'])) {
$username = $_REQUEST['username'];
} else {
die('User is blank'); //$username = "";
}
UPDATE
As per your code, you are using post method to send the data, so you should you $_POST variable to get the data from the form:
$username = $_POST['username'];
and you can check whether it is empty or not by writing following line of code:
if ($username == null) {
die('User is blank'); // if username = "", this line will be executed
} else {
// do the required task...
}
I just tried the function - isset($_REQUEST['username']) and isset($_POST['username']) also, but it is not giving the desired result. But I have got above code to work.
Hope this helps:)
Related
I'm trying to build a sign up script with PHP for a website, however whenever I input information into one of the "input" tags, for some reason they're being returned as empty, although they really aren't.
In my signup.php I have created this form to post data:
<form class="form-inline" action="includes/signup-inc.php" method="post">
<div class="">
<input type="text" name="email" class="form-control transparent" placeholder="Your email here...">
<input type="password" name="pwd" class="form-control transparent" placeholder="Your password here...">
<input type="text" name="uid" class="form-control transparent" placeholder="Your username here...">
<button type="submit" name="submit" class="btn btn-danger btn-fill">Sign Up</button>
</div>
</form>
Which brings the data over to my signup-inc.php file to allow a user to signup for an account. Whenever I try to signup for an account on my website, the error I created called "emptyInputSignup" keeps being thrown back.
This is what I have in my signup-inc.php:
if(isset($_POST["submit"])){
$email = $_POST["email"];
$pwd = $_POST["pwd"];
$uid = $_POST["uid"];
require_once 'dbh-inc.php';
require_once 'functions-inc.php';
if(emptyInputSignup($email, $pwd, $uid) !== false){
header("location: ../signup.php?error=emptyinput");
exit();
}
And the function being referenced, which I have created in my 'functions-inc.php' is here:
<?php
function emptyInputSignup($email, $pwd, $uid){
$results;
if(empty($email) || empty($pwd) || empty($uid)){
$results = true;
}
else {
$results = false;
}
}
Personally what I've done to try to debug to see if any data is in fact coming through is nulled the error handle which brings you back a page, to signup.php and instead echo'd a statement into the signup-inc.php page with the data being input. All 3 variables are echoed to the signup-inc.php page with the exact string written in the input field.
Any ideas on what I could be missing?
I'm not sure if this is it, but I don't see you actually return the result from the helper function. I think you can simplify it a bit as well, maybe give this a try
<?php
function emptyInputSignup($email, $pwd, $uid){
return empty($email) || empty($pwd) || empty($uid);
}
Whenever I try to login with incorrect information I don't get the error message, It just resets my form when I try to login with incorrect information. I think I might have a conflicting code somewhere. Is there something wrong with my code? Or if possible is there any other way to provide validation based on my code?
Everything works fine. I just need the validation.
My PHP:
<?php
session_start();
ob_start();
//Include the database connection file
include "database_connection.php";
//Check to see if the submit button has been clicked to process data
if(isset($_POST["submitted"]) && $_POST["submitted"] == "yes")
{
//Variables Assignment
$username = trim(strip_tags($_POST['username']));
$user_password = trim(strip_tags($_POST['passwd']));
$validate_user_information = mysql_query("select * from `signup_and_login_users_table` where `username` = '".mysql_real_escape_string($username)."' and `password` = '".mysql_real_escape_string($user_password)."'");
//Validate against empty fields
if($username == "" || $user_password == "")
{
$error = '<br><div class="info">Sorry, all fields are required to log into your account. Thanks.</div><br>';
}
elseif(mysql_num_rows($validate_user_information) == 1) //Check if the information of the user are valid or not
{
//The submitted info of the user are valid therefore, grant the user access to the system by creating a valid session for this user and redirect this user to the welcome page
$get_user_information = mysql_fetch_array($validate_user_information);
$_SESSION["VALID_USER_ID"] = $username;
$_SESSION["USER_FULLNAME"] = strip_tags($get_user_information["fullname"]);
header("location: home.php");
}
else
{
//The submitted info the user are invalid therefore, display an error message on the screen to the user
$error = '<br><div class="info">Sorry, you have provided incorrect information. Please enter correct user information to proceed. Thanks.</div><br>';
}
}
?>
My form:
<div class="login">
<font color="black" size="5"><p>Employee Login</p></font>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="username" placeholder="Username" required="required" />
<input type="password" name="passwd" placeholder="Password" required="required" />
<input type="hidden" name="submitted" id="submitted" value="yes">
<button type="submit" name="submit" class="btn btn-primary btn-block btn-large">Login</button>
<p></p>
<a href="index.php"><img src="img/homebutton.png" height="35px" width="35px">
</form>
</div>
First of all use mysqli functions not mysql because they are now deprecated.
Secondly, the reason you are not getting the error message is because you have not printed the error message. You should add echo $error; after you defined your error variable
I have a login form, and when I acess this login page I have a warning message saying: "Enter your acess data to start session".
And then Im validating if mail is valid or if is empty, and I want to give warning messages for this, and I want these error messages appear in place of "Enter your acess data to start session".
So I create a variable $showWarning = false, and I just show my message "Enter your acess data to start session" if this variable is false.
And the, when I want to give other warning messages I put my variable true, $showWarning = true.
But it is not working, Im getting the two warnings at the same time.
Do you see what can be wrong?
$showWarning = false;
<?php
if(!(isset($_GET['remember'])))
{
echo '<h1>Login:</h1> ';
if($showWarning ==false){
echo '<span>Enter your acess data to start session.</span>';
}
if(isset($_POST['sendForm'])){
$f['email'] = $_POST['email'];
$f['pass'] = $_POST['pass'];
if(!$f['email'] || !valMail($f['email'])){
$showWarning = true;
echo '<span>Email is empty.</span>';
}
else if(strlen($f['pass']) <5 || strlen($f['pass']) > 10){
echo '<span>Pass must have between 5 and 10 chars.</span>';
$showWarning = true;
}
}
Then I have my form:
<form name="login" action="" method="post">
<label class="label">
<input placeholder="Email" type="text" name="email" />
</label>
<label class="label">
<input type="password" placeholder="Pass" name="pass" />
<input type="submit" value="Login" name="sendForm" />
Forgot pass
</label>
</form>
You are trying to use $showWarning before it is defined. Also, you don't have any logic to check to see if a warning is displayed before attempting to show another.
Based on what I see in your code, that check isn't even necessary. Just check if the form is submitted. If so, show any errors that may arise during validation. If not, show your "Enter your access data to start session" message.
if(isset($_POST['sendForm'])){
$f['email'] = $_POST['email'];
$f['pass'] = $_POST['pass'];
if(!$f['email'] || !valMail($f['email'])){
echo '<span>Email is empty.</span>';
}
else if(strlen($f['pass']) <5 || strlen($f['pass']) > 10){
echo '<span>Pass must have between 5 and 10 chars.</span>';
}
}
else {
echo '<span>Enter your access data to start session.</span>';
}
I'm still new and trying to learn php. I have a form and everytime I run it I get an error displaying that the variable were not set when they should be. I'm definately missing something. Kindly explain what why is the variable $_POST['login_button'] not set the first time i run the page?
Code can be found below:
<?php
require 'connect.inc.php';
if (isset($_POST['login_button']) && isset($_POST['username']) && isset($_POST['password'])){
$login_button = $_POST['login_button'];
$username = $_POST['username'] ;
$password = $_POST['password'];
$password_hash = md5($_POST['password']);
if(!empty($username)&&!empty($password)){
$sql = "SELECT `id` FROM `golden_acres_username` WHERE `uname`='$username' AND '".$password_hash."'";
if($sql_run = mysql_query($sql)){
$query_num_rows = mysql_num_rows($sql_run);
}
if($query_num_rows==0){
echo'User name and password are incorrect';
}
else if($query_num_rows==1)
{
echo 'Username and password are correct';
}
}
else
{
echo 'Please fill in user name and password';
}
}
else
{
echo'Fields are not set';
}
?>
<form class="home_logon_area" action="test.php" method="POST">
Username:
<input type="text" name="username" />
Password:
<input type="password" type="password" name="password"/>
<input type="submit" name="login_button">
</form>
Thanks in advance,
Joseph
$_POST contains the result of submitting a form. If no form has been submitted yet, it will not contain anything.
Your script is working just fine; remove echo 'Fields are not set';, or use that line for code that should only run when the form hasn't been submitted yet.
The $_POST variable is set by the server to capture the data content sent by the browser as part of the form POST action. When the page is initially loaded, the browser has only executed/requested a GET call for the content of the page without sending the POST request.
Hope that helps!
This is simple to understand ;-)
First time the phpscript is executed to get the Form
So there will be no information at all (the visitor is new and have not seen the form before)
Then the User fills the form and press Submit button
The form is linked to the same side so the same phpscript gets executed again
Now you have the Formular values transmitted and you can acess them over $_POST
For more information look at php.net
Remove last else from your code and update the form with this one
<form class="home_logon_area" action="test.php" method="POST">
Username:
<input type="text" name="username" required />
Password:
<input type="password" type="password" name="password" required/>
<input type="submit" name="login_button">
</form>
I'm genuinely stuck on something VERY irritating. After a couple of hours of trying everything I know I've ended up here to see if anyone can help. Here's the general idea.
I want one certain page to be available with a password sent via a form. There is no user, and the password will not change. This should be easy, right!
I've got a form which submits with the method set to post, and the action set to $_SERVER['PHP_SELF']. The plan is, when the password variable I've pre-defined matches what is typed in the form, one set of content shows on the page, when it doesn't you get a different set of content (a form).
Here's what's weird. When looking at a print_r I see whatever I submit in the form in the array, but when I put the right password in the array fills, then empties quickly. I see this on the page reload. It completely empties itself. Even stranger, the 2nd time I do this, it works. What am I missing here? I'd love to know!
Many thanks, and Merry Christmas.
---- some code ----
The form
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="pass" id="pass">Password:</label>
<input type="text" name="pass" id="pass" />
<input type="submit" name="submit" value="Yes" />
</form>
Some PHP from the top of the file;
$pass = '12846565488374';
if($_POST['pass']){ $login = $_POST['pass']; } else { $login = 'empty'; }
if($login != $pass) { $show = 0; } elseif($login == $pass){ $show = 1; }
----- solved ------
Turns out this was a JS plugin reloading the page without me knowing.
Try:
if(isset($_POST['pass']) AND $_POST['pass'] == $pass) {
$show = 1;
} else {
$show = 0;
}
Copied from the comment below:
PHP can't update anything after the page is loaded from the server... You can only use refresh or JS/AJAX to change the content. It would be much easier if you uploaded the whole page somewhere.
Try:
<?PHP
if(isset($_POST['pass'])
{
$pass = '12846565488374';
($_POST['pass'] == $pass)? $show = 1 : $show = 0;
echo $show;
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="pass" id="pass">Password:</label>
<input type="text" name="pass" id="pass" />
<input type="submit" name="submit" value="Yes" />
</form>
<?PHP
}
?>
<?php
if (isset($_POST['pass']))
{
if ($_POST['pass'] == $pass)
{
$show = 1;
echo $show;
}
else
{
$show = 0;
echo $show;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="pass" id="pass">Password:</label>
<input type="text" name="pass" id="pass" />
<input type="submit" name="submit" value="Yes" />
</form>
perhaps something like this?
the purpose for the echo is to show when the correct password is entered, $show changes to 1 and when wrong, changed to 0
Edit:
Your Parameters Checking for $show
<?php
if (isset($show) AND $show === 1)
{
echo "The Variable Is Set To 1";
}
elseif (isset($show) AND $show === 0)
{
echo "The Variable Is Set To 0";
}
?>
This is tested and working with your code.
Thank you for your help everyone - as Matanya said, it was indeed a Javascript issue that was reloading the page. It's a music player and it was placed the "true" part of the IF statement. I don't understand why it has this effect, but at least I know. I thought the error would be in my PHP. Here's the player in question: SCM Music Player http://scmplayer.net
Thanks again.