Forum Moderators: coopster

Message Too Old, No Replies

problem creating a database with php

problem creating a database with php

         

sunnydayzrback

2:47 pm on May 24, 2007 (gmt 0)

10+ Year Member



Hi,
I wrote this code to allow the user to submit a name and then create a database with that name , but there seems to be some problem . After submitting the data , the database is not being created . what is the problem?
Thanks in advance.

<?php
$conn = @mysql_connect("localhost","","") or die("Couldnot connect");
$rs1=mysql_query("CREATE DATABASE". $_POST["db"]."");
$rs2= mysql_list_dbs($conn);
for($row=0; $row<mysql_num_rows($rs2); $row++)
{
$list .= mysql_tablename($rs2, $row). " ¦ "; }?>
<html><head><title> Create a Database </title> </head> <body>
<form action="<?php echo($PHP_SELF);?>" method="post">
Current Databases: <?php echo($list);?> <hr/>
Name:<input type="text" name="db" >
<input type="submit" value="Create Database">
</form></body></html>

[edited by: eelixduppy at 5:46 pm (utc) on May 24, 2007]
[edit reason] removed specifics [/edit]

jatar_k

3:28 pm on May 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you shoudl remove @ symbols when testing as they suppress errors

you should add an or die here

$rs1=mysql_query("CREATE DATABASE". $_POST["db"]."") or die('could not create db: ' . mysql_error());

you should never, ever, use user submitted data directly in a query

$rs1=mysql_query("CREATE DATABASE". $_POST["db"]."");

you have to test that data first, at very least for accepted chars for db names

sunnydayzrback

3:43 pm on May 24, 2007 (gmt 0)

10+ Year Member



Thanks very much. I forgot a space there as u pointed out.