le 09/10/2007 à 15:10
LA GLOBULE
Oui ne serait ce que pour éviter les erreurs SQL : si tu insères le moindre texte avec des quotes, tu vas te manger des mysql_error().
<?php
if (isset($_GET['variable'])) $variable = htmlentities($_GET['variable'], ENT_QUOTES); else $variable = '';
if (isset($_POST['variable'])) $variable = htmlentities($_POST['variable'], ENT_QUOTES); else $variable = '';
?>
<?php
if (isset($_POST['titre']) && isset($_POST['news'])) {
if ($db = @mysqli_connect('localhost', 'jackbocar', 'xxxxxxx')) {
require_once 'stripFormSlashes.inc.php';
mysqli_select_db($db, 'xxxxxxxx');
mysqli_query($db, sprintf( 'INSERT INTO my_news (titre, news) VALUES (\'%s\', \'%s\')',
mysqli_real_escape_string($db, $_POST['titre']),
mysqli_real_escape_string($db, $_POST['news'])));
echo 'Entrée sauvée.';
mysqli_close($db);
} else {
echo 'Pas de Connexion.';
}
}
?>
<?php
function stripFormSlashes($arr) {
if (!is_array($arr)) {
return stripslashes($arr);
} else {
return array_map('stripFormSlashes', $arr);
}
}
if (get_magic_quotes_gpc()) {
$_GET = stripFormSlashes($_GET);
$_POST = stripFormSlashes($_POST);
}
?>
<?php
if ($db = @mysqli_connect('localhost', 'jackbocar', 'xxxxxxx')) {
mysqli_select_db($db, 'xxxxxxx');
$result = mysqli_query($db, 'SELECT * FROM my_news');
while ($row = mysqli_fetch_object($result)) {
printf('<table border="0" cellpadding="0" cellspacing="0" width="800"><tr valign="top"><td width="100">%s</td><td width="600">%s</td></tr></table><table border="0" cellpadding="0" cellspacing="0" width="800"><tr valign="top"><td>%s</td></tr></table><br />',
htmlspecialchars($row->id),
htmlspecialchars($row->titre),
htmlspecialchars(nl2br($row->news))
);
}
mysqli_close($db);
} else {
echo '<b>Connection failed.</b>';
}
?>