MySQL throwing error on create table. No idea why - php

$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 ;

Related

CodeIgniter: usign db inside My_Lang

I found this useful Internationalization code:
http://pastebin.com/SyKmPYTX
everything works well except I am unable to connect to database.
what I need:
1) I need to check last segment
2) this is my db
DROP TABLE IF EXISTS `translate`;
DROP TABLE IF EXISTS `trans_data`;
DROP TABLE IF EXISTS `languages`;
/*Table structure for table `languages` */
CREATE TABLE `languages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`key` varchar(5) NOT NULL,
`default` tinyint(1) NOT NULL DEFAULT '0',
`display` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Data for the table `languages` */
insert into `languages`(`name`,`key`,`default`,`display`) values
('GEO','ka',1,1),
('ENG','en',0,1),
('RUS','ru',0,1);
/*Table structure for table `trans_data` */
CREATE TABLE `trans_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Table structure for table `translate` */
CREATE TABLE `translate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lang` int(11) NOT NULL,
`data` int(11) NOT NULL,
`trans` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `lang_2` (`lang`,`data`),
KEY `lang` (`lang`),
KEY `data` (`data`),
CONSTRAINT `translate_ibfk_1` FOREIGN KEY (`lang`) REFERENCES `languages` (`id`),
CONSTRAINT `translate_ibfk_2` FOREIGN KEY (`data`) REFERENCES `trans_data` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=340 DEFAULT CHARSET=utf8;
in the first I need to do something like this
$this->db->where('key', end($this->segments));
$query = $this->db->get('languages');
$lang = $query->row();
if(empty($lang)){
$this->db->where('default', 1);
$query2 = $this->db->get('languages');
$lang = $query2->row();
}
$lang_array = [
"lang_key" => $lang->key,
"lang_id" => $lang->id
];
$this->session->set_userdata($lang_array);
if(end($this->uri->segment_array()) == $lang->key){
unset($this->uri->segments[intval($this->uri->total_segments() - 1)]);
}
after this functionality
then I need to download translates from database and save it in object with this query
SELECT TD.`name` , T.`trans`
FROM `translate` AS T
INNER JOIN `languages` AS L ON (T.`lang` = L.`id`)
INNER JOIN `trans_data` AS TD ON (T.`data` = TD.`id`)
WHERE (L.`key` = :lang_key);
and when I call from view
echo $this->lang->line('trans_key');
I need to get the value
but I do not undestand how to edit this class to do this job...
can you help me and edit this class for me?
thank you for future...

Query for inserting last_insert_id() produce the same id value

