Jump to content


Photo

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


  • Please log in to reply
1 reply to this topic

#1 Street-Car

Street-Car
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 14 May 2006 - 08:07 AM

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:
<html>
<body><form action="create table.php" method="POST">
Table Name: <input type="text" name="ntable" />
<input type="submit" />
</form></body>
</html>



The PHP is this:
<?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();
}
?>


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.

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 14 May 2006 - 08:36 AM

you're missing a double quote on the end of your $sql
$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!";
}


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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users