i want to create table with php variable+ name
like.
here is my code
if($conn->query($sql1)===TRUE){
$_SESSION['username'] = $x;
$sql = "CREATE TABLE $x
(
ID int NOT NULL AUTO_INCREMENT,
image blob,
date datetime,
status longtext,
PRIMARY KEY (ID)
)";
$sql2 = "CREATE TABLE ".$x."_frnd
(
ID int NOT NULL AUTO_INCREMENT,
myfrndname varchar(255),
myfrndusername varchar(255),
PRIMARY KEY (ID)
)";
$result= mysqli_query($conn,$sql,$sql2);
if ($conn->query($result) === TRUE) {
}
here ($x) is a variable and frnd is mannual name-
i want result table like this.
[ adminfrnd ].
please suggest me.
Try something like this:
CREATE TABLE IF NOT EXISTS `{$x}frnd` (
`id` mediumint(6) unsigned zerofill NOT NULL auto_increment,
`myfrndname` varchar(255) NOT NULL,
`myfrndlastname` varchar(255) NOT NULL,
`myfrndusername` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
by the way
Why do you want to make own table for each admin ?
What you have to do is to append text like this:
$sql = "CREATE TABLE " . $x . "frnd
( ID int NOT NULL AUTO_INCREMENT,
myfrndname varchar(255),
myfrndlastname varchar(255),
myfrndusername varchar(255),
PRIMARY KEY (ID) )";
In your example $x should be string.
Your code will look like:
$x="admin";
$sql = "CREATE TABLE ".$x."_frnd
( ID int NOT NULL AUTO_INCREMENT,
myfrndname varchar(255),
myfrndlastname varchar(255),
myfrndusername varchar(255),
PRIMARY KEY (ID) )";
$result= mysqli_query($conn,$sql);
if ($conn->query($result) === TRUE) {
}
Note: its never good habit to use .(dot) in table name use _(underscore) instead
Related
I am creating a MySQL database and this doesn't seem to work, i was able to create a review table but now i'm trying to drop that table and create a reviews table but it doesn't seem to work. Please can someone take a look at this and help me check to see what's wrong here?
$reviewsTable = "CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (website)
)";
$drop = "DROP TABLE review";
mysqli_query($connect,$drop);
mysqli_query($connect,$reviewsTable);
Just use if exists to drop the table if there is one then create your table.
Id has to be primary key because of the auto increment. all auto increments have to be primary key. You can index website though. but i set id as primary key below this should help.
$reviewsTable = "
DROP TABLE IF EXISTS review;
CREATE TABLE reviews (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
Website varchar(100) NOT NULL,
Review varchar(100) NOT NULL,
TimeOfYear varchar(50),
DayOfYear varchar(50),
PRIMARY KEY (ID)
)";
i'm very new to mysql and I am trying to create a database that can store users emails and passwords on one table and the values they input on another table, how do I join the tables to make sure that the inputted values are linked to the correct user. This is the code I've been using but it won't allow the value to be stored while the foreign key is run, but if I remove the foreign key I can store the value. Please help.
CREATE TABLE IF NOT EXISTS `data` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(51) NOT NULL,
`password` varchar(15) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `email_UNIQUE` (`email`)
)
CREATE TABLE IF NOT EXISTS `gluco` (
`G_id` int(11) NOT NULL AUTO_INCREMENT,
`bloods` decimal(4,2) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `data`(`use_id`),
UNIQUE KEY `G_id_UNIQUE` (`G_id`)
)
<?php
include('db.php');
if (!isset($_POST['reading'])) { //checking if user has entered this page directly
include('contactus.php');
} else {
if (isset($_POST['reading'])&&$_POST['reading']==""||!isset($_POST['reading'])) {
$error[] = "fill in your blood/glucose";
}
$reading = mysql_real_escape_string($_POST['reading']);
$sql = "SELECT * FROM gluco WHERE bloods = '$reading'";
if(isset($error)){
if(is_array($error)){
echo "<div class=\"error\"><span>please check the errors and refill the form<span><br/>";
foreach ($error as $ers) {
echo "<span>".$ers."</span><br/>";
}
echo "</div>";
include('contactus.php');
}
}
if(!isset($error)){
$sreading=mysql_real_escape_string($_POST['reading']);
$sip=mysql_real_escape_string($_SERVER['HTTP_HOST']);
$save = mysql_query("INSERT INTO `gluco` ( `bloods` )VALUES ('$sreading')");
if($save){
echo "<div class=\"success\"><span>Your reading has been successfully stored</span><br/></div>";
} else {
echo "<div class=\"warning\"><span>Some Error occured during processing your data</div>";
}
}
}
?>
your code is correct in its logic. But theres an error on the referenced column name:
CREATE TABLE IF NOT EXISTS `data` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(51) NOT NULL,
`password` varchar(15) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `email_UNIQUE` (`email`)
)
CREATE TABLE IF NOT EXISTS `gluco` (
`G_id` int(11) NOT NULL AUTO_INCREMENT,
`bloods` decimal(4,2) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `data`(`user_id`),
UNIQUE KEY `G_id_UNIQUE` (`G_id`)
)
and on this line:
$save = mysql_query("INSERT INTO `gluco` ( `bloods` )VALUES ('$sreading')");
you are not setting the user_id in your insert statement, so, the foreign key will not work and the insert will not be made. So, you'll need to have the user id stored in a variable (since i don't know the context and the scope in the code, i can't help you setting this variable). So, your code should be like that:
$save = mysql_query("INSERT INTO gluco (bloods, user_id)VALUES ('$sreading', $user_id)");
The exact error I keep seeing is:
Key column 'alarmID' doesn't exist in table
alarmID is my primary key field.
Here is the code I have:
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alarmID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
Note: I am coding in PHP.
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
alaramID
The primary key in your table is alaramID and note the error its alarmID.So correct the spelling in the query like this
$sql = "CREATE TABLE IF NOT EXISTS alarms (
alaramID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (alaramID),
Title CHAR(30),
Description TEXT,
DT DATETIME
)";
$invited = "CREATE TABLE invited (id NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), name VARCHAR(255), email VARCHAR(255), permissions VARCHAR(255))";
mysqli_query($dbc, $invited) or die ('Error creating invited');
What is wrong with this code? Keeps giving me "Error creating invited"
You forgot the ID data type
CREATE TABLE invited
(
id INT NOT NULL AUTO_INCREMENT, ...
^--------------------------------------here
Use this.
CREATE TABLE `invited` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
My server is on hostgator running on a linux centOS.
I'm simply trying to create a table within my database and I figured out how to get the table to get created. Although when I add the AUTO_INCREMENT setting the code doesn't execute and the table isn't created.
Why would this be and how can I correct it?
Here is my code:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
To use AUTO_INCREMENT you may have to assign the column as a primary key:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL,
PRIMARY KEY (id))";
Your query will give error
there can be only one auto column and it must be defines as a key, so add primary key to id field
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";