Tuto news

Par neuillane, il y a 13 ans


bonjour à tous,

voila je viens de finir le tuto sur la creation de news en php et lorsque je veux modifier une de mes news, localhost me trouve une erreur en ligne 12 de edit.php.
j'ai refait une nouvelle fois le tuto et toujours la meme chose?

<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);    
    if(!empty($_POST)){
        extract($_POST);
        $sql="UPDATE news SET titre='$titre', contenu='$contenu' WHERE id=$id";
        $req = mysql_query($sql) or die('erreur SQL!<br />'.$sql.'<br />'.mysql_error());
        echo "News Modifiée";
        $_GET"id"]=$id;
    }
    $sql = "SELECT * FROM news WHERE id='".$_GET"id"]."'";
    $req=mysql_query($sql) or die('erreur SQL!<br />'.$sql.'<br />'.mysql_error());
    $data=mysql_fetch_assoc($req);

?>
<a href="edit.php">Modifier une news</a> 
<form method="post" action="edit.php"/>
<input name="id" type="hidden" value="<?php echo $data"id"]; ?>/>
    Titre :<input type="text" name="titre" value="<?php echo $data"titre"]; ?>/>
    <br/>
    Contenu :<br/>
    <textarea name="contenu" style="width:100%; height:200px;" value="<?php echo $data"contenu"]; ?>></textarea>
    <input type="submit" value="Valider la News"/>
 </form>

Merci de votre aide

25 réponses

neuillane, il y a 13 ans

Donc l'erreur ne vient pas du code?

Benjamin Garcia BP, il y a 13 ans

explique moi mieux le problème

neuillane, il y a 13 ans

j'ai aussi essayer avec

$sql = "SELECT * FROM news WHERE id={$_GET"id"]}";

et c'est la meme chose?

Nairolf, il y a 13 ans

Essaye plutôt:

$sql = "SELECT * FROM news WHERE id=".$_GET'id'];
neuillane, il y a 13 ans

et non la meme chose, voici l'erreur affichée...

je ne comprends pas?

neuillane, il y a 13 ans

voici mon code sql mais je pense que celui-ci est bon:

[spoiler]

-- phpMyAdmin SQL Dump
-- version 3.5.1
-- http://www.phpmyadmin.net
--
-- Client: localhost
-- Généré le: Lun 06 Mai 2013 à 14:59
-- Version du serveur: 5.5.24-log
-- Version de PHP: 5.3.13
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Base de données: `news`
--
-- --------------------------------------------------------
--
-- Structure de la table `news`
--
CREATE TABLE IF NOT EXISTS `news` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `titre` varchar(60) NOT NULL,
  `contenu` mediumtext NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=155 ;
--
-- Contenu de la table `news`
--
INSERT INTO `news` (`id`, `titre`, `contenu`, `date`) VALUES
(152, 'patatti', 'pattatta', '2013-05-03 13:51:47');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

[/spoiler]

Benjamin Garcia BP, il y a 13 ans

Bonjour, si je ne me trompe pas, ton url est sous la forme ?id=..... donc avec ton $_GET tu récupère la valeur ..... mais apparrament la tu ne fait rien passer, donc logiquement ton array $_GET'id'] est vide.

neuillane, il y a 13 ans

Je suis aller voir les commentaires du tutoriel et apparemment cette erreur est recurrente. Je peux creer mes news de ma base de données. Je peux les supprimer. Je peux aussi les creer de mon dossier admin, mais lorsque je veux les editer, il me mets cette erreur :
"erreur SQL!
SELECT * FROM news WHERE id=
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
et
"Notice: Undefined index: id in C:\wamp\www\admin\edit.php on line 12".

Benjamin Garcia BP, il y a 13 ans

et comment fait tu passer ta variable id ?

neuillane, il y a 13 ans

voici les codes:
config.php:

<?php
    define("DB_HOST","localhost");

    define("DB_LOGIN","root");

    define("DB_PASS","");

    define("DB_BDD","news");
?>

index.php:

<?php
    require "config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);
?>
<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Titre de la page</title>
  <link rel="stylesheet" href="style.css">
  </head>
<body>
<?php   
    $sql="SELECT * FROM news";
    $req=mysql_query($sql) or die('erreur sql!<br/>'.$sql.'<br/>'.mysql_error());
    while($data=mysql_fetch_assoc($req)){
        echo "<h1>{$data"titre"]}</h1>";
        echo "<p>{$data"contenu"]}</p>";
        echo "<p align\"right\">".date("j/n/Y G:i",strtotime($data"date"]))."</p>";

    }
