【问题标题】:how to fix pg_query(): Query failed ERROR: syntax error at or near while inserting data using php如何修复 pg_query():查询失败错误:使用 php 插入数据时在或附近出现语法错误
【发布时间】:2020-01-11 13:15:34
【问题描述】:

我有一个脚本,它应该使用 Postgres 在名为 user 的表中插入用户名、电子邮件和评论值,它们是 varchar。单击提交按钮时出现此错误:

警告:pg_query():查询失败:ERREUR: erreur de syntaxe sur ou «用户»前的第 1 行:插入 用户(用户名,电子邮件,comment_value)值(^ in C:\xampp\htdocs\tourist_admin\contact\commentaire.php 在第 14 行

注意:还有一个名为 admin_confirmation 的第四个属性,它可以为空,并且将在编辑过程中由管理员完成,所以我没有将它包含在插入查询中。

我的代码;

<?php
	
	$db = pg_connect( "host = localhost port = 5432  dbname = essaouira_tourisme user = postgres password = 0000");

	if(isset($_POST['submit'])){
		$username = $_POST['username'];
		$email = $_POST['email'];
		$comment_value = $_POST['comment_value'];

		if ($username && $email && $comment_value) {

			$query = "INSERT INTO user(username,email,comment_value) VALUES(
			'".$username."','".$email."','".$comment_value."')";
			$result = pg_query($db,$query);

			if ($result) {
				echo "<h2 color = 'red'>Votre commentaire a été enregistré avec succès</h2>";
			}

		}
		else{
			echo "veuillez remplir tous les champs";
		}

	}
	?>
<html>
   <head>
	<meta charset="UTF-8">
	<title>Contact Form Design</title>
	<link rel="stylesheet" href="style.css">
 </head>
<body>
	<div class="contact-title">
		<h2>vos commentaires nous seront utiles</h2>
		<h1>Donnez vos avis</h1>
	</div>
	<div class="contact-form">
		<form id="contact-form" method="post" action="commentaire.php">
			<input name="username" type="text" class="form-control" placeholder="votre pseudo" required><br>

			<input name="email" type="email" class="form-control" placeholder="votre courriel" required><br>

			<textarea name="comment_value" class="form-control" cols="30" rows="5" placeholder="votre commentaire ici" required></textarea><br>

			<input type="submit" class="form-control submit"  name= "submit" value="ENVOYER">
		</form>
	</div>
	
</body>
</html>
	

【问题讨论】:

    标签: php postgresql-9.6


    【解决方案1】:

    user 是保留字。您需要在表名周围使用反引号才能使用名为 user 的表。

    $query = "INSERT INTO `user` (username,email,comment_value) VALUES(
            '".$username."','".$email."','".$comment_value."')";
    

    【讨论】:

    • 即使我在用户表周围使用刻度,我也遇到了同样的错误,但是通过将用户表重命名为 comment_user 它可以工作。所以谢谢@Dave
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多