-4

i am pretty new to PHP and self teaching so i know i will have some errors, can someone assist me in figuring out why i get this error? i believe i have defined the variabl...

Here is my code (sorry i did not know if i could attach so i just pasted)

<?php 
include ('dbconnect.php');
?>
<html>
<head>
<title>New User </title>
</head>
<body>
<div class="user"><p><a href="../DBindex.php">Log In</a> </p>
</div>

<center>
<?php
if(isset($_POST['cmdadd'])) {
$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string($_POST["password"]);
$emailaddress = mysql_real_escape_string($_POST["emailaddress"]);
$address = mysql_real_escape_string($_POST["address"]);
$telephone = mysql_real_escape_string($_POST["telephone"]);
$country = mysql_real_escape_string($_POST["country"]);

if(empty($username)){
$errors[]="Username Not Entered";
}
if(empty($password)){
$errors[]="Password Not Entered";
}
if(empty($emailaddress)){
$errors[]="Email Not Entered";
}
if(empty($telephone)){
$errors[]="PhoneNumber Not Entered";
}

if(empty($country)){
$errors[]= "Country Not Entered";
}
}

if(!empty($errors)){

foreach($errors as $error){
echo'<a href = "DBregistration.php"> REDO </a>'; 
echo $error."<br/>";

}
} 
else{
$query = "select * 
from users 
where username = '$username' ";

$result = mysql_query($query, $connection);

if (!$query) {
echo "<p>" . mysql_error () . "</p>";
}

$rows = mysql_num_rows($result);

if ($rows != 0 ) { ?>

<a href = "DBregistration.php"> Redo </a>

<?php
}
else {
$query = "insert into users
values              ('$username','$password','$emailaddress','$address','$telephone','$country')";

mysql_query ($query , $connection);

if (!$query) {
echo "<p>" . mysql_error() . "</p>";
}            
else { ?>

<?php

}  }
}
?>

Line 51 is:

where username = '$username' ";
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
  • And line 51 is the line – ? – potashin Jan 24 '15 at 23:42
  • wow.. are we supposed to count line 51 from the top ? – karthikr Jan 24 '15 at 23:42
  • Just a note MYSQL: "This extension is deprecated as of PHP 5.5.0" - http://php.net/manual/en/book.mysql.php . You can easily use MYSQLI. – sauv0168 Jan 24 '15 at 23:45
  • Line 51 is `$username` in your sql query. Your `if(isset($_POST['cmdadd']))` statement is likely failing. – Jeremiah Winsley Jan 24 '15 at 23:46
  • Try not to mix so much php and html code. It's more readable if you don't do that. I bet this can be written without so much mixing. – Whirlwind Jan 24 '15 at 23:48
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Jan 25 '15 at 00:10

4 Answers4

1

The code you pasted contains no syntax errors. So that's your answer. I suspect the error is in some other file; you did not say which file the message came from. It could be that dbconnect.php is not setting $connection, I guess.

But since you say you're a beginner, I'll pass on some suggestions that I was given and have found useful. In no meaningful order:

  1. Indent your code. Ideally, use a checker such as CodeSniffer for PHP. Even better, use an IDE with embedded CodeSniffer support: PHPStorm, Eclipse with PHP Developers Tools, etc. Some of those are free and well supported. You'll need some time to learn - but I can promise you'll never look back.

  2. Use PDO and prepared statements instead of mysql_* functions. It makes no sense to start learning something that is going to disappear pretty soon.

  3. Try and chop up your code in smaller units, use functions whenever you can, and keep HTML text and data as separated as you possibly can. It looks a pain in the nether regions for the first week. Maintenance-wise, it's a really good habit. You might even be interested in looking at some template systems.

  4. Always keep error reporting to maximum when developing. If errors, warnings, or notices are there, they won't go away. You can only choose whether it will be you who spots them first, or your users.

  5. PHP error reporting and tracing leaves a lot to be desired. Tools that you'll want to install are XDebug and, much later, PHPXHprof. XDebug also has step-by-step debugging support if you use a supported IDE such as Eclipse with PDT.

  6. Some might think this smacks of "professional", and advise not to "waste time" on such frills, but - consider a version control system. The first time, and this also I can promise you - that time will come, that you make some changes, "nothing works!", and you'll want to go back... or see what you changed, maybe compare the two versions... the time "wasted" in setting up a simple local repository will be revealed to have been very well invested indeed. Also, such tools are also integrated in most IDEs. Using them is as easy as right-clicking and selecting 'Highlight changes' or 'Compare to a previous version'.

LSerni
  • 55,617
  • 10
  • 65
  • 107
0

If I am counting correctly, it appears that the problem is in the line "$result = mysql_query($query, $connection); ? I don't see the value of $connection set anywhere.

  • sorry guys i didnt realise there was no like counter. the line in question is $query = "select * from users where username = '$username' "; if you scroll all the way down it should be close to the top – Jerrold Rennie Jan 25 '15 at 00:03
0

For me the problem comes here : $query = "select * from users where username = '$username' ";

because if the form is $_POST['cmdadd']) doesn't exist $username is not set so it's undefined

You need to launch the request with the $username only if the $_POST['cmdadd'] exist.

fozzeuh
  • 41
  • 6
0

try this:

    <?php 
include ('dbconnect.php');
?>
<html>
<head>
<title>New User </title>
</head>
<body>
<div class="user"><p><a href="../DBindex.php">Log In</a> </p>
</div>

<center>
<?php
if(isset($_POST['cmdadd'])) {
$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string($_POST["password"]);
$emailaddress = mysql_real_escape_string($_POST["emailaddress"]);
$address = mysql_real_escape_string($_POST["address"]);
$telephone = mysql_real_escape_string($_POST["telephone"]);
$country = mysql_real_escape_string($_POST["country"]);

if(empty($username)){
$errors[]="Username Not Entered";
}
if(empty($password)){
$errors[]="Password Not Entered";
}
if(empty($emailaddress)){
$errors[]="Email Not Entered";
}
if(empty($telephone)){
$errors[]="PhoneNumber Not Entered";
}

if(empty($country)){
$errors[]= "Country Not Entered";
}


if(!empty($errors)){

foreach($errors as $error){
echo'<a href = "DBregistration.php"> REDO </a>'; 
echo $error."<br/>";

}
} 
else{
$query = "select * from users where username = '$username' ";

$result = mysql_query($query, $connection);

if (!$query) {
echo "<p>" . mysql_error () . "</p>";
}

$rows = mysql_num_rows($result);

if ($rows != 0 ) { ?>

<a href = "DBregistration.php"> Redo </a>

<?php
}
else {
$query = "insert into users
values              ('$username','$password','$emailaddress','$address','$telephone','$country')";

mysql_query ($query , $connection);

if (!$query) {
echo "<p>" . mysql_error() . "</p>";
}            
else { ?>

<?php

}  }
}
}
?>
Mohsenadc
  • 118
  • 1
  • 2
  • 10
  • thanks Mohsendac, it fixed the undefined variable error, what exactly did you change? – Jerrold Rennie Jan 25 '15 at 00:19
  • just move '}' in line 38 to line 81. when you create scope with should consider that variables aren't global. I strongly recommend you clear your code. – Mohsenadc Jan 25 '15 at 01:20