Jump to content

Archived

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

Street-Car

Could somone tell me why this isnt working. (create table form for mysql)

Recommended Posts

Hi again,

okay i've been reading and playing some with trying to get a little form working that will create a table in mysql.

The HTML is this:
[code]
<html>
<body><form action="create table.php" method="POST">
Table Name: <input type="text" name="ntable" />
<input type="submit" />
</form></body>
</html>
[/code]



The PHP is this:
[code]
<?php

// Set Mysql Variables
$host = 'localhost';
$user = 'user';
$pass = 'pass';
$db = 'db';


if ($HTTP_POST_VARS['submit']) {

$con = mysql_connect("$host","$user","$pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$ntable=$HTTP_POST_VARS['ntable'];

$sql = "CREATE TABLE '$ntable'
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
model VARCHAR(30),
date VARCHAR(30),
fuel VARCHAR(60),
enginecap VARCHAR(30),
cylinders VARCHAR(30),
valves VARCHAR(30),
power VARCHAR(30),
torque VARCHAR(30),
drivenwheels VARCHAR(30),
stdtransmission VARCHAR(60),
optstdtransmission VARCHAR(60),
bodystyle VARCHAR(30),
seatingcap VARCHAR(30),
maxspeed VARCHAR(30),
0-60 VARCHAR(30),
mpg VARCHAR(30),
lenth VARCHAR(30),
width VARCHAR(30),
height VARCHAR(30),
wheelbase VARCHAR(30),
kerbweight VARCHAR(30),
co2 VARCHAR(30),
ukins VARCHAR(30),
ncp VARCHAR(30),
usncap VARCHAR(30)
PRIMARY KEY(id));

mysql_query($sql,$con);

echo "Table Created!";

or die(mysql_error());


mysql_close();
}
?>
[/code]


I keep getting loads of things like "Parse error: parse error, unexpected $ in /hm/dir/create table.php on line 57" and when i try and fix that its on another line :( Sorry im still new and cant put my finger on whats wrong.

Thanks in advance to all.

Share this post


Link to post
Share on other sites
you're missing a double quote on the end of your $sql
[code]
$sql = "CREATE TABLE '$ntable'
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
model VARCHAR(30),
date VARCHAR(30),
fuel VARCHAR(60),
enginecap VARCHAR(30),
cylinders VARCHAR(30),
valves VARCHAR(30),
power VARCHAR(30),
torque VARCHAR(30),
drivenwheels VARCHAR(30),
stdtransmission VARCHAR(60),
optstdtransmission VARCHAR(60),
bodystyle VARCHAR(30),
seatingcap VARCHAR(30),
maxspeed VARCHAR(30),
0-60 VARCHAR(30),
mpg VARCHAR(30),
lenth VARCHAR(30),
width VARCHAR(30),
height VARCHAR(30),
wheelbase VARCHAR(30),
kerbweight VARCHAR(30),
co2 VARCHAR(30),
ukins VARCHAR(30),
ncp VARCHAR(30),
usncap VARCHAR(30)
PRIMARY KEY(id))"; // <---- " was missing

//////////////////////////////////////////////////

$do = mysql_query($sql,$con) or die(mysql_error());
if($do)
{
echo "Table Created!";
}

[/code]

You should also use $_POST instead of $HTTP_POST_VARS if you are running php 4.1.0 or higher

Share this post


Link to post
Share on other sites

×

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.