如何修复代码中未定义的变量行

hi3rlvi2  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(331)

嗨,我写的php数据编辑代码,但有些错误是在这个时候发现请修复。

<?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>

为什么在代码顶部定义变量时发出警告未定义变量行为什么在代码顶部定义变量时发出警告未定义变量行为什么在代码顶部定义变量时发出警告未定义变量行

jqjz2hbq

jqjz2hbq1#

请尝试以下代码:

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

ijxebb2r2#

你的代码有一些问题。 mysql_* 函数已弃用且不安全。请使用pdo。
阅读以下关于mysql注入的主题:如何防止php中的sql注入?
不要混合使用html5和xhtml。
不要使用php短开放标记 <? ,使用完整标记:

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

同样的道理 <?php echo $rows[1]; ?> :

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

所以完整的代码是:

<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] : '' ); ?>">
xtupzzrd

xtupzzrd3#

您正在“if”块中定义变量$rows,因为$rows在整个文档中不可用。
请尝试以下代码。

// 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);
}
2q5ifsrm

2q5ifsrm4#

好的,抓住一些问题,我在php标记中为您解决:

<?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);
// }
 ?>

只需替换php标记。愿这对你有帮助!!

相关问题