duanjing4623 2013-06-05 05:14
浏览 6
已采纳

按表格更新数据库[关闭]

<!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>

This is my code i want to run my query on this very same page! I cannot do it on other page because i want to add users recursively in this page .I called function func on click to do that but its not working.

  • 写回答

3条回答 默认 最新

  • dongqiancui9194 2013-06-05 05:38
    关注

    Try something like this

    <!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
    ";
    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条)

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答