在下面的代码中,它在“联系我们”表单中提交消息时获取用户的信息,但是当我单击提交按钮时,数据库不显示数据,只显示消息”消息发送成功。” ,但是如果我上传文件,该文件将被下载到我的网络服务器中,而不会显示在 phpMyAdmin 的数据库中。 我确信 SQL 代码是完美的,因为数据库是在 phpMyAdmin 中创建的。 如何查看已上传的文件并从 phpMyAdmin 查看它?
<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "contact_form_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$phone = mysqli_real_escape_string($conn, $_POST['phone']);
$message = mysqli_real_escape_string($conn, $_POST['message']);
$file_path = '';
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$file_tmp = $_FILES['file']['tmp_name'];
$file_name = basename($_FILES['file']['name']);
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$allowed_types = ['image/jpeg', 'image/png', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
$max_size = 5 * 1024 * 1024;
if (in_array($file_type, $allowed_types) && $file_size <= $max_size) {
$upload_dir = 'uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0755, true);
}
$unique_file_name = uniqid('file_') . "_" . $file_name;
$file_path = $upload_dir . $unique_file_name;
if (move_uploaded_file($file_tmp, $file_path)) {
echo "<p>File uploaded successfully!</p>";
} else {
echo "<p>There was an error uploading your file.</p>";
}
} else {
echo "<p>Invalid file type or size. Please upload a valid file.</p>";
}
}
$sql = "INSERT INTO contact_info (Name, Email, Phone, Message, FilePath)
VALUES ('$name', '$email', '$phone', '$message', '$file_path')";
if ($conn->query($sql) === TRUE) {
echo "<p>Message sent successfully!</p>";
} else {
echo "<p>Error: " . $conn->error . "</p>";
}
$conn->close();
}
?>
<!-- Contact Form -->
<div class="contact-form-container">
<div class="contact-info-left">
<h2>Contact Us</h2>
<div class="contact-details">
<p><i class="fas fa-map-marker-alt"></i> Zone 2, 3rd Floor, KOM4<br>
Knowledge Oasis Muscat<br>
PO Box 35, PC 135 Rusayl<br>
Sultanate of Oman</p><br>
<p><i class="fas fa-phone-alt"></i> 80077767 / +968 2417 1111</p><br>
<p><i class="fas fa-clock"></i> from 8 AM - 5:30 PM</p><br>
</div>
</div>
<form class="contact-form" method="POST" action="" enctype="multipart/form-data">
<!-- Personal Information Section -->
<div class="form-section">
<h3>Personal Information</h3>
<label for="name">Name</label>
<input type="text" id="name" name="name" pattern="[A-Za-z]+" title="Only letters are allowed" required>
<label for="phone">Phone Number</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{8,15}" title="Phone number should contain only numbers and be 8 to 15 digits long" required oninput="this.value = this.value.replace(/[^0-9]/g, '');">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="file">Attach File (optional)</label>
<input type="file" id="file" name="file" accept="image/*,.pdf,.docx,.txt">
</div>
<!-- Message Details Section -->
<div class="form-section">
<h3>Message Details</h3>
<label for="message">Message</label>
<textarea id="message" name="message" rows="10" required></textarea>
</div>
<button type="submit">Send Message</button>
</form>
<script>
function submitForm(event) {
event.preventDefault();
alert("Message sent successfully!");
}
</script>
</div>
我不知道HTML代码是否与此有关,但我确实尝试从ChatGPT获得帮助,它一直说代码没有问题。有时它会改变代码中的许多内容并使其完全不同。 我尝试更改变量名称,但仍然不起作用。
您的 submti 按钮位于
<form>
元素的外侧,将其放置在表单元素内,如下所示
`您的输入字段...
发送消息
`