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!

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?

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!

To solve my own noob problems I usually echo the query on the web page before executing it.  Then I copyNpaste the query into PHPMYADMIN to see if it works :D

 

A great way to find those school boy errors as PHPMYADMIN will give you a clue as to whats wrong.

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!

Archived

This topic is now archived and is closed to further replies.

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