PHP无法显示数据且无报错,背景图片加载失败求助
Alright, let's tackle your problem head-on—your PHP page showing nothing (not even errors) and failing to display background images from the database is definitely fixable with a few key adjustments. Let's break this down step by step:
1. Fix Error Reporting First (Critical!)
Right now, enabling error reporting crashes your site because you might not be initializing it correctly. Add these lines at the very top of all your PHP files (memepage.php, insert.php) to see actual errors instead of a blank screen:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); // Rest of your code below ?>
This will show you exactly what's breaking the page, which is the first step to fixing any PHP issue.
2. Fix the Form Submission Method
Your form has a bug: the method="post" attribute is on the submit button instead of the <form> tag. This means your data is being sent via GET instead of POST, and using $_REQUEST is risky here. Update your form code to:
<form action="insert.php" method="post"> <p class="link">Link her:</p> <br> <input type="text" name="bilde" required> <br> <br> <input type="submit" value="Fullfør"> </form>
Then, in insert.php, use $_POST['bilde'] instead of $_REQUEST['bilde'] to get the input—it's more secure and explicit.
3. Fix SQL Injection Vulnerability & Insert Logic
Your current insert code directly concatenates user input into SQL, which is a huge security risk (SQL injection) and can also cause syntax errors. Use prepared statements instead:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "mydb"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Check if the form was submitted and the input exists if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bilde'])) { $bilde = $_POST['bilde']; // Use prepared statement to prevent SQL injection $sql = "INSERT INTO meme (bilde) VALUES (?)"; $stmt = $conn->prepare($sql); $stmt->bind_param("s", $bilde); // "s" means string type if($stmt->execute()){ echo "Vellykket"; } else{ echo "ERROR: Det oppsto en feil <br> " . $stmt->error; } $stmt->close(); } else { echo "Ingen data mottatt fra skjemaet."; } echo "<br><br><a href='memepage.php'>Tilbake</a>"; $conn->close(); ?>
4. Fix the Image Display Logic
Your query code has two issues:
- Missing quotes around
$row[bilde](associative array keys need to be quoted as strings) - No escaping of the image URL, which can lead to XSS vulnerabilities and broken HTML
Update your display code to:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); // (Add your database connection code here first) $sql = "SELECT bilde FROM meme"; $result = $conn->query($sql); // First check if the query succeeded if ($result) { if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { // Escape the URL to prevent XSS and broken HTML $imageUrl = htmlspecialchars($row['bilde'], ENT_QUOTES); // Make sure the div has a visible height (35% might not work if parent has no height) echo "<div class='box' style='background-image: url({$imageUrl}); background-size: cover; width: 90%; height: 300px; background-position: center;'></div>"; } } else { echo "0 results"; } } else { echo "Feil ved spørring: " . $conn->error; } $conn->close(); ?>
Also, note that height: 35% might not work if the parent element doesn't have a defined height—using a fixed height like 300px temporarily will help you confirm if the image is loading.
5. Verify Image Paths
Double-check that the bilde field in your database contains valid, accessible URLs:
- If it's a local file, use a relative path like
./uploads/your-image.jpg(make sure the file exists at that location) - If it's an external image, use the full URL like
https://example.com/your-image.jpg
内容的提问来源于stack exchange,提问作者Kevin

