0

I am getting this error

Warning: Trying to access array offset on value of type null in E:\xampp\htdocs\word-meaning-learn\word-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;
?>


depperm
  • 10,606
  • 4
  • 43
  • 67
  • Get rid of `$row['bangla_mean'] == $bangla_mean`. If you get a result it is true because `WHERE bangla_mean=?`. – user3783243 Jan 09 '23 at 17:31
  • if `$row` is an array do you mean `$row[$bangla_mean]` – depperm Jan 09 '23 at 17:32
  • `Warning: Trying to access array offset on value of type null` means that the array you're trying to get data from, is not an array - but is a null instead. Looks like your db query is not giving results. – TaurusHORN Jan 09 '23 at 17:32
  • @depperm `$row` wouldn't have `$bangla_mean` as an index, unless the value and the column have the same name. – user3783243 Jan 09 '23 at 17:34
  • that or you tried to access an array using null as an index or higher than the size of the array – Chris G Jan 09 '23 at 17:34
  • @user3783243 its probable, I don't think using a string as index was intentional/should be the way to access an array – depperm Jan 09 '23 at 17:38
  • Can anyone tell me the exact correct code to get rid of the warning? – Najmul Hasan Jan 09 '23 at 19:41
  • @NajmulHasan Check if you have a return, https://stackoverflow.com/questions/16996649/checking-if-mysqli-query-returned-any-values – user3783243 Jan 09 '23 at 22:06
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – kmoser Jan 09 '23 at 22:12

2 Answers2

1

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) {
    // ...
}
Max Zuber
  • 1,217
  • 10
  • 16
0

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);
Mohcin Bounouara
  • 593
  • 4
  • 18