I only want to create the table once if it doesn't exist. Will this code create a new table every time? If yes, how do I prevent it?
$queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS EmailList (
`ListName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`FirstName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`ID` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`IP` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`ConfirmedEmail` tinyint(1) DEFAULT NULL,
`Subscribed` tinyint(1) DEFAULT 0,
UNIQUE KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
if(!$conn->query($queryCreateUsersTable))
{
echo "Table creation failed: (" . $dbConnection->errno . ") " . $dbConnection->error;
}
CREATE TABLE IF NOT EXISTS means it will only create the table if it is not present.
Exactly as what CREATE TABLE IF NOT EXISTS means. Create a table if table does not exist.
Therefore, no it does not.
Related
I have the following table in mysql:
CREATE TABLE `kampbs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(20) COLLATE utf8_danish_ci DEFAULT NULL,
`k1` varchar(1) COLLATE utf8_danish_ci DEFAULT NULL,
`k1r` varchar(10) COLLATE utf8_danish_ci DEFAULT NULL,
`k2` varchar(1) COLLATE utf8_danish_ci DEFAULT NULL,
`k2r` varchar(10) COLLATE utf8_danish_ci DEFAULT NULL,
`k3` varchar(1) COLLATE utf8_danish_ci DEFAULT NULL,
`k3r` varchar(10) COLLATE utf8_danish_ci DEFAULT NULL,
`week` int(2) DEFAULT NULL,
`grp` varchar(11) COLLATE utf8_danish_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci
I have a form, that INSERT INTO the fields in the database, but it should only be possible to insert once a week pr user. So it should check if the column 'user' and 'week' are unique together. Make sense? :/
Let's say the user 'freak' submit in week 13, he shouldn't be able to submit once again in week 13.
I use the following $sql:
$sql = "INSERT INTO kampbs(user, k1, k1r, k2, k2r, k3, k3r, week, grp) VALUES('$username', '$k1', '$k1r', '$k2', '$k2r', '$k3', '$k3r', '$week', '$grp')";
You can add a constraint to your table, so that the user / week couple is forced to be unique
ALTER TABLE `kampbs` ADD UNIQUE `unique_index`(`user`, `week`);
i have my database sql query file and i wanna run it in php by the code below
$query = file_get_contents('./sqlquery.txt');
print $query;
$conn->query($query);
but it return this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `ads` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ur' at line 2
i copied the print output in to the phpmyadmin and everything work well, what's wrong here?
my sql query is the this
DROP TABLE IF EXISTS `ads`;
CREATE TABLE IF NOT EXISTS `ads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(300) NOT NULL,
`path` varchar(200) NOT NULL,
`width` int(11) NOT NULL,
`height` int(11) NOT NULL,
`priority` int(11) NOT NULL,
`adsalter` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`adstitle` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
DROP TABLE IF EXISTS `comment`;
CREATE TABLE IF NOT EXISTS `comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(120) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`comment` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`contentid` int(11) NOT NULL,
`parentid` int(11) NOT NULL DEFAULT '0',
`date` varchar(250) NOT NULL,
`haschild` int(1) NOT NULL DEFAULT '0',
`visible` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`size` int(14) NOT NULL,
`type` varchar(50) NOT NULL,
`newsid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
DROP TABLE IF EXISTS `frgpss`;
CREATE TABLE IF NOT EXISTS `frgpss` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`ip` varchar(31) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`token` varchar(27) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`date` int(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `news`;
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`titrimage` varchar(100) NOT NULL,
`titr` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`titralter` varchar(160) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`newsshurt` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`keywords` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`author` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`branch` int(11) NOT NULL,
`date` varchar(160) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`visible` int(1) NOT NULL DEFAULT '0',
`visited` int(11) NOT NULL DEFAULT '0',
`titrtitle` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
DROP TABLE IF EXISTS `signup`;
CREATE TABLE IF NOT EXISTS `signup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`family` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`gender` varchar(50) NOT NULL,
`username` varchar(80) NOT NULL,
`picture` varchar(80) NOT NULL,
`password` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL,
`lastname` varchar(60) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(40) NOT NULL,
`userregistereddate` varchar(60) NOT NULL,
`key` varchar(60) NOT NULL,
`type` varchar(20) NOT NULL,
`userphoto` varchar(50) NOT NULL,
`usergroup` varchar(300) NOT NULL,
`ipaddress` text NOT NULL,
`telnumber` varchar(14) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
you're executing multiple queries here, it won't work simply, I'm pretty sure that in query function($conn->query($query);) you're using mysql_query() or mysqli_query(), but you'll have to do is to use mysqli_multi_query() instead. Have a look here at docx
$con = mysqli_connect($host, $user, $pass, $db) OR die(mysqli_error($con));
$query = file_get_contents('./sqlquery.txt');
mysqli_multi_query($con, $query);
mysqli_close($con);
Hope this will help you
Note: Assuming that all queries are working fine while executing in PHPMyAdmin.
While inserting first time it gives following error: "Result consisted of more than one row". When i try to insert record second time it gives error with message duplicate entry.
SQL=INSERT INTO `master_user` (`name`,`user_name`,`email`,`password`,`system_name_of_friend`,`system_no_of_friend`,`registered_from_site`,`registered_on`,`is_existing_user`) VALUES ('FirstName LastName','username','demo#mail.com','8c71eede42e38709e9e836021b0b9b9b','','','site','','1')
any one help will be appropriated and following is the table structure which will be get help to tracking this issue very easily and get the solution for this.
CREATE TABLE IF NOT EXISTS `master_user` (
`master_user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`user_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`system_name_of_friend` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`system_no_of_friend` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`registered_from_ip` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
`registered_from_site` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
`registered_on` datetime DEFAULT NULL,
`is_existing_user` bit(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (`master_user_id`),
UNIQUE KEY `ukMasterUser_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1293 ;
I have changed the few words from column Which are conflicting and resolved.
I am using the redbeanPHP ORM and mysql. I have the following table:
CREATE TABLE `mast` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`geolocation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` ('app')
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I want to insert records into the 'mast' table providing they are unique with respect to both of the 2 fields listed above. In other words if either 'geolocation' or 'app' is a duplicate, I don't want to insert the associated record.
I am using following php code to create the 2 unique fields:
$mast= R::dispense('mast');
$mast->setMeta("buildcommand.unique" , array(array('geolocation')));
$mast ->import($resultsarray);
$mast->setMeta("buildcommand.unique" , array(array('app')));
$id = R::store($mast); // DUMMY BEAN
A unique index is only being created for the 'app' field . Is there a way to set them both as unique in redbean?
From the documentation it should work like the following:
$mast = R::dispense('mast');
$mast->setMeta("buildcommand.unique.0", array('geolocation', 'app'));
$mast->import($resultsarray);
$id = R::store($mast);
In your code you just overwrote the value for build command.unique.
Hello I have a problem with my php code.. there are 3 tables for a recruitment system.
CREATE TABLE IF NOT EXISTS `members` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`cpassword` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`email` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`role` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`uid`)'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;
The candidate table :
CREATE TABLE IF NOT EXISTS `candidate` (
`fullname` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`webpage` varchar(150) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`tel` int(35) NOT NULL,
`nationality` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`position` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`interviewed` varchar(30) NOT NULL DEFAULT 'No',
`rating` varchar(30) NOT NULL,
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
PRIMARY KEY (`c_id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=135 ;
The academic table :
CREATE TABLE IF NOT EXISTS `academic_candidate` (
`degree` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`exp_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment1` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`proposed_positions` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`research_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment2` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`department` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`a_id` int(25) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
PRIMARY KEY (`a_id`),
UNIQUE KEY `id` (`a_id`),
KEY `uid` (`uid`),
KEY `c_d` (`c_id`),
KEY `c_id` (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;`
-- Constraints for table academic_candidate
ALTER TABLEacademic_candidate
ADD CONSTRAINTacademic_candidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINTacademic_candidate_ibfk_2FOREIGN KEY (c_id) REFERENCEScandidate(c_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table candidate
ALTER TABLEcandidate
ADD CONSTRAINTcandidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE;
--
Now, I use this query in order to store the values in the table academic_candidate
session_start();
$query = "SELECT * FROM academic_candidate WHERE degree = '$degree'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0){
echo "You ALready complete the form </br>";
header("Location:../candidate/candidate_index.php");
}
else{
$degree =($_POST['degree']);
$exp_years = ($_POST['exp_years']);
$comment1 = ($_POST['comment1']);
$proposed_positions = ($_POST['proposed_positions']);
$research_years=($_POST['research_years']);
$comment2=($_POST['comment2']);
$department=($_POST['department']);
$uid=($_SESSION['uid']);
$query1 = "INSERT INTO academic_candidate
(degree,exp_years,comment1,proposed_positions,research_years,comment2,department,uid,c_id)
SELECT
'$degree','$exp_years','$comment1','$proposed_positions','$research_years','$comment2','$department','$uid','$c_id'
FROM candidate
WHERE uid='$uid' AND c_id='$c_id' ";
$result = mysql_query($query1);
if(!$result){
echo "Error";
die (mysql_error());
}
else{
header("Location:../candidate/view_application.php");
}
}
My problem is that stores all the values on the table academic_candidate table but the c_id is 0 . What can I do in order to take the candidate.c_id?
You're passing literal values to your INSERT INTO statement.
Consider the difference:
SELECT 'bar' FROM foo;
SELECT bar FROM foo;
Here's a demo on SQLFiddle.
Looking at your query:
SELECT '$degree'
Is not the same as:
SELECT degree
One of these is simply echoing whatever value you pass in, the other is actually selecting a column from the DB.