Member Avatar for david.roun.7_1

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.

Do not use single quotes around column names. Use nothing or backticks instead.

Member Avatar for david.roun.7_1

took them out and still get the error.

Show the new create query and error message.

<?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

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());

?>

Same thing. I'm totally at a loss.

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

Perfect. Thank you so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.