【问题标题】:PHP SQL UPDATE problem and undefined variables [closed]PHP SQL UPDATE问题和未定义的变量[关闭]
【发布时间】:2020-10-25 09:57:24
【问题描述】:

我正在就两个我无法解决的问题寻求您的帮助。 第一个不是真正的问题,而是很烦人:

?php
session_start();
 $_SESSION['pseudo'];
 $CAT="";
//tentative de connexion à la base de donnée 
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=espace_membre;charset=utf8', 'root', '');
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage()); //message d'erreur au cas où la connexion échoue
}
if(isset($_SESSION['id']))
{   //echo "ok";

 }
else
{
  //echo "lol";
  header('location:connexion.php');
} 
if(isset($_GET['id']) AND $_GET['id'] > 0)
{
  $getid=intval($_GET['id']);
  $requser= $bdd -> prepare('SELECT * FROM membres WHERE id= ?');
  $requser->execute(array($getid));
  $userinfo=$requser->fetch();
}
function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
         }
$CAT = test_input($_POST["categorie"]);
$TYPE= test_input($_POST["typeannonce"]);
$WILA= test_input($_POST["wilaya"]);
$fk_membres_id=$_SESSION['id'];
 if(isset($_POST['revenirprofil']))
 {
  header("Location: profil.php?id=".$_SESSION['id']);
 }
if(isset($_POST['article_titre']) AND isset($_POST['article_description']) AND isset($_POST['categorie']) AND isset($_POST['wilaya']) AND isset($_POST['typeannonce']) )
  {
     $article_titre=htmlspecialchars($_POST['article_titre']);
     $article_description=htmlspecialchars($_POST['article_description']);
     ///insertion dans la BDD /////
     $ins=$bdd->prepare('INSERT into articles (titre_article, description,date_publication,catégorie,type_article,wilaya,fk_membres_id) VALUES(?,?, NOW(),?,?,?,?)' );
     $ins->execute(array($article_titre,$article_description,$CAT,$TYPE,$WILA,$fk_membres_id));
     //header("Location: profil.php?id=".$_SESSION['id']);
  }
  else
  {
    $erreurAE = "veuillez remplir tous les champs du formulaire d'ajout";
  }
?>
<!DOCTYPE html>
<html>
<head>
  <title>Ajouter une annonce</title>
  <meta charset="utf-8">
</head>
<body>

  <h3>Bonjour <?php echo  $_SESSION['pseudo'] ?> ajoutez une annonce TROKI par ici : </h3>
  <h3>Bonjour <?php echo  $_SESSION['id'] ?> ajoutez une annonce TROKI par ici : </h3>
  <h3>Ajoutez une annonce <?php echo  $_SESSION['email']?></h3>
  <a href="editionprofil.php"> éditer mon profil</a> 
             <a href="modifiermdp.php">modifier mon mot de passe</a>
             <a href="profil.php">mon profil</a>
             <a href="deconnexion.php"> se déconnecter</a>
    
    <div align="center">
  <form method="POST">
     <label>titre de votre annonce</label>
    <input type="text" placeholder="titre de votre annonce" name="article_titre" /> <br/>
     <label>description de votre annonce</label>
    <textarea name = "article_description" rows = "5" cols = "40" placeholder="décrivez votre annonce en insistant sur les mots clés pour attirer le plus de visiteurs possible"> </textarea> <br/>
    <label>veuillez seletionner la catégorie de votre article</label>
    
                   <input type="radio" name="categorie" value="Livres">Livres
                   <input type="radio" name="categorie" value="Sport">Sports en tout genre
                    <br/> <br/>
                    <label>veuillez seletionner la catégorie de votre article</label>
                   <input type="radio" name="wilaya" value="25">Constantine
                   <input type="radio" name="wilaya" value="31">Oran
                   
                    <br/> <br/>
               <td>Que souhaitez-vous faire de votre objet ?:</td> <br/>
               <td>
                  <input type = "radio" name = "typeannonce" value = "vente">vendre seulement
                  <input type = "radio" name = "typeannonce" value = "échange">troquer seulement
                  <input type = "radio" name = "typeannonce" value = "indécis">Je suis indécis 
               </td>
               <br/>
               <input type="submit" value="envoyer l'article" >

                                         <?php 
                          if (isset($erreur))
                          {
                            echo $erreur;
                          }
                          ?> <br/>
       
                          <?php 
                          if (isset($erreur))
                          {
                            echo $erreur;
                          }
                          ?>
<button name="revenirprofil">revenir au profil</button>


  </form>
</body>
</html>

嗯,页面显示三个错误:

注意:未定义索引:第 44 行 C:\wamp\www\projet3\formulaireajout.php 中的类别 注意:未定义索引:第 45 行 C:\wamp\www\projet3\formulaireajout.php 中的 typeannonce 注意:未定义索引:第 46 行 C:\wamp\www\projet3\formulaireajout.php 中的 wilaya

但令人惊讶的是,代码仍然有效,并且 3 条通知在完成表单并将其发送到数据库后消失了。除了神秘的 3 个不是真正错误的错误之外,一切都运行良好

我的第二个问题可能看起来像 SQL 查询中的输入错误,但仍然无法找到问题所在。

我正在尝试使用以前的表单更新发送到数据库的信息。

这里是 SQL:

$update=$bdd->prepare('UPDATE articles SET titre_article= ?, description=?,catégorie=?,type_article=?,wilaya=?,fk_membres_id=? WHERE id = ?' );
     $update->execute(array($article_titre,$article_description,$CAT,$TYPE,$WILA,$fk_membres_id,$fk_membres_id));
      die('Edit successful');

我得到“编辑成功”,但我的数据库中仍然没有进行任何更改。您通常希望将更改应用于所需的行,但似乎没有任何变化

感谢您的阅读。 (希望这不是我在查询中遗漏的东西)

【问题讨论】:

标签: php mysql pdo prepared-statement


【解决方案1】:

这是因为无论您是否提交了表单,您的 php 代码都会在页面加载后立即执行。在您尚未提交表单之前,$_POST 全局变量 无法访问 categorie、typeannoance 和 wilaya。

当您提交它时,$_POST 全局变量可以访问这些值,这就是这些通知消失的原因。

尝试首先使用 isset() 函数检查它们的存在,这应该可以解决您的问题

【讨论】:

  • 感谢您的回答,Tushar。但似乎问题仍然存在。
  • 你能分享一下你修改的代码sn-p吗?
  • 当然! :) &lt;?php session_start(); $_SESSION['pseudo']; $CAT=""; if (isset($_POST["categorie"])) { $CAT = test_input($_POST["categorie"]); } 嗯,你的第一个建议是我在发帖之前想到的,它对我来说仍然有意义,但是......错误仍然在这里感谢你的帮助:)
  • 对我来说一切都很好。也许您可以在执行任何操作之前尝试检查表单提交:)
  • 是的,我不得不承认在 Stackoverflow 上提问之前,我只尝试了两三个想法。稍后将对此进行深入检查。感谢您宝贵的时间,Tushar!
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多