0

感谢您的帮助,如果您还需要什么,请告诉我。标题中的详细信息。

<?php

$subject = $_POST['subject'];
$comment = $_POST['comment'];
$submit = $_POST['submit'];

if ($submit)

{
$connect = mysql_connect("host","un","psw");
mysql_select_db("rebeler_comment");

$query = mysql_query("INSERT INTO table VALUES('','$subject','$comment')");
}


?>

<form action="form.php" method="POST">
<label>Subject</label></br>
<input type="text" name="subject"</br>
<label>Comment</label></br>
<textarea name="comment"></textarea></br>
<input type="submit" name="submit" value="Submit">

用我的 html 更新

4

2 回答 2

0

首先,MySQL_不推荐使用 。使用MySQLi_和/或 PDO。

现在,您没有指定"where"将数据放入表中。

假设您的列分别命名subjectcomment

此外,这个词table是一个保留 。因此,如果您的表确实被称为table,则需要在反引号内换行,

like this: `table`

$query = mysql_query("INSERT INTO table (`subject`, `comment`) 
VALUES ('$subject','$comment')");

如果表名被称为“表”:

利用:

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) 
VALUES ('$subject','$comment')");

从中删除'',('','$subject','$comment')因为您在 DB 中只有 2 个值。

您甚至可能想要连接,例如:

VALUES ('" . $subject . "','" . $comment . "')");

要回显成功消息:

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) 
VALUES ('$subject','$comment')");

echo "Data successfully written to DB";
}

else{
echo "Sorry, there was a problem.";
}

编辑2:

<?php
$subject = $_POST['subject'];
$comment = $_POST['comment'];

if(isset($_POST['submit']))
{
$connect = mysql_connect("host","un","psw");
mysql_select_db("rebeler_comment");

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) VALUES ('" . $subject . "','" . $comment . "')");
$retval = mysql_query( $query, $connection ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
echo "Entered data successfully\n";
mysql_close($connection);
}
?>

编辑1:

$query = mysql_query("INSERT INTO `table` (`subject`, `comment`) VALUES ('" . $subject . "','" . $comment . "')");
$retval = mysql_query( $query, $connection ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); }
echo "Entered data successfully\n";
mysql_close($connection);
于 2013-11-04T19:58:49.703 回答
0

修复你的 SQL 插入(在某些事情中),定义要插入的列,而不是盲目地将东西扔到黑暗中。

前任:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

mysql_error()用于获取错误。我还建议利用 php 框架来解决注入问题和其他您可能会错过的手动 mysql 问题。

于 2013-11-04T19:54:51.653 回答