Monday, 30 April 2018

sql - PHP adding information to database




I have a slight problem here with my PHP page. I have difficulties getting the filled in data in a database.



Let me show you the code of the index;




Naam:
Achternaam:



$nm = $_POST['naam;];
$anm = $_POST['achternaam'];
?>


Now I thought I had obtained the variabled and sent to the 'aanmelden.php' file.
The contents of the aanmelden.php file are:




$nm = $_GET['naam'];
$anm = $_GET['achternaam'];
$connect = mysql_connect('localhost', 'root', 'usbw');
mysql_select_db ('kantine');
$sql = "INSERT into kantine (naam, achternaam)
VALUES ('$nm', '$anm')";
$res = mysql_query($sql);
mysql_close ($connect); ?>



Looks all quite good to me, but when I press the submit button I get the following errors.




Notice: Undefined index: naam in I:\USBWebserver v8.6\root\aanmelden.php on line 2



Notice: Undefined index: achternaam in I:\USBWebserver
v8.6\root\aanmelden.php
on line 3





Please help me out if you can.



Regards,



Demiƫn


Answer



Since your form is configured to use method="post", you should be using the $_POST array rather than $_GET.



$name = $_POST['name'];

$lname = $_POST['lastname'];


See The Method Attribute.



Also, to avoid potential undefined indexes, I'd advise setting these variables with the ternary operator. That way, you can check for the existence of particular indexes before trying to access them.



$name = isset($_POST['name']) ? $_POST['name'] : false;
$lname = isset($_POST['lastname']) ? $_POST['lastname'] : false;



Edit



Other potential issues (or typos) with your code are:



// invalid, should be 
// semi-colon instead of a closing quote, should be $_POST['name']
$name = $_POST['name;];


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...