Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 09/01/2012, 16h47   #1
Membre confirmé
 
Inscription : octobre 2006
Messages : 340
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 340
Points : 221
Points : 221
Envoyer un message via MSN à ideal23
Par défaut update sous condition

bonjour
je suis entrain de créer mon espace administration. je veux pouvoir modifier une table avec un champ photo.
tables articles:

voici le code pour la modification:
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
61
62
63
64
65
<?php require_once('../Connections/dream.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;
}
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE articles SET titre=%s, `description`=%s, prix=%s, photo=%s WHERE reference=%s",
                       GetSQLValueString($_POST['titre'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['prix'], "double"),
                       GetSQLValueString($_POST['photo'], "text"),
                       GetSQLValueString($_POST['ID'], "int"));
 
  mysql_select_db($database_dream, $dream);
  $Result1 = mysql_query($updateSQL, $dream) or die(mysql_error());
 
  $updateGoTo = "valider.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
 
$colname_Rsmodifclient = "-1";
if (isset($_GET['reference'])) {
  $colname_Rsmodifclient = $_GET['reference'];
}
mysql_select_db($database_dream, $dream);
$query_Rsmodifclient = sprintf("SELECT * FROM articles WHERE reference = %s", GetSQLValueString($colname_Rsmodifclient, "int"));
$Rsmodifclient = mysql_query($query_Rsmodifclient, $dream) or die(mysql_error());
$row_Rsmodifclient = mysql_fetch_assoc($Rsmodifclient);
$totalRows_Rsmodifclient = mysql_num_rows($Rsmodifclient);
?>
Lorsque je modifie un champ autre que celui de la photo, le résultat est bon , mais en même temps, la donnée du champ photo est supprimée. Je suis obligé de faire un 2ème passage pour remettre la photo.
j'ai pensé à faire une vérification sur le champ photo. S'il est vide, on ne le change pas, sinon on le change.
nouveau code:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php require_once('../Connections/dream.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;
}
}
 
$colname_Rsmodifclient = "-1";
if (isset($_GET['reference'])) {
  $colname_Rsmodifclient = $_GET['reference'];
}
mysql_select_db($database_dream, $dream);
$query_Rsmodifclient = sprintf("SELECT * FROM articles WHERE reference = %s", GetSQLValueString($colname_Rsmodifclient, "int"));
$Rsmodifclient = mysql_query($query_Rsmodifclient, $dream) or die(mysql_error());
$row_Rsmodifclient = mysql_fetch_assoc($Rsmodifclient);
$totalRows_Rsmodifclient = mysql_num_rows($Rsmodifclient);
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
 
  if (empty($_POST['photo'])) {
    $updateSQL = sprintf("UPDATE articles SET titre=%s, `description`=%s, prix=%s WHERE reference=%s",
    GetSQLValueString($_POST['titre'], "text"),
    GetSQLValueString($_POST['description'], "text"),
    GetSQLValueString($_POST['prix'], "double"),  
    GetSQLValueString($_POST['ID'], "int"));
    }
    else
    {	
    $updateSQL = sprintf("UPDATE articles SET titre=%s, description`=%s, prix=%s, photo=%s WHERE reference=%s",
    GetSQLValueString($_POST['titre'], "text"),
    GetSQLValueString($_POST['description'], "text"),
    GetSQLValueString($_POST['prix'], "double"),
    GetSQLValueString($_POST['photo'], "text"), 
    GetSQLValueString($_POST['ID'], "int"));
    } 	
 
  mysql_select_db($database_dream, $dream);
  $Result1 = mysql_query($updateSQL, $dream) or die(mysql_error());
 
  $updateGoTo = "valider.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
?>
Maintenant effectivement je peux modifier les autres champs sans que le champ photo soit supprimé. Mais je ne peux plus modifier le champ photo lui-même.
Je ne comprends pas ce résultat.avez-vous une idée et laquelle? Merci
ideal23 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/01/2012, 06h56   #2
Expert Confirmé
 
Avatar de RunCodePhp
 
Inscription : janvier 2010
Messages : 2 728
Détails du profil
Informations personnelles :
Localisation : Réunion

Informations forums :
Inscription : janvier 2010
Messages : 2 728
Points : 3 295
Points : 3 295
Salut

Essai de voir dans quelle condition tu entres lorsque tu remarques ce problème, comme par exemple rajouter un simple echo.

Code :
1
2
3
4
5
6
if (empty($_POST['photo'])) {
   echo 'Condition empty()<br />';
}
else {
    echo 'Condition Pas empty()<br />';
}
__________________
Win XP | WampServer 2.2d | Apache 2.2.21 | Php 5.3.10 | MySQL 5.5.20
Si debugger, c'est supprimer des bugs, alors programmer ne peut être que les ajouter [Edsger Dijkstra]
RunCodePhp est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/01/2012, 16h44   #3
Membre confirmé
 
Inscription : octobre 2006
Messages : 340
Détails du profil
Informations forums :
Inscription : octobre 2006
Messages : 340
Points : 221
Points : 221
Envoyer un message via MSN à ideal23
Merci de ta réponse, à quel endroit, je mets ce code ?
ideal23 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/01/2012, 05h57   #4
Expert Confirmé
 
Avatar de RunCodePhp
 
Inscription : janvier 2010
Messages : 2 728
Détails du profil
Informations personnelles :
Localisation : Réunion

Informations forums :
Inscription : janvier 2010
Messages : 2 728
Points : 3 295
Points : 3 295
En faite, je n'avais pas fais attention que la 2ème partie de code serait une suggestion, et non ton code actuel.
Ce que je te suggérait était de les rajouter dans cette 2ème partie.

Donc ton code actuel est celui de la 1ère partie.
Pour mieux comprendre ce qu'il se passe, il faudrait afficher le résultat de chaine de requête de même ce que contient $_POST.
Faire comme ceci par exemple :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
$updateSQL = sprintf("UPDATE articles SET titre=%s, `description`=%s, prix=%s, photo=%s WHERE reference=%s",
GetSQLValueString($_POST['titre'], "text"),
GetSQLValueString($_POST['description'], "text"),
GetSQLValueString($_POST['prix'], "double"),
GetSQLValueString($_POST['photo'], "text"),
GetSQLValueString($_POST['ID'], "int"));
//
echo '<pre>';
print_r($_POST);
echo '</pre><br />';
echo $updateSQL;
exit(); // Point d'arrêt
Post le résultat affiché sur le forum.
(Ne pas oublier de mettre un point d’arrêt car plus loin il y a une redirection)
__________________
Win XP | WampServer 2.2d | Apache 2.2.21 | Php 5.3.10 | MySQL 5.5.20
Si debugger, c'est supprimer des bugs, alors programmer ne peut être que les ajouter [Edsger Dijkstra]
RunCodePhp est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 09h16.


 
 
 
 
Partenaires

Hébergement Web