当我尝试编辑/更新我的表时出现错误。这很奇怪,因为我可以很好地将新数据插入到数据库中,之后我可以删除它。我只在编辑/更新数据时遇到问题。
致命错误:未捕获的异常' PDOException' with message' SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#< id&= 39; 1'''''''''在第6行'第64行的C:\ Program Files(x86)\ Ampps \ www \ Gats \ editargat.php
这是代码:
<?php
error_reporting( ~E_NOTICE );
require_once 'dbconn.php';
if(isset($_GET['edit_id'])){
$id = $_GET['edit_id'];
$editar = $conn->prepare('SELECT nom, sexe, edat, foto FROM TaulaGats WHERE id=:uid');
$editar->execute(array(':uid'=>$id));
$edit_row = $editar->fetch(PDO::FETCH_ASSOC);
extract($edit_row);
}
else{
header("Location: index.php");
}
if(isset($_POST['btn_save_updates'])){
$nom = $_POST['nom'];
$sexe = $_POST['sexe'];
$edat = $_POST['edat'];
$foto = $_FILES['imatge']['name'];
$carpeta_tmp = $_FILES['imatge']['tmp_name'];
$imatge_mida = $_FILES['imatge']['size'];
if($foto){
$carpetaImatges = 'imatges/';
$imatge_ext = strtolower(pathinfo($foto, PATHINFO_EXTENSION));
$extensions_vàlides = array('jpeg', 'jpg', 'png', 'gif');
$pic = rand(1000,1000000).".".$imatge_ext;
if(in_array($imatge_ext, $extensions_vàlides)){
if($imatge_mida < 5000000){
unlink($carpetaImatges.$edit_row['foto']);
move_uploaded_file($carpeta_tmp, $carpetaImatges.$pic);
}
else{
$error = "La foto és massa gran.";
}
}
else{
$error = "Només les extensions JPG, JPEG, PNG & GIF són admeses.";
}
}
else{
$pic = $edit_row['foto'];
}
if(!isset($error)){
$prepIexec = $conn->prepare('UPDATE TaulaGats
SET nom=:unom,
sexe=:usexe,
edat=:uedat,
foto=:ufoto,
WHERE id=:uid');
$prepIexec->bindParam(':unom', $nom);
$prepIexec->bindParam(':usexe', $sexe);
$prepIexec->bindParam(':uedat', $edat);
$prepIexec->bindParam(':ufoto', $pic);
$prepIexec->bindParam(':uid', $id);
if($prepIexec->execute()){
header("refresh:1;index.php");
}
else{
$error = "No s'ha pogut editar.";
}
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gats</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="estil.css">
<link rel="stylesheet" href="estil_2.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="jquery-1.11.3-jquery.min.js"></script>
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php" title='Gats disponibles'>Disponibles</a>
<a class="navbar-brand" href="reservats.php" title='Gats reservats'>Reservats</a>
<a class="navbar-brand" href="adoptats.php" title='Gats adoptats'>Adoptats</a>
<a class="navbar-brand" href="acollida.php" title="Cases d'acollida">Acollida</a>
<a class="navbar-brand" href="voluntaris.php" title="Llista voluntaris">Voluntaris</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1 class="h2">Editar<a class="btn btn-default" href="index.php">Veure llista gats</a></h1>
</div>
<div class="clearfix"></div>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
if(isset($error)){
?>
<div class="alert alert-danger">
<?php echo $error; ?>
</div>
<?php
}
?>
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Nom</label></td>
<td><input class="form-control" type="text" name="nom" value="<?php echo $nom; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Sexe</label></td>
<td><input class="form-control" type="text" name="sexe" value="<?php echo $sexe; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Edat</label></td>
<td><input class="form-control" type="text" name="edat" value="<?php echo $edat; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Foto</label></td>
<td>
<p><img src="imatges/<?php echo $foto; ?>" height="150" width="200" /></p>
<input class="input-group-edit" type="file" name="imatge" accept="image/*" />
</td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-default btn-guardar">
Guardar
</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
的index.php:
<?php
require_once 'dbconn.php';
if(isset($_GET['eliminar_id']))
{
$prepIexec = $conn->prepare('SELECT foto FROM TaulaGats WHERE id =:uid');
$prepIexec->execute(array(':uid'=>$_GET['eliminar_id']));
$imatgeRow = $prepIexec->fetch(PDO::FETCH_ASSOC);
unlink("imatges/".$imatgeRow['foto']);
$eliminar = $conn->prepare('DELETE FROM TaulaGats WHERE id =:uid');
$eliminar->bindParam(':uid', $_GET['eliminar_id']);
$eliminar->execute();
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>Gats</title>
<link rel="stylesheet" href="estil_index.css">
<link rel="stylesheet" href="estil.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php" title='Gats disponibles'>Disponibles</a>
<a class="navbar-brand" href="reservats.php" title='Gats reservats'>Reservats</a>
<a class="navbar-brand" href="adoptats.php" title='Gats adoptats'>Adoptats</a>
<a class="navbar-brand" href="acollida.php" title="Cases d'acollida">Acollida</a>
<a class="navbar-brand" href="voluntaris.php" title="Llista voluntaris">Voluntaris</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1>Gats en adopció
<a class="btn btn-default" href="afegirgat.php"> + Afegir nou gat </a>
</h1>
</div>
<br />
<div class="row">
<?php
$prepIexec = $conn->prepare('SELECT id, nom, sexe, edat, foto FROM TaulaGats ORDER BY id ASC');
$prepIexec->execute();
if($prepIexec->rowCount() > 0)
{
while($row = $prepIexec->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<div class="col-xs-3">
<p class="page-header infoGats_nom"><?php echo $nom ?></p>
<p class="infoGats">
<?php echo $sexe." / ".$edat; ?>
</p>
<img src="imatges/<?php echo $row['foto']; ?>" class="img-rounded" width="250px" height="200px" />
<p class="page-header">
<span>
<a class="btn btn-info" href="editargat.php?edit_id=<?php echo $row['id']; ?>" title="click per editar"><span class="glyphicon glyphicon-edit"></span> Editar </a>
<a class="btn btn-success" href="mouregat.php?moure_id=<?php echo $row['id']; ?>" title="click per moure"><span class="glyphicon glyphicon-edit"></span> Moure </a>
<a class="btn btn-danger" href="?eliminar_id=<?php echo $row['id']; ?>" title="click per eliminar" onclick="return confirm('Estàs segur que vols el·liminar aquestes dades?')"><span class="glyphicon glyphicon-remove-circle"></span> El·liminar </a>
</span>
</p>
</div>
<?php
}
}
else
{
?>
<div class="col-xs-12">
<div class="alert alert-warning">
No hi ha gats per adoptar.
</div>
</div>
<?php
}
?>
</div>
</div>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>