Jump to content

[SOLVED] I can't create a database using a form with mysql_query


giba

Recommended Posts

Hi, I am having a trouble about creating a database directly by form using a variable within CREATE DATABASE line:

 

the form:

 

<form action="create_database.php" method="post">

Type a name to your database: <br />

<input type="text" name="database_name" size="80"><br />

<input type="Submit" value="Create New Database">

 

the php script:

 

<?php

 

$database="$_POST['database_name'];

$con = mysql_connect('server', 'user', 'password');

if (!$con) {

  die('Connection to database failed: ' . mysql_error());

}

 

$sql = 'CREATE DATABASE'.($database); // I've tried all combination on this line and nothing works!

if (mysql_query($sql)) {

  echo "Database ".$banco." created!\n";

} else {

  echo 'None database created: ' . mysql_error() . "\n";

}

?>

 

I would like some friend help me!

Link to comment
Share on other sites

No, the trouble is not the number of databases allowed!

I am trying to build an application to create a database by form, because normally people are taught to create databases directly by using mysql console or direct php script, with a value or variable pre-defined.

My will is to create a script just with a form that one could enter a name.

For example:

In a school every teacher is allowed to create a personal account.

Then that is encouraged to enter an account name by a form.

So, the new database name is that, the account's name.

The trouble however is: what's wrong with the above code?

Link to comment
Share on other sites

Hi, I am having a trouble about creating a database directly by form using a variable within CREATE DATABASE line:

 

the form:

 

<form action="create_database.php" method="post">

Type a name to your database: <br />

<input type="text" name="database_name" size="80"><br />

<input type="Submit" value="Create New Database">

 

the php script:

 

<?php

 

$database="$_POST['database_name'];

$con = mysql_connect('server', 'user', 'password');

if (!$con) {

   die('Connection to database failed: ' . mysql_error());

}

 

$sql = 'CREATE DATABASE'.($database); // I've tried all combination on this line and nothing works!

if (mysql_query($sql)) {

   echo "Database ".$database." created!\n";

} else {

   echo 'None database created: ' . mysql_error() . "\n";

}

?>

 

I would like some friend help me!

Link to comment
Share on other sites

I would like to thank you, artacus. In fact a concatenation space after DATABASE was required.

 

wrong:

$sql = 'CREATE DATABASE'.$database; // is equal to CREATE DATABASE$database - it can't work!

 

Correct:

 

$sql = 'CREATE DATABASE '.($database); // is equal to CREATE DATABASE $database - it can work!!

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.