-1

I am executing two queries in one mysqli connection, the first one is working fine, but the second one is not.

Here's code:

<?php
//from server
$con = mysqli_connect("localhost","user","pass","db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


<?php

//mysqli prepare
$stmt = $con->prepare("INSERT INTO users_rooms (ip, rooms, description, date_time)
        VALUES (?,?,?, NOW())");
//bind params
$stmt->bind_param("sss", $ip, $rooms, $description);

    $trunc_text = substr($_POST['channel_description'], 0, 250);
    $rooms = preg_replace('/[^\w\d\s]+/', "", $_POST['channel_name']);
    $temp_rooms = str_replace(' ', '_', $rooms);
    $con->error;
    if(($trunc_text < 251) && isset($rooms)){

        //insert and execute msyqli
        $ip = $_SERVER['REMOTE_ADDR'];
        $rooms = mysqli_real_escape_string($con, htmlspecialchars(strip_tags($temp_rooms)));
        $description = mysqli_real_escape_string($con, htmlspecialchars(strip_tags($trunc_text)));
        $stmt->execute();
    }

//The above part of mysql is working fine, it's inserting the data as expected

//The below part of mysql is not working, it's not creating a table as $rooms

$rooms = mysqli_real_escape_string($con, htmlspecialchars(strip_tags($temp_rooms)));

$sql = "CREATE TABLE '$rooms' (
                        id BIGINT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                        ip VARCHAR(255) NOT NULL,
                        time VARCHAR(255) NOT NULL,
                        image_path VARCHAR(255),
                        smiley_path VARCHAR(255),
                        text_input VARCHAR(255),
                        current_date DATETIME
                        )";
$con->query($sql);
$con->error;
$stmt->close(); 
$con->close();
?>
Yogie
  • 986
  • 2
  • 12
  • 14
  • 2
    Does `$rooms` evaluate to something usable? Try it without the quote marks too. – ethrbunny Dec 23 '14 at 13:58
  • 1
    well, it must not mean it's not working, it's just that it does something differently than you expected. You need to check for the MYSQL error that it creates. Please see [Turning query errors to Exceptions in MySQLi](http://stackoverflow.com/q/14578243/367456) and [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) - for example the identifier quote character you use is wrong, double check with the documentation: https://dev.mysql.com/doc/refman/5.0/en/identifiers.html – hakre Dec 23 '14 at 13:59

1 Answers1

2

You have error in your sql query, try below code

$sql = "CREATE TABLE $rooms (
    `id` BIGINT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `ip` VARCHAR(255) NOT NULL,
    `time` VARCHAR(255) NOT NULL,
    `image_path` VARCHAR(255),
    `smiley_path` VARCHAR(255),
    `text_input` VARCHAR(255),
    `current_date` DATETIME
)";
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56