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());
Related
Have this SQL.
CREATE TABLE `accounts` (
`account_id` int(3) NOT NULL,
`username` varchar(36) NOT NULL,
`password` varchar(36) NOT NULL,
`email` varchar(60) NOT NULL,
`access_level` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `normal_profile` (
`normal_profileid` int(11) NOT NULL,
`first_name` varchar(64) NOT NULL,
`middle_name` varchar(64) NOT NULL,
`last_name` varchar(64) NOT NULL,
`age` int(2) NOT NULL,
`gender` varchar(6) NOT NULL,
`account_id` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `normal_profile`
ADD CONSTRAINT `normal_profile_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`account_id`);
phpmyadmin ui
The above is how i set constraints.
Whenever i insert something on to my accounts table, do i also have to query an insert into the normal_profile table? or can i automatically make it so that the DB itself will just add a row into normal_profile where account_id = accounts.account_id?
I have a vague memory of an on insert constraint or maybe im mistaken?
Adding Trigger UI phpmyadmin
try this syntax
delimiter #
CREATE TRIGGER after_accounts
AFTER insert ON accounts
FOR EACH ROW
BEGIN
-- your insert query
end#
delimiter ;
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);
Info table query:
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 ;
Upload table query :
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 want to insert the upload.id same as info.id when i post a new info. please help me.
here's my source code(sorry for the query i can't upload an image cause of my reputation still low):
<?php
include "config.php";
$id_info=$_POST['id_info'];
$judul=$_POST['judul_info'];
$konten=$_POST['konten'];
$tgl_ubah=$_POST['tgl_ubah'];
$diubah_oleh=$_POST['diubah_oleh'];
$id_kategori=$_POST['id_kategori'];
// update data in mysql database
$sql="UPDATE info SET judul_info='$judul', konten='$konten', diubah_oleh='$diubah_oleh', id_kategori='$id_kategori' WHERE id_info='$id_info'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Telah diupdate";
echo "<BR>";
echo "<a href='list_info.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
You need to use the last_insert_id() or mysql_insert_id() after your insert into the info table to get the new id inserted and then then use that in your next query while inserting record into upload table. Refer to links below on how to use them.
http://dev.mysql.com/doc/apis-php/en/apis-php-function.mysql-insert-id.html
http://php.net/manual/en/function.mysql-insert-id.php
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
I am trying to execute 3 queries to create tables within a database but it will not execute the query giving me a syntax error. Can anyone look at this code and tell me what I typed wrong to make this work? I have tried everything and I can not get the queries to execute!
$dh = mysqli_connect($_POST['hostname'], $_POST['username'], $_POST['password'], $_POST['database']);
if(! $dh )
{
die('Could not connect: ' . mysql_error());
}
echo "Connected successfully...<br /><br />";
$query = "CREATE TABLE IF NOT EXISTS `content` (
`content_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`section_id` INT(11) NOT NULL,
`header` VARCHAR(255) NOT NULL,
`sub_header` VARCHAR(255) NOT NULL,
`date_range` VARCHAR(64) NOT NULL,
`content_body` TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`section_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`position` INT(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`email` VARCHAR(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$retval = mysqli_multi_query($dh, $query);
You don't have semi colons between the queries and you are joining them all together and running at once.
If you are running multiple queries like that you need semicolons at the end of each query
$query = "CREATE TABLE IF NOT EXISTS `content` (
`content_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`section_id` INT(11) NOT NULL,
`header` VARCHAR(255) NOT NULL,
`sub_header` VARCHAR(255) NOT NULL,
`date_range` VARCHAR(64) NOT NULL,
`content_body` TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`section_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`position` INT(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$query .= "CREATE TABLE IF NOT EXISTS `sections` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(32) NOT NULL,
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`email` VARCHAR(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
You are missing semicolons between queries.
So you are trying to execute something like this: <...> ENGINE=InnoDB DEFAULT CHARSET=latin1CREATE TABLE <...>
$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 ;