How to properly sanitize user input in PHP: protection against XSS and SQL injection
Hello everyone! I'm a beginner PHP developer and I've encountered a security issue. On my website, there is a registration form and a comments section. I receive data from users and immediately insert it into the database and display it on the page. Recently, I noticed that someone entered the following code in the name field: ``, and when displayed on the page, this script executed. I've also heard about SQL injections, but I don't fully understand how to protect against them.\n\nHere is an example of my code:\n\n```php\n// Getting data from the form\n$name = $_POST['name'];\n$email = $_POST['email'];\n\n// Saving to the database (MySQL)\n$query = "INSERT INTO users (name, email) VALUES ('$name', '$email')";\nmysqli_query($connection, $query);\n\n// Displaying on the page\necho "Hello, $name!";\n```\n\nHow do I properly sanitize the data? Do I need to
No answers yet.
Log in to leave an answer
Blogs
Swift in 2025: Overview of Major New Features and Changes
Working with Databases in TypeScript: PostgreSQL and MongoDB for Beginners
OAuth 2.0 and JWT Authorization in Swift: A Step-by-Step Guide for Beginners
C++ in 2025: An Evolution You Can't Miss