按表单更新数据库

时间:2013-06-05 05:14:08

标签: php html function html-form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="admin page.php">
   <p>
     <label for="Name">Name</label>
     <input type="text" name="Name" id="Name" />
   </p>
   <p>
     <label for="Age">Age </label>
     <input type="text" name="Age" id="Age" />
   </p>
   <p>
     <label for="Email">Email</label>
     <input type="text" name="Email" id="Email" />
   </p>
   <p>
     <label for="DOB">DOB</label>
     <input type="text" name="DOB" id="DOB" />
   </p>
   <p>
     <input type="button" name="add" id="add" value="Add" onclick="func();" />
   </p>
</form>

<?php 

 function func()
 {
  $con = mysql_connect("127.0.0.1","root","") or die(mysql_error());
  $db = mysql_select_db("lr") or die(mysql_error());
  $sql=mysqli_query("INSERT INTO Persons (Name,age,Email,DOB)
                                       VALUES ('&Name', '&Age','&Email','&DOB')");
  if($sql){echo "Welcome ".$Name.",you may Login now! ";}

   mysql_close($con);    
 }

?>
</body>

这是我的代码,我想在同一页面上运行我的查询!我无法在其他页面上执行此操作,因为我想在此页面中递归添加用户。我在单击时调用了函数func,但它无效。

3 个答案:

答案 0 :(得分:2)

在代码中尝试更正/更改:

  • 表格中的行动: <form id="form1" name="form1" method="post" action="admin page.php"> - admin和page.php之间没有空格!

  • func()。你不能用onclick调用php。只需删除该行onclick="func();"即可。表单提交操作将执行您想要的操作。并删除func() {}并只留下php

  • INSERT:将&更改为$并连接您的变量。例如,我强烈建议您为表单输入添加一些验证,以避免sql注入。

  • 从输入字段中获取变量:使用$Age = $_POST["Age"]从表单帖子中获取变量。

  • 如果您只是在查询中将&替换为$,则您的代码很容易被sql injection攻击,您需要正确转义mysql_*中您可以使用的所有请求mysql_real_escape_string()

  • Mysql_* api is deprecated因此请使用PDOMySQLi,然后使用PDO

  • 您还混合了不同的api mysqli_*mysqli_query)和mysql_* api无法正常工作

答案 1 :(得分:2)

尝试这样的事情

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

   $name= $_POST['Name'];
   $age= $_POST['Age'];
   $email= $_POST['Email'];
   $dob= $_POST['DOB'];

$sql = "INSERT INTO employee ".
       "(name,age, email, dob) ".
       "VALUES('$name','$age','$email','$dob')";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form id="form1" name="form1" method="post" action="<?php $_PHP_SELF ?>">
   <p>
     <label for="Name">Name</label>
     <input type="text" name="Name" id="Name" />
   </p>
   <p>
     <label for="Age">Age </label>
     <input type="text" name="Age" id="Age" />
   </p>
   <p>
     <label for="Email">Email</label>
     <input type="text" name="Email" id="Email" />
   </p>
   <p>
     <label for="DOB">DOB</label>
     <input type="text" name="DOB" id="DOB" />
   </p>
   <p>
     <input type="submit" name="add" id="add" value="Add" />
   </p>
</form>
<?php
}
?>
</body>
</html>

答案 2 :(得分:0)

您可以使用这样的ajax执行此操作:

//Submit form without refreshing page
 $.ajax({
        type: "POST",
        url: "update.php",
        data: dataString,
        success: function(){
            $('.success').fadeIn(200).show();
            $('.error').fadeOut(200).hide();
        }
    });

请参阅HEREjQuery.ajax()

DEMO