je débute en php par le biais de dreamweaver (je sais, c'est mal !) et je suis un tuto qui vise a repérer si une info existe dans une base et l'ajouter si elle n'y ait pas. J'ai refais la procedure plusieurs fois et ca ne marche pas.
C'est la variable résultat qui n'est pas récupérée a priori...Pourquoi ??
Voici mon code (si une bonne ame pouvait m'aider...):
<?php require_once('../../Connections/connexionScore.php'); ?>
<?php
/* fonction dream pour formater requete -------------------------------------------------------------*/
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;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
/*ajout resultat -------------------------------------------------------------------------------------- */
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formAJOUT")) {
$insertSQL = sprintf("INSERT INTO ventes (commerciauxID, mois, annee, resultat) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['commerciauxID'], "int"),
GetSQLValueString($_POST['mois'], "text"),
GetSQLValueString($_POST['annee'], "date"),
GetSQLValueString($_POST['resultat'], "double"));
mysql_select_db($database_connexionScore, $connexionScore);
$Result1 = mysql_query($insertSQL, $connexionScore) or die(mysql_error());
$insertGoTo = "resultatdumois.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
/* mise a jour résultat ------------------------------------------------------------------------------------- */
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "formMODIF")) {
$updateSQL = sprintf("UPDATE ventes SET commerciauxID=%s, mois=%s, annee=%s, resultat=%s WHERE ID=%s",
GetSQLValueString($_POST['commerciauxID'], "int"),
GetSQLValueString($_POST['mois'], "text"),
GetSQLValueString($_POST['annee'], "date"),
GetSQLValueString($_POST['resultat'], "double"),
GetSQLValueString($_POST['ID'], "int"));
mysql_select_db($database_connexionScore, $connexionScore);
$Result1 = mysql_query($updateSQL, $connexionScore) or die(mysql_error());
$updateGoTo = "resultatdumois.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
/* Variable de selection -------------------------------------------------------------------------------------*/
$colcommerciauxID_rsSelectResultat = "0";
if (isset($HTTP_GET_VARS['VARcommerciauxID'])) {
$colcommerciauxID_rsSelectResultat = $HTTP_GET_VARS['VARcommerciauxID'];
}
$colmois_rsSelectResultat = "0";
if (isset($HTTP_GET_VARS['VARmois'])) {
$colmois_rsSelectResultat = $HTTP_GET_VARS['VARmois'];
}
$colannee_rsSelectResultat = "0";
if (isset($HTTP_GET_VARS['VARannee'])) {
$colannee_rsSelectResultat = $HTTP_GET_VARS['VARannee'];
}
/*requete base ---------------------------------------------------------------------------------------------*/
mysql_select_db($database_connexionScore, $connexionScore);
$query_rsSelectResultat = sprintf("SELECT ventes.commerciauxID, ventes.mois, ventes.annee, ventes.resultat, ventes.ID FROM ventes WHERE ventes.commerciauxID=%s AND ventes.mois=%s AND ventes.annee=%s", GetSQLValueString($colcommerciauxID_rsSelectResultat, "int"),GetSQLValueString($colmois_rsSelectResultat, "int"),GetSQLValueString($colannee_rsSelectResultat, "int"));
$rsSelectResultat = mysql_query($query_rsSelectResultat, $connexionScore) or die(mysql_error());
$row_rsSelectResultat = mysql_fetch_assoc($rsSelectResultat);
$totalRows_rsSelectResultat = mysql_num_rows($rsSelectResultat);
/*test---------------------------------------------------------------------------------------*/
if(mysql_num_rows($rsSelectResultat) == 0)
{
// Y a aucun résultat
echo "no result";
}
else{
// Y a au minimum 1 résultat
echo "result ok";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>Document sans titre</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
<style type="text/css">
<!--
body,td,th {
color: #333333;
}
a:link {
color: #333333;
}
a:hover {
color: #666666;
}
-->
</style></head>
<body>
<table width="100%" border="1" cellpadding="4" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#CCCCCC">
<tr>
<td align="center" valign="middle" bgcolor="#999999"><a href="../index.php" target="_self"><strong>Accueil</strong></a></td>
<td align="center" valign="middle" bgcolor="#999999"><a href="moncompte.php" target="_self"><strong>Mon compte</strong></a></td>
<td align="center" valign="middle" bgcolor="#999999"><a href="resultatdumois.php" target="_self"><strong>Resultat du mois</strong></a></td>
<td align="center" valign="middle" bgcolor="#999999"><a href="mesresultats.php" target="_self"><strong>Mes résultats</strong></a></td>
<td align="center" valign="middle" bgcolor="#999999"><a href="statistiques.php" target="_self"><strong>Statistiques</strong></a></td>
</tr>
<tr>
<td colspan="5" align="center" valign="middle"><h1>* Espace commerciaux *</h1></td>
</tr>
</table>
<!-- TemplateBeginEditable name="EditRegion1" -->
<div align="center">
<h1>Page commerciaux</h1>
<!-- form selection -->
<form action="resultatdumois.php" method="get" name="formSELECT">
<table width="50%" border="0" cellpadding="4" cellspacing="0" bgcolor="#CCCCCC">
<tr>
<td colspan="2" align="center">Selection du mois et de l'année à modifer</td>
</tr>
<tr>
<td width="50%" align="center">mois:
<select name="VARmois" id="VARmois">
<option value="1">janvier</option>
<option value="2">février</option>
<option value="3">mars</option>
<option value="4">avril</option>
<option value="5">mai</option>
<option value="6">juin</option>
<option value="7">juillet</option>
<option value="8">aout</option>
<option value="9">septembre</option>
<option value="10">octobre</option>
<option value="11">novembre</option>
<option value="12">décembre</option>
</select></td>
<td width="50%" align="center"> année:
<select name="VARannee" id="VARannee">
<option>2000</option>
<option>2001</option>
<option>2002</option>
<option>2003</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
<option>2008</option>
</select></td>
</tr>
<tr>
<td colspan="2" align="center"><label>
<input type="submit" name="Selectionner" id="Selectionner" value="Selectionner" />
<input name="VARcommerciauxID" type="hidden" id="VARcommerciauxID" value="1" />
<input name="soumission" type="hidden" id="soumission" value="ok" />
</label></td>
</tr>
</table>
</form>
<!-- form ajout -->
<?php if ($totalRows_rsSelectResultat == 0) { // Show if recordset empty ?>
<?php if (@$soumission==1) {//si soumission formulaire ?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="formAJOUT">
<table width="50%" border="0" cellpadding="4" cellspacing="0" bgcolor="#FFCC99">
<tr>
<td colspan="2" align="center">Ajouter un nouveau résultat</td>
</tr>
<tr>
<td width="50%" align="center">mois: <?php echo @$_GET['VARmois']; ?></td>
<td width="50%" align="center">année: <?php echo @$_GET['VARannee']; ?></td>
</tr>
<tr>
<td colspan="2" align="center">résultat:
<label>
<input type="text" name="resultat" id="resultat" />
</label></td>
</tr>
<tr>
<td colspan="2" align="center"><label>
<input type="submit" name="button" id="button" value="Ajouter" />
<input name="mois" type="hidden" id="mois" value="<?php echo $_GET['VARmois']; ?>" />
<input name="annee" type="hidden" id="annee" value="<?php echo $_GET['VARannee']; ?>" />
<input name="commerciauxID" type="hidden" id="commerciauxID" value="1" />
</label></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="formAJOUT" />
</form>
<?php } // FIN si soumission formulaire ?>
<?php } // Show if recordset empty ?>
<!-- form modif -->
<?php if ($totalRows_rsSelectResultat > 0) { // Show if recordset not empty ?>
<form action="<?php echo $editFormAction; ?>" method="POST" name="formMODIF" id="formMODIF">
<table width="50%" border="0" cellpadding="4" cellspacing="0" bgcolor="#99CCCC">
<tr>
<td colspan="2" align="center">Modifier un résultat</td>
</tr>
<tr>
<td width="50%" align="center">mois: <?php echo @$_GET['VARmois']; ?></td>
<td width="50%" align="center">année: <?php echo @$_GET['VARannee']; ?></td>
</tr>
<tr>
<td colspan="2" align="center">résultat:
<label>
<input name="resultat" type="text" id="resultat" value="<?php echo $row_rsSelectResultat['resultat']; ?>" />
</label></td>
</tr>
<tr>
<td colspan="2" align="center"><label>
<input type="submit" name="button" id="button" value="Modifier" />
<input name="mois" type="hidden" id="mois" value="<?php echo $_GET['VARmois']; ?>" />
<input name="annee" type="hidden" id="annee" value="<?php echo $_GET['VARannee']; ?>" />
<input name="commerciauxID" type="hidden" id="commerciauxID" value="1" />
<input name="ID" type="hidden" id="ID" value="<?php echo $row_rsSelectResultat['ID']; ?>" />
</label></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="formMODIF" />
</form>
<?php } // Show if recordset not empty ?> </div>
<!-- TemplateEndEditable -->
</body>
</html>
<?php
mysql_free_result($rsSelectResultat);
?>
Merci !