SQL expects parameters

SQLUser - 12 févr. 2015 à 02:07
Polack77 Messages postés 1098 Date d'inscription mercredi 22 mars 2006 Statut Membre Dernière intervention 22 octobre 2019 - 12 févr. 2015 à 14:33
Bonjour,

J'ai ce code :


<?php

// Server Information
$cnx = mysqli_connect("localhost", "root", "", "toolsdb");

// Get Tools Informations
// Tool ID
$ToolID = $_POST["ToolID"] ;

// Tool Type
$ToolType = $_POST["ToolType"] ;

// Turning Direction
$TurningDirection = $_POST["TurningDirection"] ;

// Tool Diameter
$ToolDiameter = $_POST["ToolDiameter"] ;

// Tool Stuff
$ToolStuff = $_POST["ToolStuff"] ;

// Till ID
$TillID = $_POST["TillID"] ;

// Add Tool Informations in DataBase
$sql = "INSERT INTO ToolsDB (ToolID, ToolType, TurningDirection, ToolDiameter, ToolStuff, TillID)
VALUES ( '$ToolID', '$ToolType', '$TurningDirection', '$ToolDiameter', '$ToolStuff', '$TillID') " ;

// SQL Execution
$requete = mysql_query($sql, $cnx) or die( mysql_error() ) ;

// SQL Execution Informations
if($requete)
{
echo("L'Outil à été ajouté à la base de données.") ;
}
else
{
echo("Echec de la connection à la base de données.") ;
}
?>


Mais impossible de valider le formulaire...
J'obtiens l'eeruer

Warning: mysql_query() expects parameter 2

Quelqu'un aurait une idée

1 réponse

Polack77 Messages postés 1098 Date d'inscription mercredi 22 mars 2006 Statut Membre Dernière intervention 22 octobre 2019 1
12 févr. 2015 à 14:33
Je ne suis pas expert en PHP.
Sauf erreur cette ligne :
 $sql = "INSERT  INTO ToolsDB (ToolID, ToolType, TurningDirection, ToolDiameter, ToolStuff, TillID)
            VALUES ( '$ToolID', '$ToolType', '$TurningDirection', '$ToolDiameter', '$ToolStuff', '$TillID') " ;

Vas insérer la valeurs des variables ($ToolID par exemple) en place des références dans ta chaine.
Je vois pas bien comment le lien est fait par contre, perso j'aurais plutôt vu un truc du genre de :
 $sql = "INSERT  INTO ToolsDB (ToolID, ToolType, TurningDirection, ToolDiameter, ToolStuff, TillID)
            VALUES ( '" + $ToolID + "', '" + $ToolType + "', '" + $TurningDirection + "', '" + $ToolDiameter + "', '" + $ToolStuff + "', '" + $TillID + "') " ;

Enfin si l'opérateur de concaténation est bien le "+" en php.

Autre chose à vérifier : tes variables contiennent-elles des caractères apostrophe ( ' ) . Si oui (ou si ça peut-être oui) pense à les échappés (en générale il faut les doubler) si non ils risquent de faire office de marque de fin de chaine, et donc ta requête ne sera plus valide.
0
Rejoignez-nous