【问题标题】:MariaDB server version for the right syntax to use near '1' [duplicate]MariaDB 服务器版本,用于在“1”附近使用正确的语法 [重复]
【发布时间】:2017-09-30 17:54:29
【问题描述】:

<html>
<body>
<form method = 'post' action= 'pt.php'>
<input type = 'text' name = 'name' placeholder = 'name'><br>
<textarea name = 'comment' cols = '40' rows = '10' placeholder = 'comment'></textarea><br>
<input type = 'submit' value = 'submit' name ='submit'><br>
</form>
</body>
</html>


<?php
require 'submit.php';

if (isset($_POST['submit'])){
	$name= mysqli_real_escape_string($conn, $_POST['name']);
	$comment = mysqli_real_escape_string($conn, $_POST['comment']);
	if (empty($name) || empty($comment)){
		echo "all fields must be filled<br>";	
		}else{
			$sql=$conn->query("INSERT INTO komen (nama, komment) VALUES ('$name','$comment')");
			if($conn->query($sql) == TRUE){
				echo "berhasil ditambahkan";
			}
			else
			{
				echo "error".$sql."<br>".$conn->error;
			}
			
		}
}
		if($result=$conn->query("SELECT * FROM komen")){
			$count = $result->num_rows;
			echo 'Total '.$count.' komen <br><br>';
			while ($rows=$result->fetch_object()){
				echo '<strong>'.$rows->nama.'></strong>'.'<br>'.$rows->komment.'<br><br><br>';
			}
$conn->close();
}
?>

我仍然收到错误“error1 您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行“1”附近使用的正确语法。

你能帮我解决这个问题吗? 非常感谢。

【问题讨论】:

  • 给出的答案都没有解释为什么你会得到near '1'。查阅副本;它已被解释,让您了解“为什么”。

标签: php forms mysqli isset


【解决方案1】:

试试这个方法

$sql="INSERT INTO komen (nama, komment) VALUES ('$name','$comment')";
    if($conn->query($sql) == TRUE){
        echo "berhasil ditambahkan";
    }
    else
    {
        echo "error".$sql."<br>".$conn->error;
    }

【讨论】:

  • 太棒了!!问题解决了。非常感谢!!!
  • 干杯。请投票并标记正确。
【解决方案2】:

尝试打印您的 SQL 语句。

die("INSERT INTO komen (nama, komment) VALUES ('$name','$comment')");

【讨论】:

    【解决方案3】:

    尝试更改以下内容

    $sql=$conn->query("INSERT INTO komen (nama, komment) VALUES ('$name','$comment')");
    

    $sql=$conn->query("INSERT INTO komen (nama, komment) VALUES ('".$name."','".$comment."')");
    

    【讨论】:

    • 我改变了它。 $name 和 $comment 成功插入,但错误消息仍然出现,而不是回显“berhasil ditambahkan”(成功添加)。
    • 你能提供你在列中插入的值吗
    • 为什么这个答案被否决了?
    猜你喜欢
    • 2017-08-26
    • 2017-10-25
    • 2022-11-12
    • 2018-01-20
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多