0

I try to show the user country and field that saved in the database by using the following code but I keep getting this error that said

Fatal error: Uncaught Error: Notice: Undefined index: country in C:\xampp\htdocs\LIVE TAG\custom_import.php on line 26

Notice: Undefined index: country in C:\xampp\htdocs\LIVE TAG\custom_import.php on line 26

Notice: Undefined index: field in C:\xampp\htdocs\LIVE TAG\custom_import.php on line 35

Notice: Undefined index: field in C:\xampp\htdocs\LIVE TAG\custom_import.php on line 35

and also here is the before and next side of the code

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "uni";
///////////

//Filter data from here so we can search the exact column that we want to

$connect = mysqli_connect($servername, $username, $password, $dbname);

$country = '';
$field = '';
$qu = "SELECT DISTINCT field FROM csv ORDER BY field ASC";
$query = "SELECT DISTINCT Country FROM csv ORDER BY Country ASC";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetch_all(); // LINE 22
foreach ($result as $row) {
    $country .= '<option value="' . $row['country'] . '">' . $row['country'] . '</option>'; //LINE 26
}
$field = '';
$state = $connect->prepare($qu);
$state->execute();
$res = $state->fetchAll();
foreach ($res as $row) {
    $field .= '<option value="' . $row['field'] . '">' . $row['field'] . '</option>'; //LINE 35
}
//rest of the code
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    You need to call `fetch_all()` on the result not the statement (https://www.php.net/manual/en/mysqli-result.fetch-all.php). – Nigel Ren Oct 31 '20 at 08:32
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Alon Eitan Oct 31 '20 at 10:57
  • you probably need to name your fields in the query like `SELECT DISTINCT field as distinct_field FROM csv ORDER BY field ASC` and then get it `$row['distinct_field']` (Same for the other query) – Alon Eitan Oct 31 '20 at 10:59
  • @AlonEitan I already red that one doesn't help –  Oct 31 '20 at 11:08
  • @AlonEitan your solution doesn't working on my code .:( –  Oct 31 '20 at 11:09

1 Answers1

0

You've got at least three problems in your code.

  1. fetch_all() is a method of mysqli_result not mysqli_stmt. You need to get the result from the statement first.

    $res = $result = $statement->get_result();
    $result = $res->fetch_all();
    // or in a single line
    $result = $statement->get_result()->fetch_all();
    
  2. You are trying to access associative array in your foreach loop, but fetch_all() returns by default a numerical array. You need to ask for associative array.

    $result = $statement->get_result()->fetch_all(MYSQLI_ASSOC);
    
  3. It's fetch_all() not fetchAll(). You are mixing PDO and mysqli here.

Dharman
  • 30,962
  • 25
  • 85
  • 135