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
You query is empty you try to get the result of "?", it’s not logic.. try to do that instead:
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 offetch_array()
method when no rows found. If row found, method returns an associative array for requested "bangla_mean" from POST.Or another option to check
$row
is not a null: