1

Actually i'm trying to create a table by name that user suggests and insert data into that table, also by user's suggestion.

I've two php files: CreateTable.php and EnterData.php

Here is my code of 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>

Here is my code of 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>

Problem is that when I write action="EnterData.php" Table doesn't create in database but form values passes to 'EnterData' file. and when I write action="CreateTable.php" table is created in database but values doesn't pass to 'EnterData' file. I want to pass values to EnterData file and database too.


this my first attempt on stackoverflow, hope i explained my question very nicely

Momin
  • 3,200
  • 3
  • 30
  • 48
  • why need user defined table name ? what is the purpose ? . take care about that user can able to spam you creating bunch of table . – JYoThI May 02 '17 at 04:53
  • My dear friend! I know, in professional coding user shouldn't have this kind of access. I am just practicing backend development and i'm curious about that **can i pass form values to both database and to another page at the same time** if yes then how can i do that??? – Manzar Gulzar May 02 '17 at 05:06

2 Answers2

0

Why would you let the user create tables in your database in the first place (with root privileges!)?

As for your question... Both php files submit to EnterData.php (that is if EnterData.php's blank action attribute is properly interpreted by the browser), so your CreateTable.php has no idea of what $_POST['tableName'] is.

I don't know what it is you are trying to do, but php files don't magically get to know each other's variables - you actually have to include a file in another one to let them share a set of variables, pass the variables through $_REQUEST or use AJAX to take care of things.

I would personally recommend using uppercase for GET and POST whenever possible.

Community
  • 1
  • 1
Pyromonk
  • 684
  • 1
  • 12
  • 27
  • it's an honor for me that you tried to answer my question. It would be helpful for me if try to give an example – Manzar Gulzar May 02 '17 at 05:13
  • i'm just curious about that can i pass form values to both database and to another page at the same time?? – Manzar Gulzar May 02 '17 at 05:14
  • @ManzarGulzar, yes, you can. The easiest way for you would probably be... Let's say you have A.php and B.php. A.php has a form with `action="B.php"` and B.php has a database insert statement. You'd be able to access values from A.php's form inside B.php by using the $_REQUEST['name'] syntax, where "name" is the name attribute of the input from A.php's form. To be honest, I am not quite sure what you are having a problem with. – Pyromonk May 02 '17 at 05:20
0

You can pass your tablename through get method

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>
Chinito
  • 1,085
  • 1
  • 9
  • 11