[英]PHP connection with SQL server error, activating 3 buttons
我在连接到我的数据库(在 phpMyAdmin 中)和激活三个按钮时遇到问题。 我的数据库是medecin ,我的表是client ,我想激活插入、修改客户端和删除按钮。 接口代码如下:
<fieldset>
<legend>Client</legend>
<table border="1">
<form method=post action="client.php">
<tr>
<td> Id : </td>
<td> <input type="text" name="id_client" /> </td>
</tr>
<tr>
<td> Nom : </td>
<td> <input type="text" name="nom" /> </td>
</tr>
<tr>
<td> Preom : </td>
<td> <input type="text" name="prenom" /> </td>
</tr>
<tr>
<td> Age : </td>
<td> <input type="text" name="age" /> </td>
</tr>
<tr>
<td> Tel : </td>
<td> <input type="text" name="tel" /> </td>
</tr>
<tr>
<td> eMail : </td>
<td> <input type="text" name="mail" /> </td>
</tr>
</table>
<br> <br>
<input type="submit" value="insert" name="Ajouter Client" /> <br> <br>
<input type="submit" value="Modify Client" /> <br> <br>
<input type="submit" value="delete Client" /> <br> <br> <br>
</form>
</form>
</fieldset>
我的 PHP 代码( client.php
)激活按钮:
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'medecin';
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
);
//Connect to MySQL and instantiate our PDO object.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options);
$id_client=$_POST['id_client'];
$nom=$_POST['nom'];
$prenom=$_POST['prenom'];
$age=$_POST['age'];
$tel=$_POST['tel'];
$mail=$_POST['mail'];
if (isset($_POST['insert'])) {
//activating insert button
$sql ="INSERT INTO client VALUES (id_client=? ,nom=? ,prenom=?, age=?, tel=? , mail=?)";
$statement = $pdo->prepare($sql);
$statement->bindValue(1, $id_client);
$statement->bindValue(2, $nom);
$statement->bindValue(3, $prenom);
$statement->bindValue(4, $age);
$statement->bindValue(5, $tel);
$statement->bindValue(6, $mail);
$statement->execute();
$inserted = $statement->execute();
if ($inserted) {
echo 'Row inserted!<br>';
}
} elseif (isset($_POST['supprimer Client'])) {
$sql = 'DELETE from client WHERE id_client= :id_client ';
$statement = $pdo->prepare($sql);
$statement->bindValue(':id_client', $id_client);
$delete = $statement->execute();
} elseif (isset($_POST['Modifier Client'])) {
$sql = "UPDATE medecin SET id_client=?, nom=?, prenom=?, age=?, tel=?, mail=? WHERE id_client=?";
$statement = $pdo->prepare($sql);
$statement->bindValue(':id_client', $id_client);
$statement->bindValue(':nom', $nom);
$statement->bindValue(':prenom', $prenom);
$statement->bindValue(':age', $age);
$statement->bindValue(':tel', $tel);
$statement->bindParam(':mail', $mail);
$statement->execute()
}
?>
我无法弄清楚我出了什么问题。
尝试使用类似的方法连接并将其放入另一个php
文件中,当您想使用它时,请使用include(PHP file name)
或include_once(PHP file name)
。
try{
$dbConnect = new PDO('mysql:host='.$dbHost.';dbname='.$dbName,$dbUser,$dbPass);
$dbConnect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $ex){
echo "ERROR: ".$ex->getMessage();
exit();
}
我还修复了您的PHP
代码,但也有一些错误。
<?php
include_once ('dbconnect.php');
if (isset($_POST['insert']))
//activating insert button
{
$statement = $pdo->prepare("INSERT INTO client VALUES (id_client=:id_client ,nom=:nom ,prenom=:prenom, age=:age, tel=:tel , mail=:mail)");
$statement->bindParam(':id_client', $id_client);
$statement->bindParam(':nom', $nom);
$statement->bindParam(':prenom', $prenom);
$statement->bindParam(':age', $age);
$statement->bindParam(':tel', $tel);
$statement->bindParam(':mail', $mail);
if($statement->execute();){
echo 'Row inserted!<br>';}
}
elseif (isset($_POST['supprimer Client'])) {
$statement = $pdo->prepare("DELETE from client WHERE id_client= :id_client");
$statement->bindParam(':id_client', $id_client);
$statement->execute();
}
elseif (isset($_POST['Modifier Client'])) {
$statement = $pdo->prepare("UPDATE medecin SET id_client=:id_client ,nom=:nom ,prenom=:prenom, age=:age, tel=:tel , mail=:mail WHERE id_client=:id_client");
$statement->bindParam(':id_client', $id_client);
$statement->bindParam(':nom', $nom);
$statement->bindParam(':prenom', $prenom);
$statement->bindParam(':age', $age);
$statement->bindParam(':tel', $tel);
$statement->bindParam(':mail', $mail);
$statement->execute()
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.