Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Undefined index help

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Undefined index help

Postby Georgezx9 » Mon Apr 07, 2014 2:26 pm

Firstly I am learning so please bear with me.

The code below gives the errors in lines 57&58, its because the two variables form1_username and form1_password presumably don't have values, how do I fix this.

It has a few areas only there for me to see what is happening, so apologies.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form
name="form1"
method="post"
action="<?php echo $_SERVER['PHP_SELF']?>";>
<h1>
Log in to welcome page
</h1>
<p>
Enter Username: <input type="text" name="form1_username" />
</p>
<p>
Enter Password: <input type="password" name="form1_password" />
</p>

<p>
<input type="submit"
name="submit"
value="submit" />
<input name="form_message"
type="text"
style="border:none; color:red;"
value="" size="60" readonly >
</p>
</body>
</html>
<?php

$user_name = "root";
$password = "";
$database = "blogs";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found)
{
print "Database Found " . $db_handle;
$SQL = "SELECT * FROM users";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result) )
{
print "<BR>";
print $db_field['User-ID'] . "<BR>";
print $db_field['User-Name'] . "<BR>";
print $db_field['User-Password'] . "<BR>";
}


$form_username=$_POST['form1_username'];
$form_password=$_POST['form1_password'];


print($form_username);
print "<BR>";
print($form_password);


if ($form_password == $db_field['User-Password'])
{
?>
<script type = "text/javascript">
result="Press BLOG to enter Jamies blog";
{
document.form1.form_message.value=result;
}
var sStr = "Hiya <?php echo $row['User-Name']?>.";
document.write(sStr);
</script>
<input type="submit" form action="test-page5.html" value="Go to blog">
</form>
<?php
}
else
{
?>
<script type="text/javascript">
{
document.form1.form_message.value="Incorrect. Please enter correct username and password";
}
</script>
<?php
}
}
else
{
print "Database NOT Found " . $db_handle;
mysqli_close($db_handle);
}
?>


Also, my code to an expert probably is a mess, I am struggling with the hierarchical flow of where to put PHP tags, when you have if statements, how do you stop both parts of the code being in different parts of the various statements, appreciate a perfect structure to see what I am missing.

Thanks in advance.
Georgezx9
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Mar 31, 2014 1:15 pm

Re: Undefined index help

Postby seandisanti » Tue Apr 08, 2014 6:35 pm

don't be too hard on yourself, nobody starts an expert. and we only really learn from the things we're not successful at the first time.

To avoid the undefined index issue, use empty() or isset() to make sure the index is populated before you try to reference the value. for example
Code: Select all
if ($_POST && !empty($_POST['user'])){
echo 
'Not only are there POST variables, but the USER variable is a non empty value!';
}
 


as far as knowing when to put php tags etc, best practice is to try to just write in php, echo'ing out your markup conditionally, rather than going in and out of php tags. It's easier to follow, and after you get used to it, it will make your life a lot easier.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.