Forum Moderators: coopster

Message Too Old, No Replies

Problems creating MySql tables

         

jamesjohnson88

1:58 am on Jul 11, 2009 (gmt 0)

10+ Year Member



I am currently trying to write a simple piece of code that allows the user to create and name a table (The table format is pre-defined). I have tried with and without the naming code, i.e - adding a table name into the code itself, like when you would use a run-once script BUT that still doesn't work.

So, any suggestions on what is going wrong? Here is the code -

admin.php - Which SHOULD be fine.
---------
<html>
<title>
LeagueSQL - Admin
</title>
<body>
<form action="createtable.php" method="post">
League Name: <input type="text" name="leagueName" />
<input type="submit" />
<br />
<br />
SITE UNDER DEVELOPMENT!
</form>
</body>
</html>

createtable.php - Where I believe the problem lies.
------------
<?php
$con = mysql_connect("localhost","u08105199","edited");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// Create table
mysql_select_db("u08105199", $con);
echo "db selected & connected to $con";
$leagueName=$_POST['leagueName'];
$sql = "CREATE TABLE $leagueName
(
PlayerID int NOT NULL AUTO_INCREMENT,
PlayerName varchar(15),
GamesPlayed varchar(15),
Wins int,
Losses int,
Draws int,
Points int
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>
----------

Btw, when I do run the script, the message I get is this - "db selected & connected to Resource id #2".

mooger35

3:09 am on Jul 11, 2009 (gmt 0)

10+ Year Member



mysql_connect("localhost","u08105199","edited");

Are you sure that is right?

It should have this structure:
mysql_connect("[server]","[username]","[password]");

Currently as shown you have "u08105199" as your username and as the database you are selecting later on:
mysql_select_db("u08105199", $con);

That should have this structure:
mysql_select_db("[database]", $con);

The error you are getting leads me to believe you aren't connecting to the database.

jamesjohnson88

3:22 am on Jul 11, 2009 (gmt 0)

10+ Year Member



I'm using the university server. u08105199 is my username, the MySql access they give us limits us to creating tables, within an existing database, which they name the same as our student usernames. I imagine this would be quite confusing to the outside world.

mooger35

3:54 am on Jul 11, 2009 (gmt 0)

10+ Year Member



remove this line:
echo "db selected & connected to $con";

[edited by: mooger35 at 4:06 am (utc) on July 11, 2009]

mooger35

4:06 am on Jul 11, 2009 (gmt 0)

10+ Year Member



$con can't be echo'd like that. You either need to input your database directly into that line like this:

echo "db selected & connected to 'u08105199'";

or create a variable for it:

$db = 'u08105199';
echo "db selected & connected to $db'";

I would personally just take it out or remove the database name from that line.

jamesjohnson88

4:18 am on Jul 11, 2009 (gmt 0)

10+ Year Member



Yeah, I now have it all working.

I think it had something to do with me not assigning a primary key in the table specification.

@mooger - I've replaced that line now but thanks for the info anyway, as I now know what causes the wierd message it was giving me about connecting.

mooger35

4:24 am on Jul 11, 2009 (gmt 0)

10+ Year Member



A couple other things...

$leagueName=mysql_real_escape_string [php.net]($_POST['leagueName']);

$sql = "CREATE TABLE IF NOT EXISTS $leagueName etc...

Always escape your post data.