Skip to content
Advertisement

How can I add more categories to the | select * from users, and more

I am currently having an error which I do not understand how to solve it and the truth is that I have been trying since yesterday and I do not get any solution. Could anyone help me please?

FILE: topics.php

if (isset($_GET['id'])){
    $id = $_GET['id'];
    $categoria = selectOne($table, ['id' => $id]);
    $id = $categoria['id'];
    $name = $categoria['name'];
}

if (isset($_GET['update-topic'])){
    dd($_POST);
}

FILE: db.php

function selectOne($table, $conditions)
{
    global $conn;
    $sql = "SELECT * FROM users, categorias";
    
        $i = 0;
        foreach ($conditions as $key => $value) {
            if ($i === 0){
                $sql = $sql . " WHERE $key=?";
                
            } else {
                $sql = $sql . " AND $key=?"; 
            }
            $i++;
        }

FILE: edit.php

  <div class="input-group">
    <button type="submit" name="update-topic" class="btn" >Update Topic</button>
  </div>

ERROR:

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:xampphtdocsappdatabasedb.php:19 Stack trace: #0 C:xampphtdocsappdatabasedb.php(77): executeQuery(‘SELECT * FROM u…’, Array) #1 C:xampphtdocsappcontrollerstopics.php(24): selectOne(‘categorias’, Array) #2 C:xampphtdocsadmintopicsedit.php(1): include(‘C:xampphtdocs…’) #3 {main} thrown in C:xampphtdocsappdatabasedb.php on line 19

Advertisement

Answer

I guess your condition column is ambiguous for database and when you apply filter on id database doesn’t know the exact reference of id column, like id of users table or categories table.

To fix that issue you need to refer the id column with table name, I can see that you have passed $table to selectOne() but you are not using it.

$sql = $sql . " WHERE ".$table.".".$key=?";

Using multiple related tables I would suggest to use joins.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement