我有一个简单的页面(下面)和表格。如果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>
答案 0 :(得分:0)
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进行数据库连接。 一些文档