Jump to content

Naming a mysql in php using a forms post value


phpserver

Recommended Posts

Hello,i have this html form.

<html>
<body>

<form action="create.php" method="post">
Class: <input type="text" name="class" />
Year: <input type="text" name="year" />
<input type="submit" />
</form>

</body>
</html> 

 

I want to create using the post values,possibly concatenate class.year and that will be the table name in mysql.

 

<?php
$con = mysql_connect("localhost","wikinger","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
//How do i replace Persons with '$_POST[class]'.'$_POST[year]'
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?> 

I want to use '$_POST[class]'.'$_POST[year]' as my table name.How do i do it in my case?.

 

Link to comment
Share on other sites

Why would you want to create multiple databases is the big question? Smells of poor design, anyway....

 

$class = mysql_real_escape_string($_POST['class']);
$year = mysql_real_escape_string($_POST['year']);
$sql = "CREATE TABLE {$class}{$year} (
  FirstName varchar(15),
  LastName varchar(15),
  Age int
)";

 

The user your php scripts connect with shouldn't even have permissions to create tables in my opinion, but on shared hosting and the like you r probably won't have much choice in the matter.

Link to comment
Share on other sites

First of all thank you very much for the help.I am making a software too run on the server2go,so its a software running on a cd.The client has a massive database that he needs to key in and so i thought it will be better to create a unique table for classes.I figured it could get fairly complicated since i am using extjs editable grid and i want a situation where the amount of joins are a minimum.

Link to comment
Share on other sites

Why would you want to create multiple databases is the big question? Smells of poor design, anyway....

 

$class = mysql_real_escape_string($_POST['class']);
$year = mysql_real_escape_string($_POST['year']);
$sql = "CREATE TABLE {$class}{$year} (
  FirstName varchar(15),
  LastName varchar(15),
  Age int
)";

 

The user your php scripts connect with shouldn't even have permissions to create tables in my opinion, but on shared hosting and the like you r probably won't have much choice in the matter.

 

Your code do not work!

Link to comment
Share on other sites

And actually as soon as you put any code into one of your files and attempt to execute it on your server, it becomes your code. You are responsible at that point for at least stating what symptom you see in front of you when you try it, because as has already been pointed out, telling someone that code does not work is useless information.

Link to comment
Share on other sites

$class = mysql_real_escape_string($_POST['class']);
$year = mysql_real_escape_string($_POST['year']);
$sql = "CREATE TABLE $class.$year (  FirstName varchar(15),  LastName varchar(15),  Age int)";

 

The code above worked,sorry if you took it the wrong way on the "Your code do not work",thingy,i tend to be straight,if you are thinking i was rude or trying to,maybe you are over analysing things.

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.