base de donnée ne ce rempli pas corectement
Bonjour.
Je me trouve devant un souci d'insertion d'élément dans ma base de données Sql.
Le fichier create_datbase.php créer ma base "devoir" et mes deux tables "écoles et sports".
le fichier generator.php quant à lui doit renseigner ces deux bases.
ça ce passe nickel pour "écoles" mais pour sports rien ne semble s'enregistrer.
Y a-t-il quelqu'un qui sache me donner une solution ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| create_database.php
<?php
$servername = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'devoir';
$table1='ecoles';
$table2='sports';
// connexion à la base de donnée
$dbco = new PDO("mysql:host=$servername", $username, $password);
$dbco->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//si les tables ecoles et sports existe sortir si non creer
//creaton de la base devoir
try{
$sql = "CREATE DATABASE devoir";
$dbco->exec($sql);
//cree les tables et les colonnes
$dbco = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);//connecxion à la table devoir
$dbco->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql1="CREATE TABLE $table2 ( `id` INT(11) NOT NULL AUTO_INCREMENT , `ecole` VARCHAR(11) NULL , `boxe` INT(11) NULL , `football` INT(11) NULL , `judo` INT(11) NULL , `natation` INT(11) NULL , `cyclisme` INT(11) NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_general_ci";
$dbco->exec($sql1);
$sql2="CREATE TABLE $table1 ( `id` INT(11) NOT NULL AUTO_INCREMENT , `ecole` VARCHAR(11) NULL , `nb_eleves` INT(11) NULL , `nb_sport` INT(11) NULL , `licences` INT(11) NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_general_ci;";
$dbco->exec($sql2);
$sql3="INSERT INTO `ecoles`(`ecole`) VALUES ('ecole_A'),('ecole_B'),('ecole_C')";
$dbco->exec($sql3);
?> <button class="btn-Générer"onclick="window.location.href='../index.php'">lancer le programe</button> <?php
}
catch (PDOException $e){
?> <button class="btn-Générer"onclick="window.location.href='../index.php'">lancer le programe</button> <?php
}
?> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| generator.php
<?php
$result=1;
while ($result <=3) {
$nb_eleves= mt_rand(1, 500);
$nb_sportif= mt_rand(1, $nb_eleves);
$nb_sportifs=$nb_sportif;
$licences="0";
while ($nb_sportif >=0 ){
$nb_sport= mt_rand(1, 3);// choix du nombre de sport
$licences=($nb_sport + $licences);// on donne un nombre de licences par sportif
$nb_sportif --;
}
$dbh = new PDO ('mysql:host=localhost;dbname=devoir', 'root', '');
$dbh->query("UPDATE ecoles SET nb_eleves='$nb_eleves',nb_sport='$nb_sportifs',licences='$licences' WHERE id='$result'");
$result++;
}
//creation des 5 valeurs dans la base de données sport ( a partir de $distribution)
$id=1;
while ($id <=3) {
$dbh = new PDO ('mysql:host=localhost;dbname=devoir', 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare("SELECT licences FROM ecoles WHERE id=$id");
$sth->execute();
//tableau de tableau
$distrib = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($distrib as $delivery){
$distribution=$delivery['licences'];
$boxe=mt_rand(0, $distribution);
$cyclisme=mt_rand(0, ($distribution - $boxe));
$judo=mt_rand(0, ($distribution - ($cyclisme + $boxe)));
$football=mt_rand(0, ($distribution - ($cyclisme+$boxe+$judo)));
$natation=($distribution - ($cyclisme+$boxe+$judo+$football));
echo "<br>";
echo("boxe") . PHP_EOL; echo($boxe);
echo "<br>";
echo("cyclisme") . PHP_EOL; echo($cyclisme);
echo "<br>";
echo("judo") . PHP_EOL; echo($judo);
echo "<br>";
echo("football") . PHP_EOL; echo($football);
echo "<br>";
echo("natation") . PHP_EOL; echo($natation);
$dbh->query("UPDATE sports SET boxe='$boxe', football='$football', judo='$judo', natation='$natation', cyclisme='$cyclisme' WHERE id='$id'");
$id++;
}
}
?> |
Mille merci à celui qui voudra m'aider.