?>  
</body>
</html>

admin/index.php:

<form method="POST" action="creer.php"/>
    Titre :<input type="text" name="titre"/>
    <br/>
    Contenu :<br/>
    <textarea name="contenu" style="width:100%; height:200px;"></textarea>
    <input type="submit" value="Valider la News"/>
 </form>
<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);

    $sql="SELECT * FROM news";
    $req=mysql_query($sql) or die('erreur sql!<br />'.$sql.'<br />'.mysql_error());
    while($data=mysql_fetch_assoc($req)){
        echo "<p>{$data"titre"]} --</p>";
        echo "<a href=\"edit.php?id={$data"id"]}\">Modifier cette news</a>";
        echo " -- <a href=\"supprimer.php?id={$data"id"]}\">x</a>";
        echo"</p>";

    }
?>

Supprimer.php:

<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);
    $sql = "DELETE FROM news WHERE id={$_GET"id"]}";
    $req=mysql_query($sql) or die('erreur sql!<br />'.$sql.'<br />'.mysql_error());
    header("Location: index.php");
?>

et enfin creer.php:

<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);
    extract($_POST);
    $sql="INSERT INTO news(titre,contenu) VALUES ('$titre','$contenu')";
    $req=mysql_query($sql) or die('erreur sql!<br />'.$sql.'<br />'.mysql_error());
    header("Location: index.php");
?>
Benjamin Garcia BP, il y a 13 ans

déjà rajoute un <input type="hidden" name="id"/>

neuillane, il y a 13 ans

dans quel fichier et a quel niveau?

Benjamin Garcia BP, il y a 13 ans

Dans ton fichier admin/index.php avec ton formulaire

neuillane, il y a 13 ans

comme ça? :

<form method="POST" action="creer.php"/>
    <input type="hidden" name="id"/>
    Titre :<input type="text" name="titre"/>
    <br/>
    Contenu :<br/>
    <textarea name="contenu" style="width:100%; height:200px;"></textarea>
    <input type="submit" value="Valider la News"/>
 </form>
<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);

    $sql="SELECT * FROM news";
    $req=mysql_query($sql) or die('erreur sql!<br />'.$sql.'<br />'.mysql_error());
    while($data=mysql_fetch_assoc($req)){
        echo "<p>{$data"titre"]} --</p>";
        echo "<a href=\"edit.php?id={$data"id"]}\">Modifier cette news</a>";
        echo " -- <a href=\"supprimer.php?id={$data"id"]}\">x</a>";
        echo"</p>";

    }
?>
Benjamin Garcia BP, il y a 13 ans

oui

neuillane, il y a 13 ans

Personne ne voit d'ou vient l'erreur?

neuillane, il y a 13 ans

un p'tit up?

patou, il y a 13 ans

Bonsoir, avez vous toujours votre problème avec la ligne 12 ?
si oui avez vous essayé ceci :

$test = $_GET"id"];
 $sql = "SELECT * FROM news WHERE id='$test'";

avez vous tenter de mettre une valeur d'id existante ?, du genre:

$sql = "SELECT * FROM news WHERE id=2";

comment est formater votre url qui envoi votre "get" : http://www.lesite.fr/monfichier.php/?id=2
tenez nous au courant de votre évolution...

neuillane, il y a 13 ans

alors lorsque je modifie mon script en y mettant :

$test = $_GET"id"]; 
$sql = "SELECT * FROM news WHERE id='$test'";

mon erreur ligne 12 persiste.
Et en mettant :

$sql = "SELECT * FROM news WHERE id=2";

id existante dans ma base de donnée, il n'y a plus d'erreur mais lorsque je clique sur modifier rien ne se passe (aucun lancement?

lorsque vous me dites :"comment est formater votre url qui envoi votre "get" : http://www.lesite.fr/monfichier.php/?id=2 " comment puis-je verifier?

merci de votre aide

neuillane, il y a 13 ans

voici mon edit.php:

<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);    
    if(!empty($_POST)){
        extract($_POST);
        $sql="UPDATE news SET titre='$titre', contenu='$contenu' WHERE id=$id";
        $req = mysql_query($sql) or die('erreur SQL!<br />'.$sql.'<br />'.mysql_error());
        echo "News Modifiée";
        $_GET"id"]=$id;
    }
    $test = $_GET"id"]; 
    $sql = "SELECT * FROM news WHERE id='$test'"; 
    $req=mysql_query($sql) or die('erreur SQL!<br />'.$sql.'<br />'.mysql_error());
    $data=mysql_fetch_assoc($req);

