简体   繁体   中英

Dynamic my profile page php keeps giving error

I'm trying to build a my profile page with dynamic url's but for some reason, my code isn't working. This is the outcome I want to have, this is what I get when I replace $username with a username from the database.

在此处输入图片说明

And here is the error I keep getting: 在此处输入图片说明

There URL in image 2 is the same as in the first image.

The code I'm using:

$host = 'localhost';
$db   = 'bitr';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

try {
     $connect = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);
     $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
     echo "DB Connection failed" . $e->getMessage();
}

session_start();
if (empty($_SESSION["userID"])) {
    header('Location: login.php');
}

$username = $_GET['user'];
$datapdo2 = "SELECT * FROM users WHERE gebruikersnaam = $username";
$stmtpdo2 = $connect->prepare($datapdo2);
$stmtpdo2->execute();
$rowpdo2 = $stmtpdo2->fetchAll(PDO::FETCH_ASSOC);
foreach ($rowpdo2 as $users) {
    echo "<tr>";
    echo "<th>" . $users['omschrijving'] . "</th>";
    echo "</tr>";
}

export.sql:

DROP DATABASE IF EXISTS bitr2;
CREATE DATABASE bitr2;

USE bitr2;

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gebruikersnaam` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `wachtwoord` varchar(255) DEFAULT NULL,
  `omschrijving` text NOT NULL,
   PRIMARY KEY (id)
);

INSERT INTO `users` (`gebruikersnaam`, `email`, `wachtwoord`, `omschrijving`) VALUES
('Mauro', 'mauroscheltens@hotmail.com', '12345678', 'Ik ben Mauro en ik ben 17 jaar oud!');

You're interpolating $username into the query string, resulting in an unquoted-value. MySQL interprets this bare string as a column name, and then errors out since you don't have such a column. Instead, you should use a placeholder and bind the value dynamically:

$datapdo2 = "SELECT * FROM users WHERE gebruikersnaam = ?";
$stmtpdo2 = $connect->prepare($datapdo2);
$stmtpdo2->execute(array($username));

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