我有一个带有表单的简单页面(如下所示)。如果action='ask.php‘,表单将提交。(ask.php是当前页面,下面是代码)。当我将ask.php更改为question.php (文件夹中的另一个页面)时,表单不会提交。为什么会这样呢?是否在将数据放入数据库之前重定向表单?
ask.php代码;
<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
<?php include '../nav.php';
?>
<?php
if (isset($_POST['submit'])){
include '../connect.php';
$question=mysql_real_escape_string($_POST['question']);
$detail=mysql_real_escape_string($_POST['detail']);
$date=date("d M Y");
$time=time();
$user=$_SESSION['id'];
$put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')");
$result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
while ($row = mysql_fetch_assoc($result)){
$q=$row['id'];
}
}
?>
<form method='POST' action='question.php'> //won't submit because action is question.php?
<p>Question:</p>
<p><input type='text' name='question' id='question' maxlength='200'></p>
<p>Add some detail (optional):</p>
<p><textarea id='detail' name='detail' ></textarea></p>
<p><input type='submit' value='submit' name='submit'></p>
</form>
</div>
<?php include '../footer.php'; ?>
</body>
发布于 2012-02-19 20:10:24
Jeroen是对的:
操作页面应包含此类信息,以便将信息发送到数据库:
第一部分和第二部分准备将数据插入到数据库中:
$question = mysql_real_escape_string($_POST['question']); // Part 1
$detail = mysql_real_escape_string($_POST['detail']); // Part 2
$con = mysql_connect("server","username","password");
mysql_select_db("YourDatabase", $con);
$sql="INSERT INTO YourTable (question, details) VALUES ('$question','$detail')";
if (!mysql_query($sql,$con)) {
//Some error handling here,
// Stay away from using die in all but development
}
mysql_close($con)您也应该使用preg_match测试格式,以检查数据是否为预期的格式。希望这能有所帮助
编辑!
这种连接方式已经过时了!使用PDO进行数据库连接。一些文档
https://stackoverflow.com/questions/8650860
复制相似问题