Reputation: 55
I am creating a time table view using PHP, in which I am getting the lecture data according to the day and time.
Here is the code which I have did
$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>";
}
But I am getting below error for the above code:
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\nexgschool\admin\notify\add_time_table.php on line 45
Upvotes: 2
Views: 98287
Reputation: 22
<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>
Upvotes: -2
Reputation: 33374
When you receive this error after fetching data from the database then it means that the database didn't found any matching rows. Most database fetching functions return either null
or an empty array when there are no matching records or when the result set has been exhausted.
To solve the problem you need to check for the truthiness of the value or for the existence of the key that you want to access.
$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>";
}
If what you are after is a single value from the result array then you can specify a default in case the result is not present.
$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;
The same applies to 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;
Upvotes: 7