• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Submit form for creating a database

Hi guys!

Id like to modify the below code, so that, I have the following in a web page:

------------------------------------------http://localhost/create.php

Please enter a name of the database you'd like to create:
________________   (testbox to the left to enter name of database)

##########  (Submit button to the left to create the database)

<Outcome here of the result of submitting the request here, on the same page ideally>

Here is my code:

<?php
include 'config.php';
include 'opendb.php';

$query  = "CREATE DATABASE Animals";
mysql_query($query);
$result = mysql_error();

if ($result == "")
{
  echo "Creation of database succeeded!";
}
else
{
  echo "Creation of database failed: $result";
}


include 'closedb.php';
?>
=========================================

0
Simon336697
Asked:
Simon336697
  • 9
  • 9
1 Solution
 
gamebitsCommented:
<?php
include 'config.php';
include 'opendb.php';

if(isset($submit)){

$dbname = $_POST['dbname'];

$query  = "CREATE DATABASE $dbname";
mysql_query($query);
$result = mysql_error();

if ($result == "")
{
  echo "Creation of database succeeded!";
}
else
{
  echo "Creation of database failed: $result";
}
include 'closedb.php';
}else{

echo "<form action=\"create.php\" method=\"POST\">";
echo "Enter the db name: <input type=\"text\" name=\dbname\" size=\"15\">";
echo "<input type=\"submit\" name=\"submit\" value=\"Create\">";
echo "</form>";


?>
0
 
Simon336697Author Commented:
Hi gamebits, thank you mate!

Im getting an error on line 30 of your code gamebits.

Parse error: syntax error, unexpected $end in C:\wamp\www\createdb.php on line 30

0
 
gamebitsCommented:
I missed a }

add a } before the php end tag

echo "<input type=\"submit\" name=\"submit\" value=\"Create\">";
echo "</form>";
}
?>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Simon336697Author Commented:
I feel really stupid gamebits.

In the php script youre helping me with here,  in the line that says...

echo "<form action=\"create.php\" method=\"POST\">";

The "create.php", is this a separate php file to the actual code in this question?

Im just trying to run this script youre helping me and its returning nothing.

Do I need to scripts, a html form, and a php form to handle the form action?

Sorry for the dumb questions
0
 
gamebitsCommented:
There is no dumb question although sometime there is dumb answer.

I use create.php for the form action assuming the page would be named create.php

The logic behind the script is this

if(isset($submit)){ //check if the form has been submitted (by the way the form is at the bottom of the page)

if the form has been submitted the if statement run the script to create the db else it should display the form.

If you don't mind add this lie of code at the top of the page right after the php opening tag

error_reporting(E_ALL);

and try the script again you should get an error messageif something is wrong.

0
 
Simon336697Author Commented:
Hi gamebits!

=============================================== http://localhost/createdb.php

Creation of database failed: Can't create database 'test001'; database exists

Code for this:

<?php
error_reporting(E_ALL);


include 'config.php';
include 'opendb.php';

if(isset($submit)){

$dbname = $_POST['dbname'];

$query  = "CREATE DATABASE $dbname";
mysql_query($query);
$result = mysql_error();

if ($result == "")
{
  echo "Creation of database succeeded!";
}
else
{
  echo "Creation of database failed: $result";
}
include 'closedb.php';
}else{

echo "<form action=\"create.php\" method=\"POST\">";
echo "Enter the db name: <input type=\"text\" name=\dbname\" size=\"15\">";
echo "<input type=\"submit\" name=\"submit\" value=\"Create\">";
echo "</form>";
}

?>

================================================ Create.php

<?php
include 'config.php';
include 'opendb.php';

$query  = "CREATE DATABASE test001";
mysql_query($query);
$result = mysql_error();

if ($result == "")
{
  echo "Creation of database succeeded!";
}
else
{
  echo "Creation of database failed: $result";
}


include 'closedb.php';
?>

==============================================

0
 
Simon336697Author Commented:
I ran in the above, createdb.php which is referencing create.php.

I changed this to just running createdb.php, and changed the line..

echo "<form action=\"create.php\" method=\"POST\">";

to

echo "<form action=\"createdb.php\" method=\"POST\">";
0
 
gamebitsCommented:
You will have to add few line of codes to check if the choosen name exist first.

0
 
Simon336697Author Commented:
Gamebits, the code here, is it split over 2 files?
Or is only 1 file used to do this?
0
 
gamebitsCommented:
"I ran in the above, createdb.php which is referencing create.php."

this is split between two files, although I created mine to be in one file.
0
 
Simon336697Author Commented:
Im totally confused.

0
 
Simon336697Author Commented:
Gamebits..sorry to ask all these questions...

1) Is the submit form, a html form eg.with a .html extension?
What code is in this form?
2) Is the resulting form a php form?
Sorry.
0
 
gamebitsCommented:
Ok lets start over to make it easier to understand I will split the code in 2 different files, sounds good?
0
 
Simon336697Author Commented:
You are so kind......I really appreciate your kindness and patience with me :>)
0
 
gamebitsCommented:
File numero 1 The form

<html>
<head>
<title>Create a Database</title>
</head>
<body>

<form action="create.php" method="POST">
<p>Enter the db name: <input type="text" name="dbname" size="15">
<p><input type="submit" name="submit" value="Create">
</form>

</body>
</html>
0
 
gamebitsCommented:
File numero 2 Handling the form

This file should be named create.php (to match the name use in the form action).

<?php
error_reporting(E_ALL);

include 'config.php';
include 'opendb.php';

$dbname = $_POST['dbname'];

$query  = "CREATE DATABASE $dbname";
mysql_query($query);
$result = mysql_error();

if ($result == "")
{
  echo "Creation of database succeeded!";
}
else
{
  echo "Creation of database failed: $result";
}
include 'closedb.php';

?>
0
 
Simon336697Author Commented:
CHAMPION GAMEBITS!!!!!!!!!!!!!!!!!!!!!

Thank you so much my friend!!

That works great, and I now understand it thanks to you !! :>)

Thanks again Gamebits.
0
 
gamebitsCommented:
Thanks for the points and the grade.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now