Jump to content

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


Street-Car

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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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