我正在尝试更新我的数据库表。所有代码都在同一页面上。我已经应用了几个条件,但它们都没有工作。当我点击提交按钮时,它会刷新页面并且不会更改表格行。有什么建议吗?
代码
<?php
session_start();
include "db.php";
if(isset($_SESSION['sess_user'])) {
$id = $_GET['id'];
?>
<form name="form1" method="post" enctype="multipart/form-data">
<?php
$sql = "SELECT * FROM categories WHERE cat_id=" . $id;
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($rows = mysqli_fetch_array($result)) {
?>
<h4>Name:</h4> <input type="text" name="cat_name" value=" <?= $rows['cat_name'] ?>">
<input type="file" name="file" value="<?= $rows['cat_image']; ?>">
<input name="id" type="hidden" id="id" value="<?= $rows['cat_id']; ?>">
<?php
}
}
?>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['cat_name'];
$image = $_FILES['file']['name'];
$temp_name = $_FILES["file"]["tmp_name"];
$id = $_POST['id'];
$query1 = "select * from categories where cat_name='$name' OR cat_image='$image'";
$search_pic = mysqli_query($con, $query1);
$count = mysqli_num_rows($search_pic);
if ($count == 0) {
// update data in mysql database
$sql = "UPDATE categories SET cat_name='$name', cat_image='$image' WHERE cat_id=$id";
$result = mysqli_query($con, $sql);
if ($result) {
$m = move_uploaded_file($_FILES['file']['tmp_name'], 'images/' . $image);
echo "<script>alert('Row updated succesfully! ')</script>";
header('Location:category_admin.php');
} else {
echo "<script>alert('Row does not updated ')</script>";
}
} else {
echo "<script>alert('Row with the same name exists! ')</script>";
}
}
}
else
{
echo "You must Login First goto : <a href='index.php'>Admin Login </a>";
}
?>