Answer the question
In order to leave comments, you need to log in
Why is the database not updated?
Good afternoon!
I copy the code from the author of the video tutorial. I do everything like the author, but his code works, but I don’t. In the video tutorial, we are talking about creating a basket, writing to it and updating it. When you click the "Update" button on the page
1_eshop.ru/admin/admin.html , the row in the database should be updated, but this does not happen. If at the moment of clicking "Refresh" in the browser you open the "Network" tab, then nothing happens there, nor any reaction of the browser to this click. I will post the code of several files that (as it seems to me) are related to this problem:
File admin.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="goods-out"></div>
<h2>Товар</h2>
<p>Имя: <input type="text" id="gname"></p>
<p>Стоимость: <input type="text" id="gcost"></p>
<p>Описание: <textarea id="gdescr"></textarea></p>
<p>Изображение: <input type="text" id="gimg"></p>
<p>Порядок: <input type="text" id="gorder"></p>
<input type="hidden" id="gid">
<button class="add-to-db">Обновить</button>
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/admin.js"></script>
</body>
</html>
<?php
$action = $_POST['action'];
require_once 'function.php';
switch ($action) {
case 'init':
init();
break;
case "selectOneGoods":
selectOneGoods();
break;
case 'updateGoods':
updateGoods();
break;
}
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "eshop";
function connect(){
$conn = mysqli_connect("localhost", "root", "", "eshop");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn, "utf8");
return $conn;
}
function init(){
//вывожу список товаров
$conn = connect();
$sql = "SELECT id, name FROM goods";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$out = array();
while($row = mysqli_fetch_assoc($result)) {
$out[$row["id"]] = $row;
}
echo json_encode($out);
} else {
echo "0";
}
mysqli_close($conn);
}
function selectOneGoods() {
$conn = connect();
$id = $_POST['gid'];
$sql = "SELECT * FROM goods WHERE id = '$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo json_encode($row);
} else {
echo "0";
}
mysqli_close($conn);
}
function updateGoods() {
$conn = connect();
$id = $_POST['id'];
$name = $_POST['gname'];
$cost = $_POST['gcost'];
$descr = $_POST['gdescr'];
$ord = $_POST['gorder'];
$img = $_POST['gimg'];
$sql = "UPDATE goods SET name='$name', cost='$cost', description='$descr', ord='$ord', img='$img' WHERE id='$id' ";
if (mysqli_query($conn, $sql)) {
echo "1";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question