Here's my info table:
CREATE TABLE `info` (
`id_info` int(10) NOT NULL auto_increment,
`judul_info` varchar(50) collate latin1_general_ci NOT NULL,
`konten` varchar(255) collate latin1_general_ci NOT NULL,
`diubah_oleh` varchar(20) collate latin1_general_ci NOT NULL,
`id_kategori` int(10) NOT NULL,
`tgl_buat` timestamp NOT NULL default '0000-00-00 00:00:00',
`tgl_ubah` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`dibuat_oleh` varchar(20) collate latin1_general_ci NOT NULL,
`id` int(10) NOT NULL,
PRIMARY KEY (`id_info`),
KEY `id_kategori` (`id_kategori`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=62 ;
Here's my upload table
CREATE TABLE `upload` (
`id` int(10) unsigned NOT NULL auto_increment,
`deskripsi` text,
`filetype` varchar(200) default NULL,
`filedata` longblob,
`filename` varchar(200) default NULL,
`filesize` bigint(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;
I'm using this query :
$sql1="INSERT INTO info VALUES('','$judul', '$konten','$diubah_oleh','$kategori',now(),'$tgl_ubah','$dibuat_oleh','')";
$sql2="insert into upload values ('','$keterangan','$tipe','$filedata','$nama_file',$ukuran)";
$sql3="UPDATE info SET id=last_insert_id()";
$result=mysql_query($sql1);
$result=mysql_query($sql2);
$result=mysql_query($sql3);
I want info.id has the same value as upload.id but with this query all of the value i get in info.id is the same as value i last inserted in upload.id.
CREATE TABLE `upload` (
`id` int(10) unsigned NOT NULL,
`deskripsi` text,
`filetype` varchar(200) default NULL,
`filedata` longblob,
`filename` varchar(200) default NULL,
`filesize` bigint(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;
$sql1="INSERT INTO info VALUES('','$judul', '$konten','$diubah_oleh','$kategori',now(),'$tgl_ubah','$dibuat_oleh','')";
$result=mysql_query($sql1);
$lastId = mysql_insert_id();
$sql2="insert into upload values ('$lastId','$keterangan','$tipe','$filedata','$nama_file',$ukuran)";
$result=mysql_query($sql2);
Your last update statement below is updating all the rows in your info tables with the same id because there is no where statement.
Since you need the upload table id information inside the info table.
Follow these steps:
Run the $sql2 first.
Then run the $sql1 inserting the last_insert_id() in info.id.
This way you don't need to use update statement as well.
You can do this by using mysql_insert_id(). This function returns the AUTO_INCREMENT ID generated from the previous INSERT operation. Your code should look like this
$sql1="INSERT INTO info VALUES('','$judul', '$konten','$diubah_oleh','$kategori',now(),'$tgl_ubah','$dibuat_oleh','')";
$result=mysql_query($sql1);
$lastinsertedid= mysql_insert_id();
$sql2="insert into upload values ('$lastinsertedid','$keterangan','$tipe','$filedata','$nama_file',$ukuran)";
$result=mysql_query($sql2);
Hope this helps you
There is an alternate php function to that mysql_insert_id() . You can use this to generate the ID inserted in the last executed query.
can you try to do this:
$sql1="INSERT INTO info VALUES('','$judul', '$konten','$diubah_oleh','$kategori',now(),'$tgl_ubah','$dibuat_oleh','')";
$sql2="insert into upload values ('','$keterangan','$tipe','$filedata','$nama_file',$ukuran)";
$last_id = last_insert_id();
$sql3="UPDATE info SET id=".$last_id;
$result=mysql_query($sql1);
$result=mysql_query($sql2);
$result=mysql_query($sql3);

PHP MySQL (PDO) CREATE TABLE does not create tables (and in fact, does nothing)

I have been struggling since this morning, trying to write an install.php file that will insert three empty tables (users, sessions, posts) into a database. The SQL code I'm using is valid when I inject it with PHPMyAdmin, but apparently the way I'm handling it in PHP is wrong, because when I run my install.php file my database remains empty. Here's my code:
<?php
try {
$con = new PDO('mysql:host=omitted;dbname=omitted','omitted','');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$q="CREATE TABLE posts (
title varchar(150) NOT NULL,
body text NOT NULL,
created varchar(100) NOT NULL,
user varchar(40) NOT NULL,
id int(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE sessions (
session_id varchar(40) NOT NULL,
data text NOT NULL,
last_activity int(11) NOT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(40) NOT NULL,
password varbinary(250) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(20) NOT NULL,
name varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY username (username),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ";
$stmt = $con->prepare($q);
$stmt->execute();
echo "success";
} catch (PDOException $e) {
$e->getMessage();
}
?>
I am assuming that it's some idiotic mistake on my part (since my last 3 headaches were also quite silly) but at this moment I really can't figure it out. Any ideas?
You can prepare only one statement per prepare call. To execute multiple statements you have to use PDO::exec method.
For example:
$con = new PDO('mysql:host=localhost;dbname=db','user','pass');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$q="CREATE TABLE posts (
title varchar(150) NOT NULL,
body text NOT NULL,
created varchar(100) NOT NULL,
user varchar(40) NOT NULL,
id int(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE sessions (
session_id varchar(40) NOT NULL,
data text NOT NULL,
last_activity int(11) NOT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(40) NOT NULL,
password varbinary(250) NOT NULL,
email varchar(40) NOT NULL,
salt varchar(20) NOT NULL,
name varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (id),
UNIQUE KEY email (email),
UNIQUE KEY username (username),
UNIQUE KEY id (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ";
try {
$con->exec($q) or die(print_r($db->errorInfo(), true));;
echo "Success";
} catch (PDOException $e) {
$e->getMessage();
}
This is not a query but rather set of queries.
Just run them one by one with separate calls.

How do i serlialize the product using php?

i am building a real estate application where in it will store the properties and search it. the property will have different categories like (residential, commercial, industrial or agricultural). based upon the category i want to serailize each and every property listing . for example the property with id 1 belongs to resedential will have the serial code rs_SOMERANDOMUNIQUENUMBER. and for commercial it can be cm_SOMERANDOMUNIQUENUMBER and so on. for this my database table looks like this.
CREATE TABLE IF NOT EXISTS `propSerials` (
`id` bigint(20) NOT NULL auto_increment,
`serial` varchar(50) NOT NULL,
`property_id` int(10) UNIQUE NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
what would be the best possible format to store the serial with the prefix according to category?
thank you
Why dont you add another column that holds category_id and in category table add column with prefixes for that category.
CREATE TABLE IF NOT EXISTS `propSerials` (
`id` bigint(20) NOT NULL auto_increment,
`serial` varchar(50) NOT NULL,
`property_id` int(10) UNIQUE NOT NULL,
`category_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `propCategories` (
`id` bigint(20) NOT NULL auto_increment,
`category` varchar(50) NOT NULL,
`property_prefix` char(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In query you can:
SELECT CONCAT('prefix_', 'serial');

Seems to be a syntax error in my MySQL

MySQL is putting off the following 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 `badips` ( `id` int(10) NOT NULL auto_increment, ' at line 2
When I run the following PHP:
if (file_exists("../login/includes/config.php")) {
$db_schema = array();
$db_schema[] = "DROP TABLE IF EXISTS `badips`;
CREATE TABLE IF NOT EXISTS `badips` (
`id` int(10) NOT NULL auto_increment,
`host` varchar(50) NOT NULL,
`ip` varchar(20) NOT NULL,
`enteredhost` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
require_once('../login/includes/config.php');
require_once('open-db.php');
echo "<h3>Creating tables...</h3>";
foreach($db_schema as $sql) {
mysql_query($sql) or die(mysql_error());
}
echo "<h3>Done!</h3>";
}
But when I run the same SQL from PHPMyAdmin, it works without any flaws. I can't figure out what the problem is. Anyone know?
mysql_query() does not support multiple queries. Quoting the PHP Manual:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
replace this:
$db_schema[] = "DROP TABLE IF EXISTS `badips`;
CREATE TABLE IF NOT EXISTS `badips` (
`id` int(10) NOT NULL auto_increment,
`host` varchar(50) NOT NULL,
`ip` varchar(20) NOT NULL,
`enteredhost` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
with
$query = "DROP TABLE IF EXISTS `badips`;
CREATE TABLE IF NOT EXISTS `badips` (
`id` int(10) NOT NULL auto_increment,
`host` varchar(50) NOT NULL,
`ip` varchar(20) NOT NULL,
`enteredhost` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26";
$db_schema = explode(";",$query);
mysql_query() does not support multiple queries., replace with
$sql="DROP TABLE IF EXISTS `badips`;";
mysql_query($sql) or die(mysql_error());
$sql="CREATE TABLE IF NOT EXISTS `badips` (`id` int(10) NOT NULL auto_increment, `host` varchar(50) NOT NULL, `ip` varchar(20) NOT NULL, `enteredhost` varchar(50) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
mysql_query($sql) or die(mysql_error());

Categories