PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Insert categories with new row
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Insert categories with new row

Réponse
 
LinkBack Outils de la discussion
Vieux 01/10/2007, 21h48   #1
frizzle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Insert categories with new row

Hi there,

I have a system to create news-items, which can belong to certain
categories.
The categories are linked by id with an extra "item_id-category_id"-
table. (InnoDB + FK's)

I want to be able to create a new News Item, together with it's
categories.
My current code is below. It works, but if the categories fail, the
News item is still inserted.
If something fails, nothing should happen...
Best of all, would be to have everything in 1 query if possible. (IMO)

The categories are inserted via an <select name="categories[]"
id="categories[]" multiple="multiple">-field which is dynamically
populated.

Thanks in advance!


$qryInsertItem =
'INSERT INTO `news_items` ( `open` , `date` , `url` , `title` ,
`body` )
VALUES ( \''.mysql_real_escape_string($_POST['open']).'\',
\''.mysql_real_escape_string($date).'\', \''.
mysql_real_escape_string(MkUrlSafe($_POST['title'])).'\',
\''.mysql_real_escape_string($_POST['title']).'\',
\''.mysql_real_escape_string($_POST['body']).'\');';

mysql_query($qryInsertItem, $rConn);

if(is_array( $_POST['categories'] )){

foreach( $_POST['categories'] as $key =>$val){
$cats .= '('.mysql_insert_id().', '.$val.'),';

};

$cats=trim($cats, ',');

}else{

$cats=NULL;

};

$qryInsertCats = 'INSERT INTO `news_cat_item` ( `item_id` , `cat_id` )
VALUES '.$cats.';';


mysql_query($qryInsertCats , $rConn) )

  Réponse avec citation
Vieux 02/10/2007, 13h28   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Insert categories with new row

On 1 Oct, 21:48, frizzle <phpfriz...@gmail.com> wrote:

> "I want to be able to create a new News Item, together with it's categories."


Possesive its has no apostrophe! This should say "I want to be able to
create a new News Item, together with its categories."

Now, can you post the table schemas?
And explain the circumstances under which something fails?
And state whether you are using transactions?

  Réponse avec citation
Vieux 02/10/2007, 19h49   #3
frizzle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Insert categories with new row

On Oct 2, 2:28 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 1 Oct, 21:48, frizzle <phpfriz...@gmail.com> wrote:
>
> > "I want to be able to create a new News Item, together with it's categories."

>
> Possesive its has no apostrophe! This should say "I want to be able to
> create a new News Item, together with its categories."
>
> Now, can you post the table schemas?
> And explain the circumstances under which something fails?
> And state whether you are using transactions?


Sorry about the "it's". English isn't my native tongue, but i do want
spell correctly of course, so thanks for pointing it out to me.

About my system:

> Now, can you post the table schemas?

They're below my reply.

> And explain the circumstances under which something fails?

If the parent's category id appears not to exist, it refuses (as it
should) to create orphans in the intermediate table (news_cat_item).
It should also discard creating the first item as well then. So I
suppose it should all be done in 1 query, maybe with some COMMIT
commands? But then i wouldn't know how to get the last_insert_id();.

> And state whether you are using transactions?

Sorry, I don't understand this question/remark.

My table structures are below...

Thanks, frizzle.


-- Table structure for table `news_items`
--

CREATE TABLE `news_items` (
`id` int(10) unsigned NOT NULL auto_increment,
`open` enum('y','n') NOT NULL default 'n',
`date` tinyint(9) unsigned default '0',
`url` varchar(185) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`body` mediumtext NOT NULL,
`views` int(5) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


-- Table structure for table `news_cats`
--

CREATE TABLE `news_cats` (
`id` int(10) unsigned NOT NULL auto_increment,
`order` int(10) unsigned NOT NULL default '0',
`url` varchar(185) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`descr` text NOT NULL,
PRIMARY KEY (`id`),
KEY `cat_url` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


-- Table structure for table `news_cat_item`
--

CREATE TABLE `news_cat_item` (
`id` int(10) unsigned NOT NULL auto_increment,
`item_id` int(10) unsigned NOT NULL default '0',
`cat_id` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `item_id` (`item_id`,`cat_id`),
KEY `cat_id` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


--
-- Constraints for table `news_cat_item`
--
ALTER TABLE `news_cat_item`
ADD CONSTRAINT `news_cat_item_ibfk_1` FOREIGN KEY (`item_id`)
REFERENCES `news_items` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `news_cat_item_ibfk_2` FOREIGN KEY (`cat_id`)
REFERENCES `news_cats` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12035 seconds with 11 queries