In my program I have an html form that takes in a name as user input:
<form action ="view_process.php" role = "form" method = "post">
....
<input type="text" name="name_in" class="form-control" placeholder="Name here" maxlength = "30">
....
</form>
and then I have php code that puts the user input into a variable
$name_in = trim($_POST['name_in']);
All of this is working, but when I issue the following query to a MySQL database,
$query = "SELECT * FROM students WHERE SID = $id_in AND name = $name_in;";
it only returns the database entry/tuple if the user inputs their name in quotes. What is causing this and how can I fix it? I realize I could write a function to add quotes to the input but I want to understand why this is happening and that does not seem like a good solution. I did some research and tried using addslashes()
, stripslashes()
, and turning on magic quotes, but nothing fixed it.
EDIT: I don't think this is a duplicate of: How can I prevent SQL-injection in PHP? because I am using mysqli and not prepared statements.