...

View Full Version : how to take the name from form?



muzammil
04-12-2011, 11:26 AM
Hi Awsome Coders. Please bear with me as i am newbie.
My question is can we name our database from taking the input from form.
For example


<html>
<head>
</head>
<body>
<form name="createDatabase" action="create.php" method="post">
Username:<input type="text" name="user" />
Password:<input type="password" name="password"/>
<input type="submit" name="submit" value="Register" />
</form>
</body>
</html>



<?php //create.php
$u=$_POST['user'];
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

mysql_close($con);
?>


All i want is name of the database should be the one that is enterd as 'username'. How can we achieve this?

bullant
04-12-2011, 11:45 AM
The value entered in "user" will be received by create.php in the superglobal $_POST['user']

After you have validated it, you can use that value wherever you need to in create.php

abduraooft
04-12-2011, 11:48 AM
How can we achieve this?
Before that, I'd seriously recommend you to learn about normalisation (http://en.wikipedia.org/wiki/Database_normalization)

muzammil
04-12-2011, 12:23 PM
Thanks bullant for bearing with me.
so the create command will look like this


if (mysql_query("CREATE DATABASE '$u'",$con))


is it?
@abduraooft Thanks I will read it. It seems like you read my mind from my post.
Thanks.

bullant
04-12-2011, 12:31 PM
so the create command will look like this


if (mysql_query("CREATE DATABASE '$u'",$con))
is it?


What happened when you ran it? Did it create the database for you?

muzammil
04-12-2011, 01:17 PM
I ran it and following error message came up.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''98745'' at line 1.

bullant
04-12-2011, 01:37 PM
Echo out the actual query you are running to create the database and compare it to the correct syntax. (http://www.w3schools.com/Sql/sql_create_db.asp)

The error in your code should then be clear.

withoutnick
04-12-2011, 02:41 PM
I think, you don't need using ' mark around the variable. Try it like that:

if (mysql_query("CREATE DATABASE $u",$con))



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum