我一直在嘗試,但我不知道我的代碼有什麼問題。Mysql INSERT INTO沒有插入到數據庫中,但沒有使用
<?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>
我不知道錯誤是我用盡了一切。它也只是這個頁面。當我爲其他任何事情做到這一點時,它的工作就很好。
注意到你有''和' UnholyRanger 2013-03-05 21:28:39