Comment puis-je convertir des $_POST à cordes, alors affecter à une variable$? Tu veux l'utiliser en SQL
Je ne sais pas comment faire pour convertir ce $_POST à la chaîne et l'ASSIGNER à une variale plutôt qu'un tableau. Est-il un Convertir.ToString(RadioButtonValue) comme en C#? Je veux utiliser la variable comme paramètre dans mon instruction SQL.
$DeptCode = $_POST['Department'];
print_r($DeptCode);
$sql = "SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$DeptCode'";
$results = mysql_query($sql,$con);
if($results != $sql)
{
die('Error' . mysql_error());
}
C'est mon instruction SQL. Ce que je fais mal?
$sql = "SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$DeptCode'";
Quand je le lance... il toujours montrer
Array ( [0] => PD ) Error
C'est l'ensemble du code:
<html>
<head>
<title>New Checkup</title>
</head>
<body>
<h1><a href="http://localhost/clinic/InsertPatient.php">Insert Patient</a></h1><br>
<h1><a href="http://localhost/clinic/InsertEmployee.php">Insert Doctor and Specialization</a></h1>
<h1><a href="http://localhost/clinic/InsertProcedureHTML.php">Insert Products and Services</a></h1>
<h1><a href="http://localhost/clinic/NewCheckup.php">New Checkup</a></h1>
<form method="post">
<?php
//action="http://localhost/clinic/NewCheckup2.php"
$con = mysql_connect('localhost', 'root', "");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_clinic", $con) or die(mysql_error());
$sql = "SELECT DeptCode, DeptName FROM DEPARTMENT";
$results = mysql_query($sql,$con);
while($row=mysql_fetch_assoc($results))
{
echo "<input type='radio' name='Department[]' value='".$row['DeptCode']."'>".$row['DeptName'];
}
mysql_close($con);
?>
<input type="submit" name="btnSubmit">
</form>
<?php
if(isset($_POST['btnSubmit']))
{
$con = mysql_connect('localhost', 'root', "");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_clinic", $con) or die(mysql_error());
$DeptCode = $_POST['Department'];
print_r($DeptCode);
echo $DeptCode;
$sql = "SELECT EMPLOYEE.EmpID, EmpName FROM EMPLOYEE, EMPLOYEE_SPECIALIZATION WHERE EMPLOYEE.EmpID = EMPLOYEE_SPECIALIZATION.EmpID AND EmpStatus='active' AND DeptCode = '$DeptCode'";
$results = mysql_query($sql,$con);
if($results != $sql)
{
die('Error' . mysql_error());
}
mysql_close($con);
}
?>
</body>
- votre
if($results != $sql)
état est complètement faux. au moins un test pour voir si$results !== false
- Ne pas utiliser mysql_* fonctions, utiliser mysqli ou PDO à la place
- stackoverflow.com/q/12859942/2536029
- Des injections SQL ftw ! 🙂
- qu'est-ce que imprimer $DeptCode ?
Vous devez vous connecter pour publier un commentaire.
Utiliser sans crochets
[]
comme ceVous créez un tableau avec le formulaire de saisie:
C'est également affiché si vous ne le
print_r
pour le débogage:Afin de ne pas créer un tableau ou d'accès comme un tableau.
Pour en savoir plus sur les tableaux en PHP en général, voir:
Pour en savoir plus sur les tableaux en PHP Formulaires, veuillez consulter:
Vous pouvez utiliser PHP les variables à cet effet. Une variable variable prend la valeur d'une variable et le considère comme le nom d'une variable. Par exemple:
Référence: http://php.net/manual/en/language.variables.variable.php
utiliser seulement ceci :
je suppose que cela va vous aider.
vous avez maintenant la chaîne de valeur dans $post_string.
vous pouvez également obtenir des données post valeurs par
Essayez ceci:
De l'utiliser dans mysql déclaration:
Je vous suggère fortement de commencer à utiliser PDO si. Son plus sûr