我一直在尝试,但我不知道我的代码有什么问题。
<?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>
我不知道错误是什么我已经尝试了一切。也只有这个页面。当我为其他所有事情都这样做时,它工作得很好。