skip to Main Content

I am getting this error

Warning: Trying to access array offset on value of type null in E:xampphtdocsword-meaning-learnword-ajax-insert.php on line 15
Inserted the meaning data!

What’s wrong with the code on the line? > if($row['bangla_mean'] == $bangla_mean)

<?php
include "config.php";
$bangla_mean = $_POST["bangla_mean"];
$english_mean = $_POST["english_mean"];
$example_mean = $_POST["example_mean"];
$synonym_mean = $_POST["synonym_mean"];

if(isset($bangla_mean)){
  $stmt = $conn->prepare("SELECT bangla_mean FROM wordmeanings_table WHERE bangla_mean=?");
$stmt->bind_param("s",$bangla_mean);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array(MYSQLI_ASSOC);

if($row['bangla_mean'] == $bangla_mean){
  $response = "This Bangla meaning already exist!";
  }
 else{
  $stmt = $conn->prepare("INSERT INTO wordmeanings_table (bangla_mean, english_mean, example_mean, synonym_mean) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss",$bangla_mean,$english_mean,$example_mean,$synonym_mean);
      if($stmt->execute()){
        $response = "Inserted the meaning data!";
      }
      else{
        $response = "Something went wrong!";
      }
  }
}

echo $response;
        exit;
?>


2

Answers


  1. You query is empty you try to get the result of "?", it’s not logic.. try to do that instead:

    if(isset($bangla_mean)){
      $stmt = $conn->prepare("SELECT bangla_mean FROM wordmeanings_table WHERE bangla_mean='$bangla_mean'");
    $stmt->bind_param("s",$bangla_mean);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_array(MYSQLI_ASSOC);
    
    Login or Signup to reply.
  2. The error message means that $row variable is null, but you’re trying to treat it as an array. Null is a legal returning value of fetch_array() method when no rows found. If row found, method returns an associative array for requested "bangla_mean" from POST.

    if (is_array($row)) {
        $response = "This Bangla meaning already exist!";
    } else {
        // ...
    }
    

    Or another option to check $row is not a null:

    if ($row !== null) {
        // ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search