我无法使用于更新才能正常工作的代码就像$ _GET方法不起作用
一切正常,我可以删除并在数据库中插入,但是我无法更新,当将提交按钮的值更改为可编辑并单击它时,它不执行更新语句,而是执行插入语句>
<?php
$sql = "SELECT * FROM brand ORDER BY brand";
$stmt = $con->prepare($sql);
$stmt->execute();
$datarows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(isset($_GET['delete']) && !empty($_GET['delete'])) {
$deleteid = (int)$_GET['delete'];
$sql = "DELETE FROM brand WHERE Bra_ID = ?";
$stmt = $con->prepare($sql);
$stmt->execute(array($deleteid));
header("Location: brands.php");
exit();
}
if (isset($_GET['edit']) && !empty($_GET['edit'])) {
$editid = (int)$_GET['edit'];
$sql = "SELECT * FROM brand WHERE Bra_ID = ?";
$stmt = $con->prepare($sql);
$stmt->execute(array($editid));
$brandsrows = $stmt->fetchAll();
}
if (isset($_POST['submit']) ) {
$brand = sanitize($_POST['brand']);
$errors = array();
if (empty($brand)) {
$errors[] = "Please Enter Brand Name";
}
$sql = "SELECT * FROM brand WHERE brand = '$brand'";
if (isset($_GET['edit'])) { //This not working work
$sql = "SELECT * FROM brand WHERE brand = '$brand' AND
Bra_ID != '$editid'";
}
$stmt = $con->prepare($sql);
$stmt->execute();
$count = $stmt->rowCount();
if($count > 0) {
$errors[] = $brand . " Brand Already Exists";
}
if (!empty($errors)) {
echo displayErrors($errors);
} else {
if (isset($_GET['edit'])) { //This not working work
$sql = "UPDATE brand SET brand = '''$brand' WHERE
Bra_ID='$editid'";
}
$sql = "INSERT INTO brand(brand) VALUES('$brand')";
$stmt = $con->prepare($sql);
$stmt->execute();
header("Location: brands.php");
exit();
}
}
?>
<div class="container-fluid" style="height: 750px">
<h1 class="text-center my-3">Brands Area</h1>
<hr>
<div class="brand-add">
<form action="brands.php" method="POST" class="form-inline">
<label for=""><?=(isset($_GET['edit'])?'Brand To Edit:
':'Brand Name: ')?></label>
<?php
$brandName ='';
if (isset($_GET['edit'])) {
$brandName = $brandsrows[0]['brand'];
} else {
if (isset($_POST['submit'])) {
$brandName = $_POST['brand'];
}
}
?>
<input type="text" class="form-control mx-2" name="brand"
value="<?=$brandName?>">
<?php
if (isset($_GET['edit'])) {
?>
<a href="brands.php" class="btn btn-outline-secondary mr-1">Cancel</a>
<?php
}
?>
<input type="submit" name="submit" class="btn btn-success"
value="<?=(isset($_GET['edit'])?'Edit':'Add')?> New Brand">
</form>
</div>
<hr>
<table class="table table-bordered width-auto table-condensed">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Brands</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php
foreach ($datarows as $rows) {
?>
<tr>
<td>
<a href="brands.php?edit=<?php echo $rows['Bra_ID'] ?>"
class="btn btn-success"> <i class="far fa-edit"></i>
</a>
</td>
<td class="text-primary font-weight-bold"><?php echo $rows['brand'] ?>
</td>
</td>
<td>
<a href="brands.php?delete=<?php echo $rows['Bra_ID'] ?>" class="btn
btn-danger confirm">
<i class="far fa-trash-alt"></i>
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
I want the statment That's used to Update to Work, it's like it dosen't existe Please Help
答案 0 :(得分:-1)
数据库是可交易的,这意味着在每条语句(如电子邮件)之后,您必须先发送,然后(如果您希望得到这样的答复)。< / p>
if (isset($_GET['edit'])) { //This not working work
$sql = "UPDATE brand SET brand = '$brand' WHERE
Bra_ID='$editid'";
//UPDATE query transaction
$stmt = $con->prepare($sql);
$stmt->execute();
}
$sql = "INSERT INTO brand(brand) VALUES('$brand')";
$stmt = $con->prepare($sql);
$stmt->execute();
由于未执行命令,因此变量$ sql被覆盖
if (isset($_GET['edit'])) { //This not working work
$sql = "UPDATE brand SET brand = '''$brand' WHERE
Bra_ID='$editid'";
}
$sql = "INSERT INTO brand(brand) VALUES('$brand')";
echo($sql);
$stmt = $con->prepare($sql);
$stmt->execute();
在上面的语句中,$ sql将回显INSERT命令,因为它覆盖了先前的语句,然后执行了。