如何将表单数据传递到另一个页面和数据库?

时间:2017-05-02 04:42:43

标签: php mysql

实际上,我试图按用户建议的名称创建一个表,并根据用户的建议将数据插入到该表中。

我有两个php文件: CreateTable.php EnterData.php

这是 CreateTable.php 的代码:

<?php 

    $conn = new mysqli("localhost","root","","mywebsite");

    if (isset($_POST['tbButton'])) {
        $qry = "Create Table ".$_POST['tableName']."(firstname varchar(25),lastname varchar(25));";
        $res = mysqli_query($conn,$qry);
        if ($res) {
            echo "Table Created!";
        }
        else{
            die("query failed!");
        }
    }

 ?>
 <!DOCTYPE html>
 <html>
 <head>
    <title>Create Table</title>
 </head>
 <body>
    <form action="EnterData.php" method="post">
        <p><input type="text" name="tableName" placeholder="Enter Table Name..."></p>
        <p><input type="submit" name="tbButton"></p>
    </form>
 </body>
 </html>

这是 EnterData.php 的代码:

<?php 
    $tbname = $_POST['tableName']; 
    $conn = new mysqli("localhost","root","","mywebsite");

    if (isset($_POST['dataButton'])) {

        $qry = "Insert into ".$tbname."(firstname,lastname) values('".$_POST['firstname']."','".$_POST['lastname']."');";
        $res = mysqli_query($conn,$qry);
        if ($res) {
            echo "Data Inserted!";
        }
        else{
            die("query failed!");
        }
    }

 ?>
 <!DOCTYPE html>
 <html>
 <head>
    <title>Create Table</title>
 </head>
 <body>
    <form action="" method="post">
        <p><input type="text" name="firstname" placeholder="Enter First Name..."></p>
        <p><input type="text" name="lastname" placeholder="Enter Last Name..."></p>
        <p><input type="submit" name="dataButton"></p>
    </form>
 </body>
 </html>

问题是指当我写action="EnterData.php"时,表格不会在数据库中创建,但表单值会传递给'EnterData'文件。 当我写action="CreateTable.php"表时,会在数据库中创建表,但值不会传递给&#39; EnterData'文件。 我也希望将值传递给EnterData文件和数据库。

这是我第一次尝试stackoverflow,希望我能很好地解释我的问题

2 个答案:

答案 0 :(得分:0)

为什么要让用户首先在数据库中创建表(具有root权限!)?

关于你的问题......两个php文件都提交给EnterData.php(即如果EnterData.php的blank action attribute被浏览器正确解释),那么你的CreateTable.php不知道是什么{{ 1}}是。

我不知道你想要做什么,但是php文件并没有神奇地了解彼此的变量 - 你实际上必须include另一个文件让他们分享一个变量集,通过$_REQUEST传递变量或使用AJAX来处理事情。

我个人建议使用大写字母$_POST['tableName']GET whenever possible

答案 1 :(得分:0)

您可以通过get方法传递您的tablename

CreateTable.php

<?php 

$conn = new mysqli("localhost","root","","mywebsite");
$tableName = $_POST['tableName'];

if (isset($_POST['tbButton'])) {
    $qry = "Create Table ".$tableName ."(firstname varchar(25),lastname varchar(25));";
    $res = mysqli_query($conn,$qry);
    if ($res) {
        header("Location: EnterData.php?tableName=".$tableName);
    }
    else{
        die("query failed!");
    }
}

?>
<!DOCTYPE html>
<html>
<head>
  <title>Create Table</title>
</head>
<body>
  <form action="CreateTable.php" method="post">
    <p><input type="text" name="tableName" placeholder="Enter Table Name..."></p>
    <p><input type="submit" name="tbButton"></p>
  </form>
</body>
</html>

EnterData.php

<?php 
   $tbname = $_GET['tableName']; 
   $conn = new mysqli("localhost","root","","mywebsite");

   if (isset($_POST['dataButton'])) {

     $qry = "Insert into ".$tbname."(firstname,lastname) values('".$_POST['firstname']."','".$_POST['lastname']."');";
     $res = mysqli_query($conn,$qry);
     if ($res) {
        echo "Data Inserted!";
     }
     else{
        die("query failed!");
     }
  }

?>
 <!DOCTYPE html>
 <html>
 <head>
    <title>Create Table</title>
 </head>
 <body>
   <form action="EnterData.php?tableName=<?php echo $tbname;?>" method="post">
     <p><input type="text" name="firstname" placeholder="Enter First Name..."></p>
     <p><input type="text" name="lastname" placeholder="Enter Last Name..."></p>
     <p><input type="submit" name="dataButton"></p>
    </form>
  </body>
 </html>