-1

hi i am write code for edit data in php but some error are find at this time please fix.

<?php 

$servername = "localhost";
$username = "root"; 
$password = "";
$dbname = "chemistry_web";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

}   
if(isset($_GET['ID'])){
$id=$_GET['ID'];
$sql="SELECT * FROM research_tab WHERE ID='$id'";
$result=mysql_query($sql); 
$rows=mysql_fetch_array($result);}

//include_once 'connection.php';

// if (isset($_GET['ID'])){
    // $id=$_GET['ID'];
    // $query = "SELECT * FROM $tbl_name WHERE id='$id'";
    // $result = mysql_query($query);
    // $row = mysql_fetch_array($result);
// }
 ?>


<!DOCTYPE html>
<html>

Why give warning Undefined variable rows when i am defined variable in top of code.

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Education</title>
    <link href="assets/plugins/bootstrap/bootstrap.css" rel="stylesheet" />
    <link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
    
    <link href="assets/css/style.css" rel="stylesheet" />
    <link href="assets/css/main-style.css" rel="stylesheet" />
    <link href="assets/plugins/morris/morris-0.4.3.min.css" rel="stylesheet" />
   </head>
<body>

    <div id="wrapper">        
        <nav class="navbar navbar-default navbar-fixed-top" role="navigation" id="navbar">          
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="dashboard.html">
                    <img src="assets/img/logo.png" alt="Chemistry"/>
                </a>
            </div>
            <ul class="nav navbar-top-links navbar-right">               
                <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                        <i class="fa fa-user fa-3x"></i>
                    </a>                  
                    <ul class="dropdown-menu dropdown-user">
                        
                        <li><a href="logout.php"><i class="fa fa-sign-out fa-fw"></i>Logout</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </nav>        
        
        <nav class="navbar-default navbar-static-side" role="navigation">           
            <div class="sidebar-collapse">               
                <ul class="nav" id="side-menu">                   
                    <li class="sidebar-search">
                    </li>
                    <li class="selected">
                        <a href="dashboard.php"><i class="fa fa-dashboard fa-fw"></i>Dashboard</a>
                    </li>                  
                    <li>
                        <a href="#"><i class="fa fa-files-o fa-fw"></i>Home Page Settings<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="research.php">Research Settings</a>
                            </li>
                           
                        </ul>
                    </li>
                </ul>               
            </div>            
        </nav>      
        <div id="page-wrapper">
        
            <div class="row">               
                <div class="col-lg-12">
                    <h1 class="page-header">Research Settings</h1>
                </div>              
            </div>
            
            <form action="#" method="post" >
            <div class="form-group">
                <label>Research Title</label>
                    <input type="text" id="title" name="newTitle" class="form-control" placeholder="Enter title" value="<?php echo $rows[1]; ?>"/>
                    <input type="hidden" id="id" name="id" class="form-control" placeholder="Enter title" value="<? echo $rows[0]; ?>"/>
            </div>  
            <div class="form-group">
                <label>Description</label>
                    <textarea id="description" name="description" class="form-control" rows="3"></textarea>
            </div>
                
            <button id="submit" name="submit" type="submit" class="btn btn-primary">Update</button>
            </form>
            
        </div>
    </div>

    <script src="assets/plugins/jquery-1.10.2.js"></script>
    <script src="assets/plugins/bootstrap/bootstrap.min.js"></script>
    <script src="assets/plugins/metisMenu/jquery.metisMenu.js"></script>
    <script src="assets/plugins/pace/pace.js"></script>
    <script src="assets/scripts/siminta.js"></script>

    <script src="assets/plugins/morris/raphael-2.1.0.min.js"></script>
    <script src="assets/plugins/morris/morris.js"></script>
    <script src="assets/scripts/dashboard-demo.js"></script>

</body>

</html>

Why does it give me a warning "Undefined variable rows" when I defined the variable in top of code?

