Source: lesson_13_a2_add_in_password_table.php | Résultat |
<?php require ("../mysql_connexion/mysql_connexion.php"); require ("../commons/functions.php"); //mysql_select_db($db) ; $tableName = "atelierphp_exe_pass_words" ; $param="lesson_13_a2_add_in_password_table.php"; if (!isset($_POST['user_name']) or !isset($_POST['password'])) { echo "<form action=../commons/show_exercises.php?exercise=$param method=post>"; ?> <table><tr><th colspan=2>Ajouter un utilisateur</th></tr> <td>Nom d'utilisateur</td> <td><input type="text" size="10" name="user_name"></td> </tr> <tr> <td>Mot de passe</td> <td><input type="PASS_WORD" size="10" name="password"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Valider"></td> </tr> </form> </table> </center> <?php } else { $user_name = $_POST['user_name'] ; $password = crypt($_POST['password'],$user_name) ; echo "Received user : ".$user_name."<br/> Received pass : ".$password."<br/>" ; $result = mysqli_query($mysqli,"INSERT INTO atelierphp_exe_pass_words (PASS_WORD_USER, PASS_WORD_PASS) VALUES ('$user_name','$password')") or mySQLdie(); if ($result == 1) { echo "values correctly inserted<br/>"; } else { echo "error inserting in the table the table<br/>"; }
//read the table echo "<table border=1><tr><th>User</th><th>Pass</th></tr>"; $result = mysqli_query($mysqli,"select * from atelierphp_exe_pass_words "); $fields = mysqli_num_fields($result); while ($row=mysqli_fetch_array($result)) { echo "<tr>"; for($i=0;$i<$fields;$i++) { echo "<td>$row[$i]</td>"; } echo "</tr>"; } echo "</table>"; echo "<p><a href=../commons/show_exercises.php?exercise=lesson_13_b_login_form.php> Saisie user/mot de passe pour entrer sur le site</a><br/>"; } ?>
Fichier : ../commons/connect_to_db.php
<?php @include ("../mysql_connexion/mysql_connexion.php"); @include ("../../mysql_connexion/mysql_connexion.php"); function mySQLdie($error = "") { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); global $strError,$strSQLQuery ; global $strMySQLSaid, $strBack, $sql_query;
echo "<b> Error : $strError </b>"; if(isset($sql_query) && !empty($sql_query)) { echo "$strSQLQuery: <pre>$sql_query</pre><p>"; } if(empty($error)) echo $strMySQLSaid.mysqli_error($mysqli); else echo $strMySQLSaid.$error; echo "<br/><a href=\"javascript:history.go(-1)\"> $strBack</a>"; exit; } ?>
Fichier : ../commons/functions.php
<?php // Pour savoir si une table existe function mySQLtable_exist($tableName) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $query = "SELECT COUNT(*) FROM $tableName"; $result = mysqli_query($mysqli,$query); $num_rows = @mysqli_num_rows($result); if($num_rows) { echo "<small>La table existait déjà <br/>"; return TRUE; } else { echo "<small>La table n'existait pas <br/>"; return FALSE; } } // Supprime une table avec message approprié //attention $tableName et non $table_name function drop_table($tableName) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); echo "Efface table ".$tableName."<br/>"; $result = mysqli_query($mysqli,"DROP TABLE $tableName") ; if ($result == 1) { echo "Table effacée <br/>"; } else { echo "Erreur en effaçant the table<br/>"; } } // Insère un enregistrement dans une table function insert_a_row_1 ($table_name, $field_name_1, $field_name_2, $field_1, $field_2) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $result = mysqli_query($mysqli,"INSERT INTO $table_name ($field_name_1,$field_name_2) VALUES ('$field_1','$field_2')"); if ($result == 1) { echo "valeur $field_2 insérée dans $field_name_2 <br/>"; } else { echo "erreur lors de l'insersion $field_2 dans la table $table_name <br/>"; } } function insert_a_row_2 ($table_name, $field_name_1, $field_name_2, $field_name_3, $field_1, $field_2, $field_3) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $result = mysqli_query($mysqli,"INSERT INTO $table_name ($field_name_1,$field_name_2,$field_name_3) VALUES ('$field_1','$field_2','$field_3')"); if ($result == 1) { echo "value $field_2 inserted in $field_name_2 <br/>"; } else { echo "error inserting $field_2 in the table $table_name <br/>"; } } //lecture d'une table function readTable ($tableName,$field_1,$field_2="",$field_3="", $field_4="",$field_5="") { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); echo "Lecture de la table $tableName :"; echo "<table border=1><tr><th>$field_1 </th><th>$field_2</th> <th>$field_3</th><th>$field_4</th> <th>$field_5</th></tr>"; $result=mysqli_query($mysqli,"select * from $tableName "); $fields = mysqli_num_fields($result); while ($row=mysqli_fetch_array($result)) { echo "<tr>"; for($i=0;$i<$fields;$i++) { echo "<td>$row[$i]</td>"; } echo "</tr>"; } echo "</table>"; } ?>
|
|