100
有太多内部联接供我查看,所以我非常感谢。
我所缺少的是要显示的图像。
在第四张图片上,您可以看到它的外观。
它在该页面上有效,但不适用于我现在正在做的那个页面。
<?php
$html = '';
if (isset($_GET['sletBruger'])) {
$id = secInput($_GET['id']);
$billede = secInput($_GET['billede']);
if (checkElement($id)) {
if (is_file('../img/'.$billede)) {
unlink('../img/'.$billede);
}
$sql = "DELETE brugere.*, bruger_billeder.* FROM brugere
INNER JOIN brugerroller
ON brugerroller.ID = brugere.fk_rolle
INNER JOIN bruger_billeder
ON bruger_billeder.billednavn = bruger_billeder.fk_bruger
WHERE brugere.ID = ".$id;
deleteFromDb($sql);
}
}
?>
<h5>Oprettede Brugere</h5>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>NAVN</th>
<th>EMAIL</th>
<th>MOBILNUMMER</th>
<th>NIVEAU</th>
<th>KILOMETER</th>
<th>BILLEDE</th>
<th>REDIGER</th>
<th>SLET</th>
</tr>
</thead>
<tbody>
<?php
//SQL - henter det som vi har valgt, i databasen. Inner join parrer brand og kategori fra produklisten, med hhv brand listen og kategorilisten.
$sql = "SELECT DISTINCT
brugere.*, brugere.navn, brugere.email, brugere.mobil, brugere.kilometer,
faerdighedsroller.f_navn, brugerroller.rollenavn, bruger_billeder.billednavn
FROM brugere
INNER JOIN faerdighedsroller
ON faerdighedsroller.ID = brugere.fk_faerdighed
INNER JOIN brugerroller
ON brugere.fk_rolle = brugerroller.ID
INNER JOIN bruger_billeder
ON bruger_billeder.billednavn = bruger_billeder.fk_bruger
ORDER BY ID DESC";
$arr = getData($sql, $conn);
foreach($arr as $value){
$html .= '
<tr>
<td>'.$value['ID'].'</td>
<td>'.$value['navn'].'</td>
<td>'.$value['email'].'</td>
<td>'.$value['mobil'].'</td>
<td>'.$value['f_navn'].'</td>
<td>'.$value['kilometer'].'</td>
<td><img src="../images/' . $value['billednavn'] . '" alt="' . $value['billednavn'] . '"></td>
<td><a href="?s=sections/redigerBruger&id='.$value['ID'].'"><i class="fa fa-edit ">Rediger</i></a></td>
<td><a class="btn btn-danger" onclick="return confirm(\'Er du sikker på du vil slette '.$value['navn'].'\');" href="?s=sections/brugere&id='.$value['ID'].'&sletBruger">Slet</a></td>
</tr>
';
}
$html .= '
</tbody></table>
';
return $html;
?>
我需要它来显示另一张桌子上的图像。
警告:第63行的kajakklubben-h \ admin \ sections \ brugere.section.php中为foreach()提供的参数无效
答案 0 :(得分:0)
可以这么说,这个答案是第一个测试版本。它的目的是找出问题的确切根源(在PHP函数代码中,在数据库设置中或在数据库表中的数据中)。
因此,请按照原样使用我的两个页面(brugere.php
和connect.php
)而不是PHP页面。但是,首先,在页面connect.php
中,使用您的数据库凭据来更改我的数据库凭据!运行两个页面,看看它是如何工作的。根据结果或最终引发的错误/异常/警告,我们将为您解决问题提供一个起点。
注意:首先,让我们同意将精力集中在选择记录部分(例如,成功运行SELECT
语句),而不是删除表数据部分(例如,成功运行DELETE
语句)。
我还提供了在测试中使用的表定义和数据的列表。也许我们将不需要它。但是我发布了它,以便您可以自己定义表及其关系的定义方式。
由于您未提供正在使用的数据访问扩展名( PDO ,面向对象的mysqli 或过程化mysqli ),我决定在代码中使用 PDO 。找到问题后,将代码轻松切换到您正在使用的任何扩展名即可。
我将等待您的反馈。
祝你好运。
brugere.php:
<?php
require 'connection.php';
$brugerDeleted = FALSE;
$imagesDir = '../images/';
if (isset($_GET['sletBruger'])) {
$brugerId = $_GET['id'] ?? '';
/*
* ===========================================
* Validate the bruger id sent in query string
* ===========================================
*/
if (empty($brugerId)) {
$errors[] = 'No bruger id provided. Please provide one.';
} elseif (!is_numeric($brugerId)) {
$errors[] = 'The provided bruger id must be an integer.';
} else {
/*
* =========================================================
* Get the billednavn for the bruger id sent in query string
* =========================================================
*/
$sql = 'SELECT billednavn
FROM bruger_billeder
WHERE fk_bruger = :fk_bruger';
$statement = $connection->prepare($sql);
$statement->execute([
':fk_bruger' => $brugerId,
]);
$billednavn = $statement->fetchColumn(0);
/*
* ===========================================================
* Delete the file defined by the value of "billednavn" column
* in "bruger_billeder" table from the images directory
* ===========================================================
*/
if (FALSE !== $billednavn && is_file($imagesDir . $billednavn)) {
unlink($imagesDir . $billednavn);
}
/*
* ===================================================================
* Delete the bruger identified by the bruger id sent in query string.
* The corresponding billednavn is automaticaly deleted too, because
* of the FK defined in "bruger_billeder" table.
* ===================================================================
*/
$sql = 'DELETE FROM brugere
WHERE ID = :ID';
$statement = $connection->prepare($sql);
$statement->execute([
':ID' => $brugerId,
]);
$brugerDeleted = TRUE;
}
}
/*
* =======================
* Get the list of brugere
* =======================
*/
$sql = 'SELECT
brug.*,
fr.f_navn,
br.rollenavn,
bb.billednavn
FROM brugere AS brug
INNER JOIN bruger_billeder AS bb ON brug.ID = bb.fk_bruger
INNER JOIN faerdighedsroller AS fr ON brug.fk_faerdighed = fr.ID
INNER JOIN brugerroller AS br ON brug.fk_rolle = br.ID
ORDER BY brug.ID DESC';
$statement = $connection->prepare($sql);
$statement->execute();
$brugere = $statement->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo</title>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<style type="text/css">
body { padding: 30px; }
.centered { text-align: center; }
img { max-width: 200px !important; }
</style>
</head>
<body>
<div class="container">
<h5>Oprettede Brugere</h5>
<?php
if (isset($errors)) {
foreach ($errors as $error) {
?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $error; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php
}
} elseif ($brugerDeleted) {
?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
The selected bruger has been successfully deleted.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php
}
?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>NAVN</th>
<th>EMAIL</th>
<th>MOBILNUMMER</th>
<th>NIVEAU</th>
<th>KILOMETER</th>
<th class="centered">BILLEDE</th>
<th class="centered">REDIGER</th>
<th class="centered">SLET</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($brugere)) {
foreach ($brugere as $bruger) {
$id = $bruger['ID'];
$navn = $bruger['navn'];
$email = $bruger['email'];
$mobil = $bruger['mobil'];
$kilometer = $bruger['kilometer'];
$fNavn = $bruger['f_navn'];
$rollenavn = $bruger['rollenavn'];
$billednavn = $bruger['billednavn'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $navn; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $mobil; ?></td>
<td><?php echo $fNavn; ?></td>
<td><?php echo $kilometer; ?></td>
<td class="centered">
<img src="<?php echo $imagesDir . $billednavn; ?>" alt="<?php echo $billednavn; ?>" class="img-fluid img-thumbnail" />
</td>
<td class="centered">
<a href="?s=sections/redigerBruger&id=<?php echo $id; ?>"
class="btn btn-success btn-sm">
<i class="fa fa-edit"></i>
</a>
</td>
<td class="centered">
<a href="?s=sections/brugere&id=<?php echo $id; ?>&sletBruger"
onclick="return confirm('Er du sikker på du vil slette \'<?php echo $navn; ?>\'');"
class="btn btn-danger btn-sm">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="9">
No records found
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</html>
connection.php:
<?php
/*
* This page contains the code for creating a PDO connection instance.
*/
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'tests');
define('USERNAME', 'root');
define('PASSWORD', 'root');
define('CHARSET', 'utf8');
/*
* Error reporting.
*
* @link http://php.net/manual/en/function.error-reporting.php
*/
error_reporting(E_ALL);
ini_set('display_errors', 1); /* SET IT TO 0 ON A LIVE SERVER! */
/*
* Create a PDO instance as db connection to db.
*
* @link http://php.net/manual/en/class.pdo.php
* @link http://php.net/manual/en/pdo.constants.php
* @link http://php.net/manual/en/pdo.error-handling.php
* @link http://php.net/manual/en/pdo.connections.php
*/
$connection = new PDO(
sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s', HOST, PORT, DATABASE, CHARSET)
, USERNAME
, PASSWORD
, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_PERSISTENT => FALSE,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
表“ bruger_billeder”:
CREATE TABLE `bruger_billeder` (
`fk_bruger` int(11) unsigned NOT NULL,
`billednavn` varchar(255) NOT NULL,
PRIMARY KEY (`fk_bruger`),
CONSTRAINT `bruger_billeder_ibfk_1` FOREIGN KEY (`fk_bruger`) REFERENCES `brugere` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `bruger_billeder` (`fk_bruger`, `billednavn`)
VALUES
(1,'image-3.jpg'),
(2,'image-1.jpg'),
(3,'image-4.jpg'),
(4,'image-1.jpg'),
(5,'image-2.jpg');
表格“ brugere”:
CREATE TABLE `brugere` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`navn` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`mobil` varchar(8) NOT NULL,
`kilometer` varchar(255) NOT NULL,
`fk_tilmelding` int(11) NOT NULL,
`fk_faerdighed` int(11) unsigned NOT NULL,
`fk_rolle` int(11) unsigned NOT NULL,
PRIMARY KEY (`ID`),
KEY `fk_faerdighed` (`fk_faerdighed`),
KEY `fk_rolle` (`fk_rolle`),
CONSTRAINT `brugere_ibfk_1` FOREIGN KEY (`fk_faerdighed`) REFERENCES `faerdighedsroller` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `brugere_ibfk_2` FOREIGN KEY (`fk_rolle`) REFERENCES `brugerroller` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `brugere` (`ID`, `navn`, `email`, `password`, `mobil`, `kilometer`, `fk_tilmelding`, `fk_faerdighed`, `fk_rolle`)
VALUES
(1,'asdf','kjhkjhj','ziasdf','kjhdf','234',0,2,5),
(2,'hhkjh','iuzaiusdf','mashdfk','ziqewr','673',0,2,5),
(3,'izziu','trere','jhghjf','bnbngh','877',0,2,1),
(4,'oiuoiu','rtzadskf','kjhfasdf','cbvcbvc','654',0,3,3),
(5,'piuoiu','rztrtr','mnbfd','ewtre','49',0,1,4);
表“ brugerroller”:
CREATE TABLE `brugerroller` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`rollenavn` varchar(25) NOT NULL,
`niveau` tinyint(2) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `brugerroller` (`ID`, `rollenavn`, `niveau`)
VALUES
(1,'jhkjhj',1),
(2,'iuzot',0),
(3,'uziqwezr',0),
(4,'etretre',1),
(5,'poiuiou',0);
表“ faerdighedsroller”:
CREATE TABLE `faerdighedsroller` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`f_navn` varchar(255) NOT NULL,
`niveau` tinyint(3) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `faerdighedsroller` (`ID`, `f_navn`, `niveau`)
VALUES
(1,'iuziuz',0),
(2,'iouoiu',0),
(3,'uzizu',1),
(4,'kljh',0),
(5,'trerte',1);
答案 1 :(得分:0)
我解决了这个问题,谢谢您的所有帮助
<?php
$html = '';
if (isset($_GET['sletBruger'])) {
$id = secInput($_GET['id']);
$billede = secInput($_GET['billede']);
if (checkElement($id)) {
if (is_file('../img/'.$billede)) {
unlink('../img/'.$billede);
}
$sql = "DELETE brugere.*, bruger_billeder.* FROM brugere
INNER JOIN brugerroller
ON brugerroller.ID = brugere.fk_rolle
INNER JOIN bruger_billeder
ON bruger_billeder.fk_bruger = bruger.ID
WHERE brugere.ID = ".$id;
deleteFromDb($sql);
}
}
?>
<h5>Oprettede Brugere</h5>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>NAVN</th>
<th>EMAIL</th>
<th>MOBILNUMMER</th>
<th>NIVEAU</th>
<th>KILOMETER</th>
<th>BILLEDE</th>
<th>REDIGER</th>
<th>SLET</th>
</tr>
</thead>
<tbody>
<?php
//SQL - henter det som vi har valgt, i databasen. Inner join parrer brand og kategori fra produklisten, med hhv brand listen og kategorilisten.
$sql = "SELECT DISTINCT
brugere.*, brugere.navn, brugere.email, brugere.mobil, brugere.kilometer,
faerdighedsroller.f_navn, brugerroller.rollenavn, bruger_billeder.billednavn
FROM brugere
INNER JOIN faerdighedsroller
ON faerdighedsroller.ID = brugere.fk_faerdighed
INNER JOIN brugerroller
ON brugere.fk_rolle = brugerroller.ID
INNER JOIN bruger_billeder
ON bruger_billeder.fk_bruger = brugere.ID
ORDER BY ID DESC";
$arr = getData($sql, $conn);
foreach($arr as $value){
$html .= '
<tr>
<td>'.$value['ID'].'</td>
<td>'.$value['navn'].'</td>
<td>'.$value['email'].'</td>
<td>'.$value['mobil'].'</td>
<td>'.$value['f_navn'].'</td>
<td>'.$value['kilometer'].'</td>
<td><img src="../images/' . $value['billednavn'] . '" alt="' . $value['billednavn'] . '"></td>
<td><a href="?s=sections/redigerBruger&id='.$value['ID'].'"><i class="fa fa-edit ">Rediger</i></a></td>
<td><a class="btn btn-danger" onclick="return confirm(\'Er du sikker på du vil slette '.$value['navn'].'\');" href="?s=sections/brugere&id='.$value['ID'].'&sletBruger">Slet</a></td>
</tr>
';
}
$html .= '</tbody></table>';
return $html;
?>