php


How to pass form data to another page and database both?


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

Related Links

MySQLI - INSERT or UPDATE on duplicate, multiple users
How to fetch all unread mail with attachment as an array ? i am using following code
If statement within Wordpress
Is there any reason to use the “public” keyword before method and member variable names? [duplicate]
Get URL variable in twig
Crawling a website using Laravel & Elvedia\Goutte: How to extract JSON
You have requested a non-existent service “user_service”
php simple dom parser get parent of element
JSON_NUMERIC_CHECK and phone numbers
PHP only displaying ID and not the value from mysql [closed]
PHP: DateTime '-1 day'
Cookie Not Available Sitewide
How to dispay previous orders made via PayPal for each customer
get element name from complex array in php
Redirect function to reload the same page
PHP, class variables and var

Categories

HOME
artificial-intelligence
blast
windows-8.1
vagrant
selenium-builder
writefile
kivy
ews
ios-simulator
typeerror
google-play-services
jint
minecraft
native-base
telegraf
scheduler
swarm
nexus3
vimeo
orc
mailmerge
phonegap-build
pywinauto
exe
salt-cloud
spring-ldap
yii2-basic-app
activeadmin
generator
chronicle-queue
jstree
introspection
adapter
amazon-cloudtrail
substance
infopath2010
epplus
devtools
markov-chains
restful-architecture
java-stream
formsauthenticationticket
android-alarms
telephonymanager
aurelia-http-client
recovery
text-decorations
widestring
iscroll
windows-store
hypothesis-test
linq-to-excel
viewmodel
windows-media-player
cordova-ios
mongodb-aggregation
portability
alertify
msxml
cognos-tm1
thread-exceptions
xmlbeans
livereload
seccomp
oci
flask-security
webgrind
arbre
cfwheels
pythonxy
canopy
netdatacontractserializer
ssmtp
cakephp-2.2
inputbox
tabris
visual-c++-2012
osascript
accessory
mfmessagecomposeview
pageviews
hotswap
principalcontext
frontbase
bcdedit
itunes-sdk
significant-digits
dojo-1.9
gprof
email-verification
gitx
playorm
popup-blocker
border-box
viewpagerindicator
pageheap
hardcode
will-paginate
kohana-auth
directshow.net
regression-testing
formal-semantics
activestate
wmd-editor
icon-language

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App