Declare a variable if the variable is empty

Posted on 2005-03-01
Medium Priority
Last Modified: 2008-02-01
I had this problem (Notice: Undefined index: username )
because the user didn't  enter the password.
Can we write the script like this

if username is empty then set the username =none

Question by:php5atoz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 32

Expert Comment

ID: 13432423

LVL 32

Expert Comment

ID: 13432424
$username = ( isset($username) ) ? $username : "";
LVL 32

Expert Comment

ID: 13432430
$username = ( isset($_POST["username"]) ) ? $_POST["username"] : "";
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.


Author Comment

ID: 13432537
I think I need to little more detail here.
So what should I do to make it not display the message
(Notice: Undefined index: username )

That why I believe If we
Can we write the script like this

if username is empty then set the username =abc


//setup names of database and table to use
$db_name ="xxxx";
$table_name ="xxxx";

$username = ( isset($_POST["username"]) ) ? $_POST["username"] : "none";

//connect to server and select database
$connection = @mysql_connect("localhost","xxxx","xxxx") or die(mysql_error());

$db = @mysql_select_db($db_name,$connection)or die(mysql_error());

//build and issue the query
$sql ="SELECT * FROM $table_name WHERE username = '$_POST[username]' AND Password = password('$_POST[password]')";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get the number of rows in the result set
$num = mysql_num_rows($result);

//print a message or redirect elsewhere,based on result
if ($num != 0) {
      $msg ="<P>Congratulations,you're authorized!</p>";
} else {

echo "<h1>Login to Secret Area</h1>";
echo "<form method=\"post\" action=\"do_authuser_ch15.php\">";
echo "<font color=\"#FF0000\"><strong>Reenter your username and password correctly.";
echo "<br>Remember, passwords are case-sensitive.</strong></font>";
echo " <br><strong>Username:</strong><br>\n";
echo "      <input maxLength=\"25\" size=\"25\" name=\"username\">\n";
echo "      <br><strong>Password:</strong><br>\n";
echo "      <input type=\"password\" maxLength=\"25\" size=\"25\" value name=\"password\">\n";
echo "      <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Sign In\" name=\"signinbt\">\n";
echo "</form>";
<TITLE>Secret Area</TITLE>
<? echo "$msg"; ?>
LVL 32

Accepted Solution

ldbkutty earned 480 total points
ID: 13432558
Like this :

$username = ( isset($_POST["username"]) ) ? mysql_real_escape_string($_POST["username"]) : "";

//connect to server and select database
$connection = @mysql_connect("localhost","xxxx","xxxx") or die(mysql_error());

$db = @mysql_select_db($db_name,$connection)or die(mysql_error());

//build and issue the query
$sql ="SELECT * FROM $table_name WHERE username = '$username' AND Password = password('" . mysql_real_escape_string($_POST['password']) . "')";

Note that you have to validate such that if username/password is empty, ask the user to enter them.

Author Comment

ID: 13432845
uh why the use mysql_real_escape_string on username
BUT use the  password('" . mysql_real_escape_string($_POST['password']) . "')";

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question