HTML表单关联PHP数据库插入脚本失效,请求技术支援
Troubleshooting Your HTML/PHP Form Issue
Let's break down the issues step by step and fix them for you:
1. Root Cause of the "Download PHP Script" Problem
When your browser tries to download the PHP file instead of executing it, this means your web server (like Apache or Nginx) isn't configured to process PHP scripts. Here's how to check:
- Ensure PHP is installed on your server. Run
php -vin the terminal to verify. - For Apache: Make sure the
mod_phpmodule is enabled, and your virtual host configuration includesAddType application/x-httpd-php .php. - For Nginx: Confirm you have the PHP-FPM service running, and your server block has the proper
fastcgi_passdirective pointing to PHP-FPM. - Also, double-check that your PHP file has a
.phpextension (not.txtor anything else).
2. Fixes for Your HTML Form
Your HTML has several syntax errors and missing configurations that prevent proper submission:
- The
<form>tag'sactionattribute is empty – set it to the path of your PHP script (e.g.,action="submit_issue.php"). - Typos:
<tittle>should be<title>,maxlenght→maxlength,type"text"→type="text",<img>tag should be closed properly (<img src="logo.png" width="100%" />). - Radio button name inconsistency: The first radio group has
name="lost call"(with space) and latername="lost_call"– keep the name consistent for proper value submission. - Invalid input type:
date-time-localshould bedatetime-local. - Broken tags: There's an unclosed
<br>tag (<br <br>– fix that to<br>). - For multi-line description, use
<textarea>instead of a text input for better user experience.
Here's the corrected HTML:
<html> <head> <title>FORMAT FOR TECHNICAL ISSUES</title> <link rel="stylesheet" href="/var/www/html/proyecto/css/styles.css"> <hr> </head> <body> <div> <img src="logo.png" width="100%" /> <hr> <form method="POST" action="submit_issue.php"> <input type="text" placeholder="NOMBRE" name="Nombre" maxlength="30" size="20" id="nombre"> <input type="text" name="Extension" placeholder="EXTENSION" maxlength="5" id="extension"> <br> <hr> <br><br> <h2>CAMPAIGN /CAMPAÑA</h2> <br> <label for="Campaign">Select your campaign:</label> <select name="Campaign" id="campaign"> <option value="Procall">Procall</option> <option value="Tigo">Tigo</option> <option value="Spanish">Spanish</option> </select> <hr> <h2>KIND OF FAILURE</h2> <label for="incident">Select the type of incident:</label> <select id="incident" name="incidente" placeholder="Tipo de incidente"> <option value="Slow Computer">Slow Computer</option> <option value="Headset fail">Headset Fail</option> <option value="Zoiper Issue">Zoiper Issue</option> <option value="Crm Issue">Crm Issue</option> <option value="Spark Issue">Spark Issue</option> <option value="Vc Dialer Issue">VC Dialer Issue</option> <option value="Network Issue">Network Issue</option> </select> <textarea id="description" name="Description" placeholder="DESCRIPTION" style="width:300px;height:100px"></textarea> <br><br> <h2>FAILURE DATE</h2> <label for="failure_date">Enter the day and hour when failure occurred:</label> <input name="failure_date" type="datetime-local" placeholder="yyyy/mm/dd/hh/mm" id="failure_date" min="2018-05-01T08:30" max="2018-12-30T22:30"> <br> <hr> <label for="lost_call">Did you lose any call?:</label> <br> <input type="radio" name="lost_call" value="yes" id="lost_call_yes"> Yes, I lost a call. <br> <input type="radio" name="lost_call" value="no" id="lost_call_no"> No, I have not lost any call <br><br> <label for="document">Did you lose any document or information?:</label> <br> <input type="radio" name="document" value="yes"> Yes, I lost one or more documents <br> <input type="radio" name="document" value="no"> No, I have not lost any document. <br><br> <label for="logout">Did you have to log out?:</label> <br> <input type="radio" name="logout" value="yes">Yes, I had to logout <br> <input type="radio" name="logout" value="no">No, I didn't have to logout <br><br><br><br> <input type="submit" value="Submit Issue"> </form> </div> </body> </html>
3. Critical Fixes for Your PHP Script
Your PHP code uses outdated functions and has security vulnerabilities:
- The
mysql_*functions are completely deprecated – usemysqli(improved MySQL extension) instead. - Directly inserting
$_POSTvalues into SQL causes SQL injection attacks – use prepared statements to prevent this. - Table name
tec-issuescontains a hyphen, so you need to wrap it in backticks (`tec-issues`) in your SQL query. - Add a check to ensure the script only runs when the form is submitted (to avoid errors when accessing the PHP file directly).
- Properly handle missing POST values (you can add validation if needed).
Here's the corrected PHP script (save as submit_issue.php):
<?php // Only run if form is submitted via POST if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Use mysqli instead of deprecated mysql_* functions $con = mysqli_connect("localhost", "root", "m0l0t0v", "it"); // Check connection if (!$con) { die('Could not connect: ' . mysqli_connect_error()); } // Prepare and bind parameters to prevent SQL injection $stmt = mysqli_prepare($con, "INSERT INTO `tec-issues` (Nombre, Extension, Campaign, incidente, Description, failure_date, lost_call, document, logout) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($stmt, "sssssssss", $_POST['Nombre'], $_POST['Extension'], $_POST['Campaign'], $_POST['incidente'], $_POST['Description'], $_POST['failure_date'], $_POST['lost_call'], $_POST['document'], $_POST['logout'] ); // Execute the statement if (mysqli_stmt_execute($stmt)) { echo "1 record added successfully!"; } else { die('Error: ' . mysqli_stmt_error($stmt)); } // Clean up mysqli_stmt_close($stmt); mysqli_close($con); } else { // Redirect back to form if accessed directly header("Location: your_form_page.html"); exit; } ?>
Final Checks
- Make sure both files are in the same directory on your server, or adjust the
actionpath accordingly. - Verify your MySQL database
itexists, and the tabletec-issueshas all the columns specified in the INSERT query with matching data types. - Ensure the MySQL user
roothas permission to insert data into thetec-issuestable.
内容的提问来源于stack exchange,提问作者average
相关产品推荐
相关产品推荐

