Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Notice: Undefined index: First Name in C:\xampp\htdocs\egx\code2.php on line 3

Notice: Undefined index: Second Name in C:\xampp\htdocs\egx\code2.php on line 4

Notice: Undefined index: Email in C:\xampp\htdocs\egx\code2.php on line 5

Notice: Undefined index: password in C:\xampp\htdocs\egx\code2.php on line 6
Error:You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Name,Second Name,Email,Password) VALUES('','','','')' at line 1


What I have tried:

<?php
require_once('connection.php');
$n1=$_POST['First Name'];
$n2=$_POST['Second Name'];
$e=$_POST['Email'];
$p=$_POST['password'];
$ins="INSERT INTO signup(First Name,Second Name,Email,Password)
VALUES('$n1','$n2','$e','$p')";
if(!mysqli_query($conn,$ins)) {die("Error:" . mysqli_error($conn));}
else echo "your information was inserted successfully";
?>
Posted
Updated 18-Jan-20 16:51pm
Comments
[no name] 18-Jan-20 10:23am    
1.) From what I remember, spaces in the key value will be replaced automatically by an underscore. Therefore $n1=$_POST['First Name']; becomes $n1=$_POST['First_Name'];. But that does not explain the errors in line 5 and 6 :(

2.) Your code is dangerous. Do use parametrized queries instead of building the SQL directly with user input
Richard MacCutchan 18-Jan-20 10:46am    
The issue, as noted in lines 3,4,5 and 6, is that those index names have never been defined in the web page. Those messages do not refer to the error in the SQL, that is the last message.
[no name] 18-Jan-20 10:49am    
Yes I'm aware of this. But I think when that is solved the space in some key values will be the next problem?
Richard MacCutchan 18-Jan-20 10:52am    
You are correct, but note that @Email' and 'password' are also undefined.
Richard MacCutchan 18-Jan-20 10:45am    
There is something missing from the code you have shown above. Where are the items referred to in the $_POST array?

While the question was related to an error during execution, it's important to note that the current code has a major problem since it stores the passwords as plain text to the database.

The correct way to handle the passwords is to hash them and store the result into the database. This way the passwords won't be compromised even if data is leaked.

For an example, have a look at Password Storage: How to do it.[^]
 
Share this answer
 
Comments
Richard Deeming 21-Jan-20 11:42am    
Not only that, but it's vulnerable to SQL Injection[^] too.

Looks like a massive GDPR fine waiting to happen. :)
Wendelius 21-Jan-20 23:05pm    
Yep, and think about all the poor souls who loose their only password :~
$n1=$_POST['First_Name'];
$n2=$_POST['Second_Name'];
Check the
Quote:
$_POST
Array
 
Share this answer
 
Quote:
I don't know where is the error !

You need to learn reading the error messages!
You are told where
Notice: Undefined index: First Name in C:\xampp\htdocs\egx\code2.php on line 3

You are told why
Notice: Undefined index: First Name in C:\xampp\htdocs\egx\code2.php on line 3

You are told that 'First Name' does not exist in array $_POST
PHP
$n1=$_POST['First Name'];


In order to see what is going on when you rin your code, the only solution is Debugger.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900