Home > Database > Mysql Tutorial > body text

Why is My PHP Form Data Not Inserting into MySQL?

Barbara Streisand
Release: 2024-11-11 20:59:03
Original
693 people have browsed it

Why is My PHP Form Data Not Inserting into MySQL?

PHP: Inserting Values from the Form into MySQL without Using Prepared Statements

Despite creating the users table in MySQL and defining the dbConfig and Index.php files, you're encountering an issue where data is not being inserted into the database when the save button is clicked.

The provided code retrieves values from the form using $_POST but only declares an INSERT query string in the following line:

$sql = "INSERT INTO users (username, password, email)
        VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";
Copy after login

This code doesn't actually execute the query. To execute MySQL queries, you can use the mysqli_query function:

mysqli_query($mysqli, $sql);
Copy after login

However, this approach is vulnerable to SQL injection attacks. It's essential to use prepared statements when handling user input to prevent malicious data manipulation.

Here's an example using prepared statements:

$sql = "INSERT INTO users (username, password, email)
    VALUES (?,?,?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("sss", $_POST['username'], $_POST['password'], $_POST['email']);
$stmt->execute();
Copy after login

The above is the detailed content of Why is My PHP Form Data Not Inserting into MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template