Query for inserting last_insert_id() produce the same id value - php

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

Related

database phpmyadmin on insert trigger

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 ;

NULL entry at id field in Phpmyadmin

I exported the sql from one phpmyadmin db and imported same at another phpmyadmin db.
The create table statement is as follows
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) DEFAULT NULL,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The insert sql file also I have exported it as follows
INSERT INTO `h_stats` (`Id`, `Category`, `Success`, `Failure`, `ThCount`, `OCount`, `HDate`, `Count`) VALUES
(13, 'Hits', 31303, 8828, 8893, 30372, '2015-04-07', 40151),
Note :
When I try to insert new rows at the place of id NULL value is coming.
Can anyone suggest me what to change in create table statement.
You need to define the id as primary key and auto-increment .
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So when you add null for id, the auto-increment will come in to play and set it the next value in insert.
Note that this will enforce you to have unique value for id so while doing insert leave id into the insert statement. Something as
insert into h_stats (`Category`,`Success` ...) values ( ....);

Unable to enter in the 'commenter' field of my database after executing my sql query, all the other fields are set except the said field

I have a database having this schema
CREATE TABLE IF NOT EXISTS `comment`
(
`comment_id` int(11) NOT NULL auto_increment,
`commenter` varchar(100) NOT NULL,
`comment` varchar(255) NOT NULL,
`time` varchar(100) NOT NULL,
`date` varchar(100) NOT NULL,
PRIMARY KEY (`comment_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;
and i have a following code snippet in PHP
$comment=$_POST['comment'];
$dbNmee = $_SESSION['user'];
$time=date("h:i:sA",strtotime("+0 minute"));
$date=date("d-m-y",strtotime("+0 minute"));
$sql="INSERT INTO comment (commenter , comment , time , date) VALUES ('$dbNmee','$comment','$time','$date')";
but am unable to enter in the commenter field of my database.

How to insert upload table primary key(ID) to info table (ID) at the same time?

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

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