[英]Because it only shows the id and not the table info in my index.php
目前我正在讓我的產品顯示它們的狀態“可用”或“缺貨”在我的數據庫中創建一個名為“estado”的新表......在我的產品表中添加一個名為“estado_id”的新列,它存儲在什么狀態是產品。
數據庫圖像:
然后當放置它以便它顯示在我在 index.php 中的產品列表中時,它看起來只是狀態表的 ID。 例如,在下圖中,它顯示“1”,我需要顯示產品的狀態,以說明它是否有貨或缺貨。
索引.php
<div class="productos">
<a href="productos/producto.php?id=<?php echo $productos['id']?>">
<img src="<?php echo '/images/' . $productos['image']; ?>" class="img-test">
<p class="text-test"><?php echo $productos['title']?></p>
<p class="status"><?php echo $productos['tipo_id']; ?></p>
</a>
</div>
productos.php
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');
$table = 'productos';
$categorias = selectAll('categorias');
$estado = selectAll('estado');
$errors = array();
$id = '';
$title = '';
$body = '';
$estadoid = '';
$topic_id = '';
$productos = selectAll($table);
if (isset($_POST['add-post'])) {
$_POST['topic_id']=serialize($_POST['topic_id']);
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = "../../images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result){
$_POST['image'] = $image_name;
} else {
array_push($errors, "¡Algo fallo al subir la imagen!");
}
} else {
array_push($errors, "¡Necesitas subir una imagen!");
}
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Post creado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic = $_POST['topic_id'];
$estadoid = $_POST['estado_id'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Post eliminado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
}
if (isset($_POST['update-post'])){
$_POST['topic_id']=serialize(array_values($_POST['topic_id']));
$_POST['estado_id']=serialize($_POST['estado_id']);
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Post actualizado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$body = $_POST['body'];
$topic = $_POST['topic_id'];
$estadoid = $_POST['estado_id'];
}
}
?>
數據庫文件
<?php
session_start();
require('connect.php');
function dd($value)
{
echo "<pre>", print_r($value, true), "</pre>";
die();
}
function executeQuery($sql, $data)
{
global $conn;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare($sql);
$values = array_values($data);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, ...$values);
$stmt->execute();
return $stmt;
}
function selectAll($table, $conditions = [])
{
global $conn;
$sql = "SELECT * FROM $table";
if (empty($conditions)) {
$sql = $sql . " ORDER BY id DESC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
} else {
// $sql = "SELECT * FROM $table WHERE username='ElVictox' AND admin=1";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$stmt = $conn->prepare($sql);
$values = array_values($conditions);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, $values);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
}
function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table ";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$sql = $sql . " LIMIT 1";
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
function create($table, $data)
{
global $conn;
$sql = "INSERT INTO $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id;
return $id;
}
function update($table, $id, $data)
{
global $conn;
$sql = "UPDATE $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$sql = $sql . " WHERE id=?";
$data['id'] = $id;
$stmt = executeQuery($sql, $data);
return $stmt->affected_rows;
}
function delete($table, $id)
{
global $conn;
$sql = "DELETE FROM $table WHERE id=?";
$stmt = executeQuery($sql, ['id' => $id]);
return $stmt->affected_rows;
}
您需要對第二個表進行左連接,例如:
SELECT t1.*, t2.name as estado FROM productos AS t1 LEFT JOIN estados as t2 ON (t1.estado_id = t2.id)
然后在html上:
<p class="status"><?php echo $productos['estado']; ?></p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.