简体   繁体   中英

SQL ERROR = Unknown column 'Backpack' in 'where clause'…tips?

I am taking an intro class to PHP and mySQL. This code am writing is trying to return more information from a database when I click on a link from a table with less information from the same database. For some reason I cannot get the code to work after endless hours of research and testing. I would greatly appreciate any help or tips.

SQL:

#stops table from being duplicated
 DROP TABLE IF EXISTS stuff;
 #create table for stuff
 CREATE TABLE IF NOT EXISTS stuff (
  id INT primary key auto_increment, 
  location_id INT not null,
  name TEXT not null,
  description TEXT not null,
  create_date DATETIME not null,
  update_date DATETIME not null,
  room TEXT,
  owner TEXT,
  finder TEXT,
  status SET('found','lost','claimed') not null
);

INSERT INTO stuff (location_id, name, description, create_date, update_date, room, owner, finder, status)
    VALUES(2, "Cell phone", "Black iPhone 5s, scratches on screen with a black otterbox", now(), now(), "", "Zach Tsouprakos", "", 'Lost'),
    (16, "Backpack", "Black and blue Under Armour backpack, with keychain hanging off the front zipper", now(), now(), "Room 2023", "", "Casimer DeCusatis", 'Found'),
    (32, "Sunglasses", "Tortoise print Ray Ban sunglasses", now(), now(), "", "", "Rachel Ulicni", 'Found');

PHP:

    #Creates individual table for each hotlink(row) 
        while ( $row = mysqli_fetch_array( $results , MYSQLI_ASSOC ) )
        {
            $alink = '<A HREF=linkystuff.php?name=' . $row['name']. '>' . $row['name'] . '</A>' ;
            echo '<TR>' ;            
            echo '<TD>' . $row['id'] . '</TD>' ;
            echo '<TD>' . $row['create_date'] . '</TD>' ;
            echo '<TD>' . $row['status'] . '</TD>' ;
            echo '<TD ALIGN=right>' . $alink . '</TD>' ;
            echo '</TR>' ;
        }

        #End the table
        echo '</TABLE>';

        #Free up the results in memory
        mysqli_free_result( $results ) ;
    }
}

#Shows the records in presidents
function show_record($dbc, $name) {
    #Create a query to get the name and price sorted by price
    $query = 'SELECT id, location_id, name, description, create_date, update_date, room, owner, finder, status FROM stuff WHERE name = ' . $name ;

You need to quote $name in your query

$query = 'SELECT id, location_id, name, description, create_date, update_date, room, owner, finder, status' .
    ' FROM stuff' .
    ' WHERE name = "' . $name . '"';
//                 ^             ^

But it would be better tu use prepared statements to prevent sql injection and others

$query = 'SELECT id, location_id, name, description, create_date, update_date, room, owner, finder, status' .
    ' FROM stuff' .
    ' WHERE name = ?';
$stmt = $mysqli_link->query($query); // or: mysqli_query($link, $query);
$stmt->bind_param('s', $name);
$stmt->execute();
$row = $stmt->fetch_assoc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM