【发布时间】:2015-12-11 16:47:17
【问题描述】:
好的朋友们,我对这个基本的东西很困惑。我已经阅读了很多帖子,上面写着,我需要添加 SELF POST 或其他内容,但我不明白。
我有两个文件,index.html 和 submit.php。 index.html 有一个带有提交按钮的表单,单击该按钮会调用 submit.php 文件并显示一条消息“添加了 1 条记录”。我想从 submit.php 文件重定向回 index.html 文件。我不知道我做错了什么。是因为一个是html文件,另一个是php吗?请帮忙。这是我的代码
index.html 文件
<form method="post" action="submit.php">
提交.php文件
<?php
$con = mysql_connect("localhost","myuser","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$sql="INSERT INTO members (sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$_POST[name]', '$_POST[city]', '$_POST[mobile]', '$_POST[email]', '$_POST[sub]', '$_POST[slogan]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
编辑
请查找 index.html 和 submit.php 文件的代码。在您的帮助下,两者都能完美运行。我仍在努力将验证代码放在哪里。我在 html 文件中使用 html5 输入类型,但我不知道在 submit.php 文件中究竟在哪里进行验证。是的,我确实有多种表格,并且按照您的建议制作了validations.php 文件。我不明白的是,如果您在validations.php 文件中有名称字段的函数validate_name($input),那么您为什么要在submit.php 中再次验证名称? ( if (!empty($_POST['name']) )。我也不明白错误消息会显示在哪里?如果我尝试添加这些功能,它会在单击提交时给我空白页,并且数据不会转到数据库。
您能否在 submit.php 文件中建议一个位置,我应该通过编辑 submit.php 文件来添加这些验证?
电子邮件的正则表达式 ('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/')
电话正则表达式 (^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$)
这是我的 index.html 文件
<!DOCTYPE HTML>
<html>
<head>
<title>My Title</title>
</head>
<body>
<form method="post" action="submit.php">
<div class="box">
<div class="cl"><input type="text" name="name" placeholder="Name" /></div>
<div class="cl"><input type="text" name="city" placeholder="City" /></div>
<div class="cl"><input type="text" name="mobile" placeholder="Mobile" /></div>
<div class="cl"><input type="email" name="email" placeholder="Email" /></div>
<div class="cl"><input type="text" name="sub" placeholder="Want 3 m Free Subscription (Yes/No)?"></textarea></div>
<div class="cl"><input type="text" name="slogan" placeholder="Suggest a slogan for 6 m subscription"></textarea></div>
</div>
<div class="srow">
<div class="cl1">
<ul class="action">
<li><input type="submit" value="Submit" /></li>
</ul>
</div>
</div>
</form>
</body>
</html>
这是我从你那里得到的 submit.php 文件帮助
<?php
include 'config.php'; // store your configuration in a seperate file so
// you only need to update it once when your environment changes
$errors = false;
$output = '';
$nl = '<br>'.PHP_EOL;
$redirect_url = 'index.html';
if (!$con = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME)){
$errors = true;
$output .= "ERROR Can't connect to DB".$nl;
};
if (!$errors){
//should validate/clean $_POST before using in query
$name = $con->escape_string($_POST['name']);
$city = $con->escape_string($_POST['city']);
$email = $con->escape_string($_POST['email']);
$mobile = $con->escape_string($_POST['mobile']);
$sub = $con->escape_string($_POST['sub']);
$slogan = $con->escape_string($_POST['slogan']);
$sql="INSERT INTO members
(sName, sCity, sMobile, sEmail, sSub, sSlogan)
VALUES ('$name', '$city', '$mobile', '$email',
'$sub','$slogan')";
if (!$con->query($sql)){ //forgot a parenthesis here earlier
$output .= 'ERROR: DB said: ('.$con->errno.') '.$con->error.$nl;
$output .= 'Query was:'.$sql.$nl;
$errors = true;
}else{
$output .= "1 record added".$nl;
}
}
if (!$errors){
//if there are no errors redirect to index.html;
header('refresh: 2; URL='.$redirect_url);
$output .= '...Redirecting...'.$nl;
}else{
//show the errors and allow display a link to go back/try again
$output .= '<a href="'.$redirect_url.'">Try again</a>'.$nl;
}
echo $output;
?>
PS:我观察到的一件事是 html5 (input type="email") 在您转到下一个字段后立即显示无效的电子邮件警报,就在该字段的下方。怎么可能在所有领域都做到这一点? (类似于字段丢失焦点的验证检查)
谢谢
【问题讨论】:
-
你的重定向在哪里?
-
首先了解一下sql注入。其次 - 不推荐使用 mysql,请改用 mysqli 或 PDO。第三 - 将 index.html 重命名为 index.php,在 submit.php 之后 mysql_close($con) 添加 header("Location: localhost/index.html?success=1 (or what url do you have)")。在 index.php 添加 1 记录添加
-
你有一个语法错误,你很可能在标题之前输出