php 如何修复Trying to access array offset on value of type null错误

xmq68pz9  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(2440)

我正在使用PHP创建一个时间表视图,其中我根据日期和时间获取讲座数据。
下面是我做的代码

$monday_lectures = "SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'";
$result_11to1 = mysqli_query($con, $monday_lectures);
$m11to1 = mysqli_fetch_array($result_11to1);
if ($m11to1["lecture_day"] == !'') {
    echo "<td>".$m11to1["lecture_name"]."</td>";
} else {
    echo "<td> no class</td>";
}

但我得到下面的错误为上述代码:
警告:尝试访问C:\xampp\htdocs\nexgschool\admin\notify\add_time_table中类型为null的值的数组偏移量。第45行

wfsdck30

wfsdck301#

当您从数据库中获取数据后收到此错误时,这意味着数据库没有找到任何匹配的行。大多数数据库提取函数在没有匹配记录或结果集已耗尽时返回null或空数组。
要解决这个问题,您需要检查值的真实性或要访问的键的存在性。

$monday_lectures = "SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'";
$result_11to1 = mysqli_query($con, $monday_lectures);
$m11to1 = mysqli_fetch_array($result_11to1);
if ($m11to1 && $m11to1["lecture_day"] == !'') {
    echo "<td>".$m11to1["lecture_name"]."</td>";
} else {
    echo "<td> no class</td>";
}

如果您要查找的是结果数组中的单个值,那么您可以指定一个默认值,以防结果不存在。

$monday_lectures = "SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'";
$result_11to1 = mysqli_query($con, $monday_lectures);
$m11to1 = mysqli_fetch_array($result_11to1);
$lecture = $m11to1["lecture_day"] ?? null;

这同样适用于PDO。

$monday_lectures = $pdo->prepare("SELECT * from lectures where lecture_time = '11am to 1pm' and lecture_day = 'firday'");
$monday_lectures->execute();
$m11to1 = $monday_lectures->fetch();
$lecture = $m11to1["lecture_day"] ?? null;
5uzkadbs

5uzkadbs2#

<section class="appl">
                <div class="appl-form">
                    <h1>Edit Form</h1>
                    <?php
                        
                        include('database_conn.php');
    
    
                        if ($_SERVER["REQUEST_METHOD"] === "POST") {
                        //if(isset($_POST['submit'])) {
                            $names = $_POST['names'];
                            $sex = $_POST['sex'];
                            $email = $_POST['email'];
                            $phone = $_POST['phone'];
                            $qualifications = $_POST['qualifications'];
                    
                            $sql = "UPDATE appl_table SET names='$names', sex='$sex', email='$email', phone='$phone', qualifications='$qualifications' WHERE id = ".$_GET["id"];
                                 
                            if (mysqli_query($con, $sql)) {
                                echo "Record updated successfully";
                                //echo  "<div class="alert alert-success">Record updated successfully</div>";
                            } else {
                                echo "Error updating record: " . mysqli_error($con);
                            }
                        }
                        
                        
                        $id="";
                        $name = "";
                        $gender = "";
                        $email = "";
                        $phone = "";
                        $qualification = "";
                        if(isset($_GET['id']))
                        {
                            
                            
                            try{
                                $sql = "SELECT names, sex, email, phone, qualifications FROM appl_table WHERE id=".$_GET["id"];
                                $query = mysqli_query($con,$sql);
                                $data = mysqli_fetch_assoc($query);
                                $id = $_GET["id"];
                                $name = $data["names"];
                                $gender = $data["sex"];
                                $email = $data["email"];
                                $phone = $data["phone"];
                                $qualification = $data["qualifications"];
                            }
                            catch(Exception $ex)
                            {
                                
                                ?><h3><?php echo $ex->getMessage()." ".mysqli_error($con);?></h3><?php
                            }
                            
                        }else
                        {
                            
                        }
                        
                    ?>
                    <form action="update.php?id=<?php echo $id; ?>" method="POST">
                        <input type="text" name="names" placeholder="Full Name" value="<?php echo $name; ?>" required>
                        <select name="sex" id="" class="sex">
                                <option><?php echo $gender; ?></option>
                                <option value="Male" name="gender">Male</option>
                                <option Value="Female" name="gender">Female</option>
                            </select>
                        <input type="text" name="email" placeholder="Email" value="<?php echo $email; ?>" required>
                        <input type="text" name="phone" placeholder="Phone" value="<?php echo $phone; ?>" required>
                        <input type="" name="qualifications" placeholder="Qualifications" value="<?php echo $qualification; ?>" required>
                        <!--<input type="" name="" placeholder="Lorem" required>
                        <input type="" name="" placeholder="Lorem" required>
                        <input type="" name="" placeholder="Lorem" required>
                        <input type="" name="" placeholder="Lorem" required>-->
                        <!--<textarea name="" id="" cols="30" rows="10" placeholder="Your Message"></textarea>-->
                        <button type="submit"  name="submit">Save</button>
                    </form>
                </div> 
            </section>

相关问题