I cant create table based on the php code after connect to database, i dont knw wht problem is going on

$sql = "CREATE TABLE ‘$subname’(

subid INT(20) AUTO_INCREMENT PRIMARY KEY,

subcode VARCHAR(255) DEFAULT '$subcode',

membertype VARCHAR(8),

username VARCHAR(255))";

The table name shouldn’t have quotes around it.

$sql = "CREATE TABLE $subname(
subid INT(20) AUTO_INCREMENT PRIMARY KEY,
subcode VARCHAR(255) DEFAULT '$subcode',
membertype VARCHAR(8),
username VARCHAR(255))";

Assuming $subname is a valid table name, the above should work.

ya, this works! thank you! Now i met another problem. In following code, I success to create database but insert table is not working, no idea what happened.

$conn = new mysqli($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS);
$sql = “CREATE DATABASE $subname”;
if($conn->query($sql)===TRUE){
$sql = “CREATE TABLE members(
membersid INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
subcode varchar(50) DEFAULT ‘$subcode’,
membertype VARCHAR(8),
username VARCHAR(255))”;
$conn->query("$sql");

$sql = "INSERT INTO members (membertype,username) VALUES (?,?)";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param('ss', $user['type'],$user['username']);
$stmt->execute();
}

echo '<script>
alert("Subject created successfully!")
window.location.href = "http://localhost/TBL/HomePageLec.php";
</script>';

}

i settled it, i have to select the database after create it in order to perform action.

mysqli_select_db($conn,$subname);

Good catch.

In future, you might find MySQLi’s error property useful for debugging:

echo $conn->error;

will display the last error encountered by your MySQL session.

alright, thanks a lot!

Sponsor our Newsletter | Privacy Policy | Terms of Service