Si valeur existe (2)

Bonjour à tous !

Pouvez-vous m'aider à corriger ce bout de code svp ?

<?php
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS); mysql_select_db(MYSQL_BASE,$db);
$req = mysql_query("SELECT COUNT(IPs) AS dejaexistant FROM tb_jeux_pakinou WHERE IPs = '".$_SERVER['REMOTE_ADDR']."'");

$res = mysql_fetch_array($req);

if($res['dejaexistant'] == 0) {echo "<script>location.href = 'index.php';</script>";}
if($res['dejaexistant'] != 0) {

$NumId = $_GET["NumId"];
if(isset($_POST['Vote'])) $Vote = mysql_real_escape_string($_POST["Vote"]); else $Vote ="";
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Erreur de connexion '.mysql_error());
mysql_select_db(MYSQL_BASE,$db) or die('Erreur de selection '.mysql_error());
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "UPDATE tb_jeux_pakinou SET IPs=CONCAT(IPs, ', ".$ip."'), Vote = Vote + 1 WHERE NumId='".$NumId."'";
mysql_query($sql) or die('Erreur SQL !'.$sql."<br />".mysql_error());
echo "<script>location.href = 'index.php';</script>";

} ?>

Merci d'avance

9 réponses

  1. Bonjour,

    Peux-tu nous fournir plus de détails : qu'elle erreur as-tu? A Qu'elle ligne, etc...
    1. Dans ce cas, plutôt que :
      echo "<script>location.href = 'index.php';</script>";

      utilise
      header("location:index.php");


      Ensuite, pourquoi tu ouvre deux fois ta connexion SQL (une au début et de ton script et une dans ton
      if($res['dejaexistant'] != 0)

      La deuxième connexion est inutile, et vue que je suis de bon humeur aujourd'hui, voilà une idée de code que tu pourrais utiliser.

      <?php
      $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
      mysql_select_db(MYSQL_BASE,$db);
      
      //LIKE %....% te permet de rechercher une sous-chaine dans une chaine de caractère, peut importe sa position
      $req = mysql_query("SELECT COUNT(IPs) AS dejaexistant FROM tb_jeux_pakinou WHERE IPs LIKE %".$_SERVER['REMOTE_ADDR']."%;"); 
      $res = mysql_fetch_array($req);
      if($res['dejaexistant'] == 0) {
      	header("location:index.php");
      } else {
      	$NumId = mysql_real_escape_string($_GET["NumId"]);
      	
      	if(isset($_POST['Vote'])) $Vote = mysql_real_escape_string($_POST["Vote"]);
      	else $Vote ="";
      	
      	$ip = $_SERVER['REMOTE_ADDR'];
      	
      	$sql = "UPDATE tb_jeux_pakinou SET IPs=CONCAT(IPs, ', ".$ip."'), Vote = Vote + 1 WHERE NumId='".$NumId."' LIMI 1;";
      	mysql_query($sql) or die('Erreur SQL !'.$sql."<br />".mysql_error());
      	
      	header("location:index.php");
      }
      ?>


      Attention, je n'ai pas testé le code, il se peut que tu es à l'adapté en fonction de tes besoins.
      1. Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /homez.724/xyz/www/jeux/pakinou2014/vote.php on line 60

        Warning: Cannot modify header information - headers already sent by (output started at /homez.724/xyz/www/inclusion/header.php:15) in /homez.724/xyz/www/jeux/pakinou2014/vote.php on line 62
        1. Rajoute
           or die(mysql_error());

          à la fin de ta ligne 6, et vois l'erreur.
          1. Voilà ce qu'il me donne :s
            Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /homez.724/xyz/www/jeux/pakinou2014/vote.php on line 80
            You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%00.00.00.01%' at line 1

            $res = mysql_fetch_array($req) or die(mysql_error());
            1. Corrige le WHERE avec ce code.
              LIKE '%".$_SERVER['REMOTE_ADDR']."%'
              1. Merci joker, il me renvois encore une erreur sur la limite :
                Erreur SQL !UPDATE tb_jeux_pakinou SET IPs=CONCAT(IPs, ', 00.00.00.01'), Vote = Vote + 1 WHERE NumId='6' LIMI 1;
                You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMI 1' at line 1
                1. J'ai un peux fouillé, mais je me retrouve avec toujours un ajout dans ma table même si l'IP existe :
                  <?php
                  $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Erreur de connexion '.mysql_error());
                  mysql_select_db(MYSQL_BASE,$db) or die('Erreur de selection '.mysql_error());
                  $req = mysql_query("SELECT COUNT(IPs) AS ExisteDeja FROM tb_jeux_pakinou WHERE IPs LIKE '%".$_SERVER['REMOTE_ADDR']."%' ;"); 
                  $res = mysql_fetch_array($req) or die(mysql_error());
                  if($res['ExisteDeja'] == 0) {
                  echo "Existant";}
                  
                  else {$NumId = $_GET["NumId"];
                  if(isset($_POST['Vote'])) $Vote = mysql_real_escape_string($_POST["Vote"]); else $Vote ="";
                  $ip = $_SERVER['REMOTE_ADDR'];
                  $sql = "UPDATE tb_jeux_pakinou SET IPs=CONCAT(IPs, ', ".$ip."'), Vote = Vote + 1 WHERE NumId='".$NumId."' LIMIT 1";
                  mysql_query($sql) or die('Erreur SQL !'.$sql."<br />".mysql_error());
                  echo "Validé";}
                  
                  ?>


                  Il saute la condition du LIKE je penses :s