jps
  • 20,041
  • 15
  • 75
  • 79
  • Don't mix `mysqli_*` and `mysql_*` – brombeer Jan 16 '18 at 11:12
  • **The `mysql` PHP extension is dead** -- Stop using the [`mysql_*()` PHP functions](http://php.net/manual/en/function.mysql-connect.php). They are old, deprecated since PHP 5.5 and completely removed in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Jan 16 '18 at 11:39
  • because the variable $rows is defined in the "if" block and not accessible to whole document. declare $rows before the if statement with default value null. – Tarun Jan 16 '18 at 11:45
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Scriptman Jan 16 '18 at 12:24

4 Answers4

1

There are some issues with your code.

  • mysql_* functions are deprecated and insecure. Please use PDO.
  • Read the following topic about MySQL injections: How can I prevent SQL injection in PHP?
  • Don't mix HTML5 and XHTML.
  • Don't use PHP short open tags <?, use the full tag: <?php

    <input type="text" id="title" name="newTitle" class="form-control" placeholder="Enter title" value="<?php echo $rows[1]; ?>"/> <input type="hidden" id="id" name="id" class="form-control" placeholder="Enter title" value="<? echo $rows[0]; ?>"/>

Instead of <?php echo $rows[0]; ?> use:

<?php echo ( isset ( $rows[0] ) ? $rows[0] : '' ); ?>

Same goes for <?php echo $rows[1]; ?>:

<?php echo ( isset ( $rows[1] ) ? $rows[1] : '' ); ?>

So the complete code would be:

<input type="text" id="title" name="newTitle" class="form-control" placeholder="Enter title" value="<?php echo ( isset ( $rows ) ? $rows[1] : '' ); ?>">
<input type="hidden" id="id" name="id" class="form-control" placeholder="Enter title" value="<?php echo ( isset ( $rows ) ? $rows[0] : '' ); ?>">
Scriptman
  • 424
  • 2
  • 13
-1

you are defining variable $rows in "if" block and because of that $rows is not available in whole document.

Try the below code.

// declare $rows at global level
$rows=null;

if(isset($_GET['ID'])){
    $id=$_GET['ID'];
    $sql="SELECT * FROM research_tab WHERE ID='$id'";
    $result=mysql_query($sql); 
    // assign value
    $rows=mysql_fetch_array($result);
}
Tarun
  • 90
  • 10
-2

Try Below Code:

if(isset($_GET['ID'])){
   $id = $_GET['ID'];
   $result = $conn->query("SELECT * FROM leads WHERE LeadId='$id'"); 
   $rows = $result->fetch_row();
}
TechMech
  • 170
  • 3
  • 15
-3

OK catch some problems and i fixit for you in PHP tags:

<?php 

$servername = "localhost";
$username = "root"; 
$password = "";
$dbname = "chemistry_web";

$conn = mysqli_connect($servername,$username,$password,$dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

}   
//Test if your ID IS a Int if is that you put in your DB
if(isset($_GET['ID']) AND is_numeric($_GET['ID'])){
$id=$_GET['ID'];
$sql="SELECT * FROM research_tab WHERE ID='".$id."'";
$result=mysql_query($sql); 
$rows=mysql_fetch_array($result);}

//include_once 'connection.php';

// if (isset($_GET['ID'])){
    // $id=$_GET['ID'];
    // $query = "SELECT * FROM $tbl_name WHERE id='$id'";
    // $result = mysql_query($query);
    // $row = mysql_fetch_array($result);
// }
 ?>

Just replace your Php tags. May this help you!!

Sylarman5
  • 110
  • 1
  • 6
  • 2
    Ever heard of MySQL Injections? – Scriptman Jan 16 '18 at 12:06
  • It very safe if he use this with PDO but here it's not! – Sylarman5 Jan 16 '18 at 12:50
  • It can also be very safe if he uses mysqli (which he is trying to do). Your example is wide open to attacks, and also mixing mysql_* and mysqli_* functions, which is not permitted. If you post code in an answer, it needs to be correct, as people come to stackoverflow and copy a lot of code from answers. – IncredibleHat Jan 29 '18 at 14:19