我一直在努力尝试,但我不知道我的代码有什么问题。
<?php
session_start();
include('../database.php');
$to = mysql_real_escape_string($_POST['to']);
$from = $_SESSION['username'];
$msg = mysql_real_escape_string($_POST['msg']);
$subject = mysql_real_escape_string($_POST['subject']);
echo $to, $from, $msg, $subject;
$_SESSION['to'] = $to;
$_SESSION['msg'] = $msg;
$_SESSION['subject'] = $subject;
$err = array( );
$errflag = false;
if ($to = '') {
$err[ ] = 'Missing To.';
$errflag = true;
}
if ($from = '') {
$err[ ] = 'Please Login.';
$errflag = true;
}
if ($msg = '') {
$err[ ] = 'Missing Message.';
$errflag = true;
}
if ($subject = '') {
$err[ ] = 'Missing Subject.';
$errflag = true;
}
if ($errflag = true) {
$_SESSION['mailerr'] = $err;
//header('Location: send.php');
}
$result = mysql_query("INSERT INTO mail(id, subject, to, from, msg) VALUES(NULL,'" . $subject . "', '" . $to . "', '" . $from . "', '" . $msg . "')");
if ($result) {
echo "No Errors";
} else {
echo mysql_error();
}
//header("Location: index.php");
?>
当我手动插入数据而不是变量时,它会起作用,但是当我尝试使用变量时它不会。
也是我得到的错误
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near
'to, from, msg) VALUES(NULL, '', '', '', '')' at line 1
这是send.php
<form method="post" action="insert.php">
<table border="0">
<tr>
<td>Subject:</td>
<td><input type="text" id="subject" name="subject" value="<?php if(isset($_POST['subject'])){echo $_POST['subject'];}else if(isset($_SESSION['subject'])){echo $_SESSION['subject']; unset($_SESSION['subject']);}?>"/></td>
</tr>
<tr>
<td>To:</td>
<td><input type="text" id="to" name="to" value="<?php if(isset($_POST['to'])){echo $_POST['to'];}else if(isset($_SESSION['to'])){echo $_SESSION['to']; unset($_SESSION['to']);}?>"/></td>
</tr>
</table>
<?php if(isset($_POST['old'])){echo "<textarea disabled=disabled rows='8' cols='20'>".$_POST['old']."</textarea><br />";}?>
<input type="text" name="msg" />
<br />
<input type="submit" name="msg" id="msg" value="Send"/>
</form>
</div>
</body>
</html>
我不知道错误是什么我已经尝试了一切。它也只是这个页面。当我为其他一切做到这一点时它运作得很好。
答案 0 :(得分:4)
你使用
if($from = ''){
随处可见。
将其更改为:
if($from == ''){
这样你比较变量,而不是设置它......
另外,在mysql查询中使用保留关键字。反引号可以解决这个问题。
INSERT INTO mail(`id`, `subject`, `to`, `from`, `msg`)
另一个提示:将PDO或mysql用于新项目的预准备语句。如果$ subject中有'
,您也会收到此错误(尽管您似乎逃避了它)。
答案 1 :(得分:0)
TO
和FROM
是mysql的保留关键字
尝试使用反引号
INSERT INTO mail(id, subject, `to`, `from`, msg)