0

I'm trying to create a new table each day (each time my php script runs). I want to dynamically name these tables to help keep them organized. Here's what I've attempted so far with no luck:

$tableName = "CE" . date("Y.m.d");

// how do i use $tableName? to do this below dynamically?
$sql = "CREATE TABLE '$tableName'( 
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
SKU VARCHAR(30) NOT NULL,
Title VARCHAR(125) NOT NULL,
Price FLOAT(8),
AZPrice FLOAT(8)
)";

$table = mysqli_query($connection, $sql);

//Same thing here below, insert into $tableName (dynamic name each day for new table)
$query = "INSERT INTO '$tableName'(Date,SKU,Title,Price,AZPrice) VALUES (CURDATE(), ?, ?, ?, ?)";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "ssdd", $cleanedSku, $titlePlain, $priceFloat, $amazonPrice);
mysqli_stmt_execute($stmt);

How do I accomplish this? Thank you

ERRORS:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\demo\mysql\table.php on line 524

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\demo\mysql\table.php on line 525

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\demo\mysql\table.php on line 524

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\demo\mysql\table.php on line 525

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\demo\mysql\table.php on line 524

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, bool given in C:\xampp\htdocs\demo\mysql\table.php on line 525
Tman
  • 389
  • 1
  • 14
  • Where is your error checking? This question is incomplete. You can avoid calling `CURDATE()` in your prepared statement if you set the default date when declaring the table. – mickmackusa Feb 06 '20 at 22:39
  • https://prnt.sc/qyobed Here are the errors, is this what you mean? – Tman Feb 06 '20 at 22:44
  • The statement works with CURDATE(), I think it's complaining about the $query line, specifically the `'$tablename'(Date,SKU,Title,Price,AZPrice)` – Tman Feb 06 '20 at 22:45
  • 1. Never post your textual question details as screenshot 2. Question details go in the body of your question, not as comments. 3. You are not checking the CREATE TABLE error. All those `bool` errors are subsequent errors because of the failed CREATE. 4. The duplicate page used to close your question is not a punishment -- it is what you need to read to understand and fix your issue. – mickmackusa Feb 06 '20 at 22:46
  • 1
    Yeah I got it I used apostrophes instead of backticks.. – Tman Feb 06 '20 at 22:53

0 Answers0