-1

I have a link which will use to create sql table in database . I want to use date + string as table name . When I am just using string that is creating table in my database . But when I am using variable as table name with date function , its not working and not creating any table .

<?php require("connection.php");
$ins = new Db();
$ins->dbconnect();
$tablename = date('d-m-y')."customername";
$table= "CREATE TABLE  $tablename(id int) ";
<a href="<?php $ins->dbconnection->query($table)?>">Create table</a>

When I am just using string in $tablename and its create table . But when using date() function in $tablename , its not creating table anymore .

KAMRUL HASAN
  • 45
  • 1
  • 7
  • So what's the actual runtime value of the table name you're trying to create? And, more importantly, *why*? Why on Earth do you think you need this? Also, what do you expect that `href` value to be and why? It sounds like you're making several simultaneous mistakes based on what is essentially the wrong design in the first place. Can you elaborate on the overall goal here? – David Apr 01 '19 at 18:22
  • If you have a hyphen in your table name (which the format `d-m-y` would), you'd need to use a quoted identifier in all queries:``"CREATE TABLE `$tablename`(id int) "``. Other than that, it's difficult to answer this question, as you don't include the error message. – Joshua Dwire Apr 01 '19 at 18:26
  • I have used quotation as you said but its not working . Its not throwing any error but not creating any new table too . $tablename = date('d_m_y')."customername"; $table= "CREATE TABLE '$tablename'(id int) "; Create table – KAMRUL HASAN Apr 01 '19 at 18:42
  • I want to use the link to create database table which name would be current_date+a string . so when clicking the link , It should create unique table . I am learner and I asked to do that for my assignment – KAMRUL HASAN Apr 01 '19 at 18:46
  • 1
    @KAMRULHASAN: The table isn't going to be created when clicking the link. That link isn't going to have anything useful in it. The SQL query will execute when the page loads. You'd do well to start learning about server-side code vs. client-side code: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming It's also worth noting that, aside from being an academic assignment you've been given, this is *almost never* something you'd actually want to do in real code. Users shouldn't be creating new tables. – David Apr 01 '19 at 18:49
  • its creating table when I am not using date functions . Can you please tell me how can I create table name with current date from php file ? Thanks – KAMRUL HASAN Apr 01 '19 at 18:53
  • @KAMRULHASAN: Remove the hyphens from the date format, or enclose the whole table name in backticks. You'll also want to look into how to get error messages from whatever data access driver you're using, so you can get meaningful error messages when something fails. – David Apr 01 '19 at 18:57

2 Answers2

0

You're creating a empty table which will error out, if you want a table without columns you can do it like this:

CREATE TABLE 01_01_01(t INT);
ALTER TABLE 01_01_01
DROP COLUMN t;

Notice you have to use underscores instead of hyphens!

Lulceltech
  • 1,662
  • 11
  • 22
0

I have done it as follows :

public function table($tname){
    return $this->dbconnection->query("CREATE TABLE $tname(id int)");
}
$n = $validation['name'];
$m = str_replace(" ","",$n);
$m=date("d_m_Y_g_i_s_A_").$m;
$tname = "$m";
$t = $ins->table($tname);

I have paste those code in submit button and its finely creating new table with date + string .

KAMRUL HASAN
  • 45
  • 1
  • 7