Bonjour ,
J'ai créé un forumaire php pour insérer des enregistrements.
Je souhaiterais vérifier les 2 champs prenom et message et afficher un echo si les champs sont vide.
Voici le code de ma page :
<?php require_once('Connections/deejayfloo.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue
"", $theNotDefinedValue "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$currentPage = $_SERVER["PHP_SELF"];
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO guestbook (prenom, message) VALUES (%s, %s)",
GetSQLValueString($_POST['prenom'], "text"),
GetSQLValueString($_POST['message'], "text"));
mysql_select_db($database_deejayfloo, $deejayfloo);
$Result1 = mysql_query($insertSQL, $deejayfloo) or die(mysql_error());
$insertGoTo = "guestbook.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
$maxRows_Recordset1 = 2;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
mysql_select_db($database_deejayfloo, $deejayfloo);
$query_Recordset1 = "SELECT * FROM guestbook";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $deejayfloo) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset1") == false &&
stristr($param, "totalRows_Recordset1") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<!-- saved from url=(0014)about:internet -->
<html xmlns="
http://www.w3.org/1999/xhtml ">
<head>
<title>DeeJay FloO | GuestBoOk</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
td img {display: block;}body {
background-color: #000000;
}
.Style1 {
color: #FFFFFF;
font-size: 12px;
}
</style>
<!--Fireworks CS3 Dreamweaver CS3 target. Created Mon Jul 06 12:03:15 GMT+0200 2009-->
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
<script language="JavaScript" type="text/JavaScript">
<!--
function addSmiley(codesmiley)
{
var msg = document.form1.message; // Remplace formulaire par le nom de ton formulaire
// et zonetext par le nom de la textbox ou textarrea
// Compatible IE
if(document.selection)
{
msg.focus();
sel = document.selection.createRange();
sel.text = codesmiley;
document.step2.focus();
}
else if(msg.selectionStart || msg.selectionStart == "0")
{
var startPos = msg.selectionStart;
var endPos = msg.selectionEnd;
var chaine = msg.value;
msg.value = chaine.substring(0, startPos) + codesmiley + chaine.substring(endPos, chaine.length);
msg.selectionStart = startPos + codesmiley.length;
mdg.selectionEnd = endPos + codesmiley.length;
msg.focus();
}
else
{
msg.value += codesmiley;
msg.focus();
}
}
//-->
</script>
</head>
<link rel="stylesheet" href="css.css" charset="ISO-8859-1" type="text/css"/>
<!-- fwtable fwsrc=\"Flo_V2.png\" fwpage=\"Page 1\" fwbase=\"index.jpg\" fwstyle=\"Dreamweaver\" fwdocid = \"118373649\" fwnested=\"0\" -->
----
,
,
,
,
,
,
----
,
,
,
,
,
----
,
,
,
----
,
,
----
,
,
----
,
,
----
<table width="100%" border="0">
----
, <table border=\"0\" align=\"left\">
----
<?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
">
<?php } // Show if not first page ?> ,
<?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
">
<?php } // Show if not first page ?> ,
<?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
">
<?php } // Show if not last page ?> ,
<?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
">
<?php } // Show if not last page ?> ,
</td>
<td>
GuestBook
</td>
</tr>
</table>
<hr />
<?php do { ?>
----
Ecrit par : <?php echo $row_Recordset1['prenom']; ?>,
----
<?php $mess = stripslashes($row_Recordset1['message']);
$mess = preg_replace('#:D#i','', $mess);
$mess = preg_replace('#:\)#','', $mess);
echo $mess;
?>
<hr color="#FF33CC"/>
,
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
----
Pseudo:,
<table width="100%" align="left">
----
,
</td>
<table width="200" align="right" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
----
, </td>
,
,
,
,
,
,
,
,
,
</td>
</tr>
</table></td>
<td valign="middle"> </td>
</tr>
<tr valign="top">
<td width="63" align="right" nowrap="nowrap" class="date_news">
Message:
</td>
<td width="344">
----
<textarea name="message" cols="45" rows="4"></textarea>,
</td>
<td width="129" valign="middle"></td>
</tr>
</table>
</form> </td>
<td></td>
</tr>
</table>
<script type="text/javascript">
<!--
swfobject.registerObject("FlashID");
//-->
</script>
</html>
<?php
mysql_free_result($Recordset1);
?>
J'essaye par moi même, voilà mon code mais ca ne fonctionne pas :
$msg_erreur = "Erreur. Les champs suivants doivent être obligatoirement remplis :
";
$msg_ok = "Votre demande a bien été prise en compte.";
$message = $msg_erreur;
// vérification des champs
if (empty($_POST['prenom']))
$message .= "Votre prenom
";
if (empty($_POST['message']))
$message .= "Votre message
";
// si un champ est vide, on affiche le message d'erreur
if (strlen($message) > strlen($msg_erreur)) {
echo $message;
// sinon c'est ok
} else { ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO guestbook (prenom, message) VALUES (%s, %s)",
GetSQLValueString($_POST['prenom'], "text"),
GetSQLValueString($_POST['message'], "text"));
mysql_select_db($database_deejayfloo, $deejayfloo);
$Result1 = mysql_query($insertSQL, $deejayfloo) or die(mysql_error());
$insertGoTo = "guestbook.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
if ($Result1) {
echo $msg_ok;
} else {
echo mysql_error();
}
Voyez vous l'erreur ?
Merci d'avance !
Afficher la suite