par
Fre3z69 » 29 oct. 2012, 14:17
Ayant pas eu le temps de m'occuper de cela plus tôt, je remonte ce message afin de demander de l'aide sur mon script.
Avant toute choses, voici mes tables
La table des sondages
CREATE TABLE IF NOT EXISTS `t_polls` (
`polls_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`topic_id` int(10) unsigned NOT NULL,
`users_id` int(10) unsigned NOT NULL,
`polls_title` varchar(255) NOT NULL,
PRIMARY KEY (`polls_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
La table des questions
CREATE TABLE IF NOT EXISTS `r_polls_isfrom_question` (
`question_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`polls_id` int(10) unsigned NOT NULL,
`question_content` varchar(250) NOT NULL,
`nbvote` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
Voici mon problème
Lors de l'inscription d'un sondage, je dois récupérer le dernièr
"polls_id" afin de l'insérer dans la table
"r_polls_isfrom_question"
Je ne sais pas comment m'y prendre, auriez vous une suggestion?
Voici ce que j'ai fait pour l'inscription d'un sondage:
$polls_title=$_POST['polls_title'];
$polls_question=$_POST['polls_question'];
$topic_id=$_POST['topic_id'];
$polls=$cnx->prepare('INSERT INTO t_polls
(polls_title, topic_id)
VALUES(:polls_title, :topic_id)');
$polls->bindValue(':polls_title', $polls_title, PDO::PARAM_STR);
$polls->bindValue(':topic_id', $topic_id, PDO::PARAM_INT);
$polls->execute();
$polls_question = $_POST['polls_question'];
$order=$cnx->prepare('SELECT
question_order
FROM r_polls_isfrom_question
ORDER BY question_id DESC
LIMIT 0 , 1');
$order->execute();
if ($order->rowCount()!=0){
$data=$order->fetch();{
$question_order = $data['question_order']+1;
}
}
else{
$question_order = 1;
}
print_r($polls_question);
foreach($polls_question as $content){
$polls=$cnx->prepare('INSERT INTO r_polls_isfrom_question
(question_id, polls_id, question_content)
VALUES(:polls_id, :question_order, :question_content)');
$polls->bindValue(':polls_id', $polls_id, PDO::PARAM_INT);
$polls->bindValue(':question_order', $question_order, PDO::PARAM_INT);
$polls->bindValue(':question_content', $content, PDO::PARAM_STR);
$polls->execute();
}
$polls->CloseCursor();
echo'<p><span class="reussi">Le sondage a bien été ajouté !</span></p>';
Ayant pas eu le temps de m'occuper de cela plus tôt, je remonte ce message afin de demander de l'aide sur mon script.
Avant toute choses, voici mes tables
La table des sondages
[sql]CREATE TABLE IF NOT EXISTS `t_polls` (
`polls_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`topic_id` int(10) unsigned NOT NULL,
`users_id` int(10) unsigned NOT NULL,
`polls_title` varchar(255) NOT NULL,
PRIMARY KEY (`polls_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;[/sql]
La table des questions
[sql]CREATE TABLE IF NOT EXISTS `r_polls_isfrom_question` (
`question_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`polls_id` int(10) unsigned NOT NULL,
`question_content` varchar(250) NOT NULL,
`nbvote` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;[/sql]
Voici mon problème
Lors de l'inscription d'un sondage, je dois récupérer le dernièr [b]"polls_id"[/b] afin de l'insérer dans la table [b]"r_polls_isfrom_question"[/b]
Je ne sais pas comment m'y prendre, auriez vous une suggestion?
Voici ce que j'ai fait pour l'inscription d'un sondage:
[php] $polls_title=$_POST['polls_title'];
$polls_question=$_POST['polls_question'];
$topic_id=$_POST['topic_id'];
$polls=$cnx->prepare('INSERT INTO t_polls
(polls_title, topic_id)
VALUES(:polls_title, :topic_id)');
$polls->bindValue(':polls_title', $polls_title, PDO::PARAM_STR);
$polls->bindValue(':topic_id', $topic_id, PDO::PARAM_INT);
$polls->execute();
$polls_question = $_POST['polls_question'];
$order=$cnx->prepare('SELECT
question_order
FROM r_polls_isfrom_question
ORDER BY question_id DESC
LIMIT 0 , 1');
$order->execute();
if ($order->rowCount()!=0){
$data=$order->fetch();{
$question_order = $data['question_order']+1;
}
}
else{
$question_order = 1;
}
print_r($polls_question);
foreach($polls_question as $content){
$polls=$cnx->prepare('INSERT INTO r_polls_isfrom_question
(question_id, polls_id, question_content)
VALUES(:polls_id, :question_order, :question_content)');
$polls->bindValue(':polls_id', $polls_id, PDO::PARAM_INT);
$polls->bindValue(':question_order', $question_order, PDO::PARAM_INT);
$polls->bindValue(':question_content', $content, PDO::PARAM_STR);
$polls->execute();
}
$polls->CloseCursor();
echo'<p><span class="reussi">Le sondage a bien été ajouté !</span></p>';[/php]