PHP impossible d'obtenir l'id dans le URL et afficher sa

Par jeSuisUnNouchi, il y a 8 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

je suis en cours de dévelloppement d'un mini site en Php,

je cherche a obtenir l'id dans le URL du navligateur atravers la requete $_Get['id']

ceci es le fichier DB <?php /** * database */ class DB { private $host = DB_HOST; private $user = DB_USER; private $pass = DB_PASS; private $dbname = DB_NAME; private $dbh; private $error; private $stmt; public function __construct(){ // set DSN $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; //set options $options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); //create a new PDO instance try{ $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options); } //catch any errors catch (PDOException $e) { $this->error = $e->getMessage(); } } public function query($query){ $this->stmt = $this->dbh->prepare($query); } public function bind($param, $value, $type = null){ if (is_null($type)) { switch (true) { case is_int ($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; default : $type = PDO::PARAM_STR; } } $this->stmt->bindValue ($param, $value, $type); } public function execute(){ return $this->stmt->execute(); } public function resultset(){ $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_OBJ); } public function single(){ $this->execute(); return $this->stmt->fetch(PDO::FETCH_OBJ); } public function rowCount(){ return $this->stmt->rowCount(); } public function lastInsertId(){ return $this->dbh->lastInsertId(); } public function beginTransaction(){ return $this->dbh->beginTransaction(); } public function endTransaction(){ return $this->dbh->commit(); } public function cancelTransaction(){ return $this->dbh->rollBack(); } } ----------------------------------------------------- le fichier topic1.php ?php require('core/ini.php'); ?> <?php $topic = new topic; $user = new User; $topic_id = $_GET['id']; if (isset($_POST['do_reply'])) { $data = array(); $data['topic_id'] = $_GET['id']; $data['body'] = $_POST['body']; $data['user_id'] = getUser()['user_id']; $validate = new validator; $field_array = array('body'); if ($validate->isRequired($field_array)) { if ($topic->reply($data)) { redirect('topic1.php?id='.$topic_id.'your reply has been posted', 'success'); }else{ redirect('topic1.php?id='.$topic_id.'something wrong with your reply', 'error'); } }else{ redirect('topic1.php?id='.$topic_id.'your reply form is blank', 'success'); } } $template = new Template('template/topic1.php'); //Assign vars $template->topic = $topic->getTopic($topic_id); $template->replies = $topic->getreplies($topic_id); $template->title = $topic->getTopic($topic_id)->title; //display template // Get template & Assign vars echo $template;

je cherche a obtenir l'id dans le URL du navligateur atravers la requete $_Get['id'] dans un autre fichier topic1.php

Et ce que j'obtiens comme reponse :

Notice: Undefined index: id in C:\xampp\htdocs\talkingespace\topic1.php on line 8

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in C:\xampp\htdocs\talkingespace\librairies\DB.php:61 Stack trace: #0 C:\xampp\htdocs\talkingespace\librairies\DB.php(61): PDOStatement->execute() #1 C:\xampp\htdocs\talkingespace\librairies\DB.php(66): DB->execute() #2 C:\xampp\htdocs\talkingespace\librairies\topic.php(129): DB->resultset() #3 C:\xampp\htdocs\talkingespace\topic1.php(34): topic->getreplies(NULL) #4 {main} thrown in C:\xampp\htdocs\talkingespace\librairies\DB.php on line 61

5 réponses

G1.3, il y a 8 ans

Salut

Tu peux regarder ce qu'il y a dans les GET en faisant un die(var_dump($_GET)); afin de voir s'il y a ton information.
Qu'elle est l'URL de ta page lorsque tu as cette erreur ?

jeSuisUnNouchi, il y a 8 ans

merci je essayer voir sa

jeSuisUnNouchi, il y a 8 ans

voila ce que j'obtiens quand je met

die(var_dump($_GET));

array(1) { ["id"]=> string(1) "3" }

jeSuisUnNouchi, il y a 8 ans

ta une idée?

SimonDevelop, il y a 8 ans

Ton erreur vien est du coté de mysql, tu envoie ta variable $_GET['id'] directement dans ta requête ce qui est pas bon.
Regarde la function Quote de PDO, après je ne vois pas tous ton code donc je ne suis pas sûr à 100% que ça soit ça.