Php 如何添加提交按钮的操作
请回答我的业余问题。我编写了以下代码,用于从输入获取文本并将外接程序添加到数据库:Php 如何添加提交按钮的操作,php,database,form-submit,submit-button,Php,Database,Form Submit,Submit Button,请回答我的业余问题。我编写了以下代码,用于从输入获取文本并将外接程序添加到数据库: <html xmlns="http://www.w3.org/1999/xhtml" lang="fa"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Title</title>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Title</title>
</head>
<body>
<form action="" method="POST">
<label for="name" id="app_name_label" >name</label>
<input type="text" name="name" id="name_textfield">
<input type="submit" name"Submit" value="send" >
</form>
<?php
if(isset($_POST['Submit']))
{
include_once("config.php");
$con = mysql_connect($db_host,$db_user,$db_pass)
or die(mysql_error());
$selected=mysql_select_db($db_name, $con)
or die(mysql_error());
if($selected){
$name = $_REQUEST['name'];
$ins = "INSERT INTO infos (app_name ) VALUES ('$name')";
$saved=mysql_query($ins );
if($saved)
{
echo "Saved!!";
}
else
{
echo "Don't Saved!!";
}
}
mysql_close($con);
}
?>
</body>
</html>
我的头衔
名称
如果其他一切正常,那么我担心您的代码中有一个简单的输入错误,这会导致您出现问题
<input type="submit" name"Submit" value="send" >
应该是
<input type="submit" name="Submit" value="send" >
您在姓名后的代码中错过了等号。:)
另外,当表单方法为POST时,不应使用$\u REQUEST['name']
,而应使用$\u POST['name']
。它更安全。其次,尝试使用数据库扩展,而不是使用mysql。从PHP5.5开始,mysql扩展就被弃用。请参考,最好在正文上方使用PHP标记,并在会话中显示错误,另外,为什么在执行提交后只包含一次(“config.PHP”),而不是在加载实际文件时?这是一个脚本,它与您试图实现的功能相同,但处理效果要好得多
<?php
include_once("config.php");
if (isset($_POST['Submit'])){
$err = array();
$name = $_POST['name'];
if(!$name){
$err[] = 'All fields required';
}
if(!count($err)){
$name = mysql_real_escape_string($name); //Make sure you escape unwanted chars
$row = mysql_fetch_assoc(mysql_query("INSERT INTO infos (app_name ) VALUES ('$name')"));
if(mysql_affected_rows() == 1)
{
$_SESSION['msg']['succ'] = 'Sent!';
header("Location: index.php");
} else{
$_SESSION['msg']['err'] = 'Noooooo!';
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Title</title>
</head>
<body>
<form action="" method="POST">
<?php
// Lets output message
if($_SESSION['msg']['err'])
{
echo $_SESSION['msg']['err'];
unset($_SESSION['msg']['err']);
}
if($_SESSION['msg']['succ'])
{
echo $_SESSION['msg']['succ'];
unset($_SESSION['msg']['succ']);
}
?>
<label id="app_name_label">Name</label>
<input type="text" name="name" id="name_textfield">
<input type="submit" name="Submit" value="send">
</form>
</body>
</html>
我的头衔
名称
代码应采用两种不同的脚本,首先浏览器请求并显示html部分。然后调用表单action
属性中记录的脚本,并将表单内容发送到那里。该文件(一个php文件)处理表单数据。代码不必位于两个单独的文件中。问题是相同的:(@Ahmad yeah,有时会发生这种愚蠢的问题:)