【发布时间】: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');
我得到“编辑成功”,但我的数据库中仍然没有进行任何更改。您通常希望将更改应用于所需的行,但似乎没有任何变化
感谢您的阅读。 (希望这不是我在查询中遗漏的东西)
【问题讨论】:
-
catégorie不是未加引号的有效列名。使用反引号或更改名称。摆脱test_input并停止使用您从中获得的任何教程。您应该查看stackoverflow.com/questions/3999850/pdo-error-message,以便从 PDO 中获取错误。 -
第一个错误是因为在使用之前没有检查索引是否设置。
-
嗨!我没有任何 PDO 错误。 SQL 查询正在执行,而我的数据库没有明显的变化。如果你有一个更好的替代“test_input”,我很乐意接受! :) 感谢您的链接
标签: php mysql pdo prepared-statement