This query is for an IPN script integrated with PayPal. Everything works in the script except the insertion of data into the table. Here's the code to insert data, all of the objects have their appropriate value.
if ($item == "RuneCoins") {
mail("admin#sallesy.com", "Item purchased!", "$acc has just purchased $item for $paid.");
mysql_query("INSERT INTO `purchases` (`id`, `email`, `price`, `product`, `fname`, `lname`, `time`, `transaction_id`, `acc`) VALUES (NULL, '$email', '$paid', '$item', '$fname', '$lname', CURRENT_TIMESTAMP, '$trans', '$acc');");
} else {
mail("admin#sallesy.com", "Invalid item!", "$acc purchased $item, and it was invalid.");
}
I am receiving the email that the user has purchased the item, but the data is not inserted into the table. Why is this happening?
Here is the SQL structure for the table
CREATE TABLE IF NOT EXISTS `purchases` (
`id` int(11) NOT NULL,
`email` longtext COLLATE utf8_unicode_ci NOT NULL,
`price` int(11) NOT NULL,
`product` longtext COLLATE utf8_unicode_ci NOT NULL,
`fname` longtext COLLATE utf8_unicode_ci NOT NULL,
`lname` longtext COLLATE utf8_unicode_ci NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`transaction_id` bigint(11) NOT NULL,
`acc` longtext COLLATE utf8_unicode_ci NOT NULL,
`delivered` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
What am I doing wrong?
I see the problem. You cannot use NULL as the value for id, because it is specifically prohibited in your create table statement. What you need to do is two fold.
First, change your insert to this:
mysql_query("INSERT INTO `purchases` (`id`, `email`, `price`, `product`, `fname`, `lname`, `time`, `transaction_id`, `acc`) VALUES (0, '$email', '$paid', '$item', '$fname', '$lname', CURRENT_TIMESTAMP, '$trans', '$acc');");
The only change I made there is changing NULL to 0, since 0 would be valid, and works nicely with the next change. Change two, modify your create table:
CREATE TABLE IF NOT EXISTS `purchases` (
`id` int(11) NOT NULL auto_increment,
`email` longtext COLLATE utf8_unicode_ci NOT NULL,
`price` int(11) NOT NULL,
`product` longtext COLLATE utf8_unicode_ci NOT NULL,
`fname` longtext COLLATE utf8_unicode_ci NOT NULL,
`lname` longtext COLLATE utf8_unicode_ci NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`transaction_id` bigint(11) NOT NULL,
`acc` longtext COLLATE utf8_unicode_ci NOT NULL,
`delivered` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You need to make sure your id field is auto-incremented. If it is not auto-incremented, then you have to maintain a counter in your code, which is annoying. Changing it to auto-increment will allow you to put a 0 as the id, which will be interpreted as <the next available id>.
Hope this helps.
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`);
Its my first time using php and mysql together. Here's my database's script:
CREATE SCHEMA IF NOT EXISTS `Alinfo_Express` ;
USE `Alinfo_Express` ;
CREATE TABLE IF NOT EXISTS `Alinfo_Express`.`Categorias` (
`idCategoria` INT(11) NOT NULL,
`titulo` VARCHAR(128) NOT NULL,
`descricao` TEXT NOT NULL,
PRIMARY KEY (`idCategoria`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Alinfo_Express`.`Produtos` (
`idProduto` INT(11) NOT NULL,
`nome` VARCHAR(256) NOT NULL,
`descricao` TEXT NOT NULL,
`precoUnidade` FLOAT NOT NULL,
`qtdEstocada` INT(11) NOT NULL,
`emDestaque` BIT(1) NOT NULL,
`idCategoria` INT(11) NOT NULL,
PRIMARY KEY (`idProduto`),
INDEX `fk_Produtos_Categorias1_idx` (`idCategoria` ASC),
CONSTRAINT `fk_Produtos_Categorias1`
FOREIGN KEY (`idCategoria`)
REFERENCES `Alinfo_Express`.`Categorias` (`idCategoria`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Alinfo_Express`.`Usuarios` (
`idUsuario` INT(11) NOT NULL,
`nome` VARCHAR(256) NOT NULL,
`sexo` CHAR(1) NOT NULL,
`cpf` VARCHAR(256) NOT NULL,
`rg` VARCHAR(256) NOT NULL,
`email` VARCHAR(256) NOT NULL,
`endereco` VARCHAR(256) NOT NULL,
`CEP` VARCHAR(256) NOT NULL,
`bairro` VARCHAR(256) NOT NULL,
`cidade` VARCHAR(256) NOT NULL,
`estado` VARCHAR(256) NOT NULL,
`senha` VARCHAR(256) NOT NULL,
`tipo` CHAR(1) NOT NULL,
PRIMARY KEY (`idUsuario`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Alinfo_Express`.`Compra` (
`idCompra` INT NOT NULL,
`valor` DECIMAL NOT NULL,
`tipoPagamento` VARCHAR(45) NOT NULL,
`dataCompra` DATETIME NOT NULL,
`idUsuario` INT(11) NOT NULL,
`idProduto` INT(11) NOT NULL,
PRIMARY KEY (`idCompra`),
INDEX `fk_Compra_Usuarios_idx` (`idUsuario` ASC),
INDEX `fk_Compra_Produtos1_idx` (`idProduto` ASC),
CONSTRAINT `fk_Compra_Usuarios`
FOREIGN KEY (`idUsuario`)
REFERENCES `Alinfo_Express`.`Usuarios` (`idUsuario`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Compra_Produtos1`
FOREIGN KEY (`idProduto`)
REFERENCES `Alinfo_Express`.`Produtos` (`idProduto`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Here's my code:
$MySQLi->query("INSERT INTO Produtos (idProduto, nome, descricao, precoUnidade,qtdEstocada, emDestaque, idCategoria) VALUES ($cod, '$nome', '$desc', $preco,$qtd, $dest, '$url', $cat);");
I don't know why it isn't working out. There's a long time I don't work with databases, that maybe be my problem.
You have couple of mistakes in your query. Try to execute in phpmyadmin. Try this one
$MySQLi->query("INSERT INTO Produtos (idProduto, nome, descricao, precoUnidade,qtdEstocada, emDestaque, idCategoria) VALUES ($cod, '$nome', '$desc', $preco, $qtd, $dest, '$url', $cat)");
It should work. In phpmyadmin, it will tell you error. Share it with us.
Here you can try with this i hope it will work
$MySQLi->query("INSERT INTO Produtos (idProduto, nome, descricao, precoUnidade,qtdEstocada, emDestaque, idCategoria) VALUES ('".$cod."', '".$nome."', '".$desc.'", '".$preco."','".$qtd."', '".$dest."', '".$url."', '".$cat."')");
Insert code:
INSERT INTO CalendarAppointment (`codiCategory`, `codiPersona`, `nomePersona`, `comment`, `day`, `description`, `from`, `shortDescription`, `to`, `modificatoDa`, `dataModifica`, `idModifica`) VALUES ('oUQz/ejwAfoAdAw7Jm+W1cFKeNH8N26ZBBcvhwwXPAA=', 'oUQz/ejwAfoAdAw7Jm+W1cFKeNH8N26ZBBcvhwwXPAA=', 'HuWZQyWSAnY9ReZ0q0+ZrBVRhReagvUG9Th1q3TtLPw=', 'q46vm3AsgJsEO6NOJ3r5xKhLMvvy+Sy05E76krKhiNo=', '2014-10-29', 'q46vm3AsgJsEO6NOJ3r5xKhLMvvy+Sy05E76krKhiNo=', '2014/10/13 09:00', 'q46vm3AsgJsEO6NOJ3r5xKhLMvvy+Sy05E76krKhiNo=', '2014/10/13 09:30', '2014-10-29 11:11:34', '2014-10-29 11:11:34', '1')
Show create table CalendarAppointment:
| CalendarAppointment | CREATE TABLE `CalendarAppointment` (
`idAppointment` int(10) NOT NULL AUTO_INCREMENT,
`codiCategory` blob,
`codiPersona` blob,
`nomePersona` blob,
`comment` blob,
`day` datetime NOT NULL,
`description` blob,
`from` datetime NOT NULL,
`shortDescription` blob,
`to` datetime NOT NULL,
`modificatoDa` varchar(80) NOT NULL DEFAULT '',
`dataModifica` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`idModifica` int(11) NOT NULL DEFAULT '0',
UNIQUE KEY `idAppointment` (`idAppointment`)
) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=utf8
What is wrong?
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);
This is my table:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL default '',
`pass` varchar(32) NOT NULL default '',
`lang` varchar(2) default NULL,
`locale` varchar(2) default NULL,
`pic` varchar(255) default NULL,
`sex` char(1) default NULL,
`birthday` date default NULL,
`mail` varchar(64) default NULL,
`created` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `mail` (`mail`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
And this is my query:
$query = "INSERT IGNORE INTO `users` (`name`, `mail`, `birthday`, `lang`, `locale`, `sex`, `pic`) VALUES ('".$name."', '".$email."', '".date_format($birthdaynew, 'Y-m-d H:i:s')."', '".substr($locale, 0, 2)."', '".substr($locale, -2, 2)."', '".$sex."', 'pic/".$uid.".jpg')";
$rows = mysql_query($query) or die("Failed: " . mysql_error());
$_SESSION['id'] = mysql_insert_id(); // I have tryed also mysql_insert_id($db_con) where $db_con is the link to db.
$_SESSION['name'] = $name;
$_SESSION['name'] contains correctly the name but $_SESSION['id'] contains 0.
Why ?
I'm going crazy!
Is there a particular reason why you are using INSERT IGNORE?
If you use INSERT IGNORE, then the row won't actually get inserted if there is a duplicate key (PRIMARY or UNIQUE), or inserting a NULL into a column with a NOT NULL constraint.
Referring to the pass column, as you have not defined anything to insert into it, and it has NOT NULL constraint.
EDIT:
Referring also to the mail column, as you have a UNIQUE constraint on it.