-2

Hi iam trying to fetch the record from database and need to update the query in database but if iam trying getting errors as below

Notice: Undefined variable: result in C:\xampp\htdocs\index\index.php on line 57 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\index\index.php on line 57

here is my code:

index.php

<?php
session_start();
?>
<title> Dashboard </title>
</head>
<?php  
$username = $_SESSION['username'];
if($username) 
{ 
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php 
}  
else 
{ 
echo "no"; 
} 
?> 

<body>

    <form method="post" action="personalinfo.php" id="myform">
    <input type='hidden' value="<?php echo $username; ?>" name='email'>

   <div class="box-body table-responsive no-padding">
 <table class="table table-hover">
      <tr>
          <th>ID</th>
          <th>Name</th>

      </tr>
      <tr>
           <?php
                if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
    ?>
              <td><?php echo $row['first_name']; ?></td> 
              </tr>

            <?php

            }
            }
             ?>
   </table>
</div>
   <input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
    </form>
</body>

Personalinfo.php

<?php 
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$email=$_POST['email'];
$firstname = $_POST['first_name'];
$res = "SELECT * FROM registered WHERE email ='$email' ";
$result=mysql_query($res);//this is for comparing ids

$res1 = "SELECT first_name FROM registered WHERE email ='$email' ";
$result=mysql_query($res1);//this is for getting first_name from database table
$query = mysql_query("UPDATE registered SET first_name='$firstname' WHERE email= '$email'");
 // mysql_query("$query") OR die("Error:".mysql_error());

 if($query)
 { 
    echo "Successfully Registered";    
 }
 else{
    echo "Registration has not been completed.Please try again";
 }

Can anyone help me regarding this.Thanks in advance

user5751258
  • 17
  • 2
  • 9
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Jan 22 '16 at 12:04
  • 1
    The error seems to be kinda clear... btw, stop using mysql_* functions and start using prepared statements using PDO or mysqli_* – Naruto Jan 22 '16 at 12:05
  • But what is the problem in my query which i has been done i know mysqli is latest but i should know what i did wrong – user5751258 Jan 22 '16 at 12:11
  • Hi can anyone help me this please – user5751258 Jan 22 '16 at 12:15
  • Was already said that error is kinda clear. Where is the `$result` variable in your `index.php`? – akasummer Jan 22 '16 at 12:23
  • that $result i have been declared in personal info.php file and included that file in my index.php file may i know what is the problem – user5751258 Jan 22 '16 at 12:44
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 22 '16 at 13:28

2 Answers2

1

Maybe you should include Personalinfo.php into index.php so you have access to the $result variable, because it is undefined when you called mysql_num_rows

You cannot access variables from other files without including them, or parsing them.

Also you should use mysqli, pdo, etc. Because it's more secure with a example being prepared statements

0
<?php
session_start();
?>
<title> Dashboard </title>
</head>
<?php  
$username = $_SESSION['username'];
if($username) 
{ 
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php 
}  
else 
{ 
echo "no"; 
} 
?> 
<body>

<input type='hidden' value="<?php echo $username; ?>" name='email'>
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
  <tr>
      <th>Name</th>
  </tr>
  <tr>
        <?php  include "personalinfo.php";?>
        <td><?php echo $row['first_name']; ?></td> 
          </tr>
 </table>
 </div>
 <input type="button" value="Logout" id="logout"     onClick="document.location.href='login.php'" />
 </form>
 </body>
user5751258
  • 17
  • 2
  • 9