0

I'm wanting to create an sql table, when a user registers at my site. This table will eventually be used to log messages. The code follows:

<?php
require('../connect/kidsblogsconnect.php');
$name=trim(mysql_real_escape_string($_POST['name']));


mysql_query("CREATE TABLE '$name'('sender' VARCHAR(30), 'message' VARCHAR(300), 'dateposted' DATE)") or DIE(mysql_error());


?>

When I run it, I get:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test'('sender' VARCHAR(30), 'message' VARCHAR(300), 'dateposted' DATE)' at line 1

I tried everything I know (which isn't much) but can't figure out why it isn't working. Any help is appreciated. If I need to post the whole code I can but thought it was overkill.

4
Contributors
10
Replies
40
Views
3 Years
Discussion Span
Last Post by david.roun.7
0

took them out and still get the error.

0
<?php
require('../connect/connect.php');

$name=trim(mysql_real_escape_string($_POST['name']));

require('../connect/kidsblogsconnect.php');


mysql_query("CREATE TABLE '$name'(sender VARCHAR(30), message VARCHAR(300), dateposted DATE)") or DIE(mysql_error());
echo 'Your account has been created and your picture uploaded';

?>

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test'(sender VARCHAR(30), message VARCHAR(300), dateposted DATE)' at line 1

Edited by david.roun.7: copied code again

0

No go. Same error message. I can't find a problem with it, or the code around it.

<?php
$name=trim(mysql_real_escape_string($_POST['name']));

require('../connect/kidsblogsconnect.php');
mysql_query("CREATE TABLE $name(sender VARCHAR(30), message VARCHAR(300), dateposted DATE)") or DIE(mysql_error());

?>
1

Hi Try something like this

$mysql_hostname="localhost";
$mysql_database="";
$mysql_user="";
$mysql_password="";

$con = mysqli_connect($mysql_hostname,$mysql_user, $mysql_password, $mysql_database);

$name=trim(mysql_real_escape_string($_POST['name']));

$sql=("CREATE TABLE IF NOT EXISTS `$name` ( `sender` varchar(30), `message` varchar(300), `dateposted` date )");

if (mysqli_query($con,$sql)) {
  echo "Table $name created successfully";
} else {
  echo "Error creating table: " . mysqli_error($con);
}

and refer to this link

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.