php 我收到此错误警告:尝试访问类型为null的值的数组偏移量

tkclm6bt  于 2023-01-12  发布在  PHP
关注(0)|答案(2)|浏览(294)

我收到此错误
警告:试图访问第15行E:\xampp\htdocs\word-meaning-learn\word-ajax-insert.php中类型为null的值的数组偏移量插入了含义数据!
行上的代码有什么问题?〉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;
?>
uklbhaso

uklbhaso1#

错误消息表示$row变量为空,但您试图将其视为数组。当未找到行时,fetch_array()方法的法律的返回值为空。如果找到行,方法将从POST中为请求的“bangla_mean”返回关联数组。

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

或者检查$row不为空的另一个选项:

if ($row !== null) {
    // ...
}
e0bqpujr

e0bqpujr2#

你的查询是空的,你试图得到的结果是“?",这是不符合逻辑的..尝试这样做:

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);

相关问题