?>
<a href="edit.php">Modifier une news</a> 
<form method="post" action="edit.php"/>
<input name="id" type="hidden" value="<?php echo $data'id']; ?>/>
    Titre :<input type="text" name="titre" value="<?php echo $data"titre"]; ?>/>
    <br/>
    Contenu :<br/>
    <textarea name="contenu" style="width:100%; height:200px;" value="<?php echo $data"contenu"]; ?>></textarea>
    <input type="submit" value="Valider la News"/>
 </form>

et mon admin_index.php

<form method="POST" action="creer.php"/>
    <input type="hidden" name="id"/>
    Titre :<input type="text" name="titre"/>
    <br/>
    Contenu :<br/>
    <textarea name="contenu" style="width:100%; height:200px;"></textarea>
    <input type="submit" value="Valider la News"/>
 </form>
<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);

    $sql="SELECT * FROM news";
    $req=mysql_query($sql) or die('erreur sql!<br />'.$sql.'<br />'.mysql_error());
    while($data=mysql_fetch_assoc($req)){
        echo "<p>{$data"titre"]} --</p>";
        $monid= $data'id']; echo "<a href=edit.php/?id='$monid'>Modifier cette news</a>";
        echo $monid;
        //echo " -- <a href=\"supprimer.php?id={$data"id"]}\">x</a>";//
        //echo"</p>";//

    }
?>

et lorsque je vvais sur http://localhost/admin/edit.php/edit.php j'ai ce message d'erreur:

en tout cas merci pour ton aide

patou, il y a 13 ans

Voila j'ai récupéré tes fichiers pour les travailler en local
j 'ai un peu modifié le fichier edit.php cela fonctionne bien chez moi.

<?php
    require "../config.php";
    mysql_connect(DB_HOST,DB_LOGIN,DB_PASS);
    mysql_select_db(DB_BDD);
    if(!empty($_POST)){
        extract($_POST);
        $sql="UPDATE news SET titre='$titre', contenu='$contenu' WHERE id=$id";
        $req = mysql_query($sql) or die('erreur SQL!<br />'.$sql.'<br />'.mysql_error());
        echo "<p>News Modifiée</p>";
        echo '<a href="index.php">Retour</a>'; 
        $_GET"id"]=$id;
    }

    if(!empty($_GET)):
    $test = $_GET'id']  ;

?>
<h3>Modifier une news</h3>
<form method="post" action="edit.php"/>
<?php $sql = "SELECT * FROM news WHERE id='$test'"; 
    $req=mysql_query($sql) or die('erreur SQL!<br />'.$sql.'<br />'.mysql_error());
    while($data=mysql_fetch_assoc($req)):
    ?>
    <input name="id" type="hidden" value="<?php echo $data"id"]; ?>"/>
    Titre :<input type="text" name="titre" value="<?php echo $data"titre"]; ?>"/>
    <br/>
    Contenu :<br/>
    <textarea name="contenu" style="width:100%; height:200px;" value=""><?php echo $data"contenu"];?></textarea>
    <?php endwhile ;?>
    <?php endif ;?>
    <input type="submit" value="Valider la News"/>
 </form>
neuillane, il y a 13 ans

encore une fois merçi pour ton aide Patou.
j'ai tout simplement fait un copier-coller dans le fichier edit.php et voila ce que ça me dit...

Modifier une news
erreur SQL!
SELECT * FROM news WHERE id=''1''
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1''' at line 1

ça ne pourrait pas etre mon server wamp qui fonctionne mal?

neuillane, il y a 13 ans

j'ai hebergé sur free => ici pour que tu puisses voir le resultat.
Plus d'erreur? mais faut j'essaye sur wamp ou l'erreur du dessus persistait et je te dirai si elle est toujours d'actualité.

merci pour ton aide et ta patience...

patou, il y a 13 ans

Bonjour, essaye avec ceci ligne 22 du fichier edit.php

<?php $sql = "SELECT * FROM news WHERE id=$test";
neuillane, il y a 13 ans

merci Patou ca marche