Forum Moderators: coopster

Message Too Old, No Replies

INSERT INTO using $ GET

         

SimonH24

10:52 pm on Dec 12, 2014 (gmt 0)

10+ Year Member



I have this code and it is giving me an undefined index for each one can anyone please help me, it's for a college project

PHP SCRIPT:
<?php
require ("db_conn.php");
$con=mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$FirstName = $_GET['firstname'];
$SurName = $_GET['surname'];
$Club = $_GET['club'];
$Nationality = $_GET['nationality'];
$DOB = $_GET['dob'];
$WorkVisa = $_GET['workvisa'];

$sql= "INSERT into Player(FirstName, SurName, Club, Nationality, DOB, WorkVisa) values ('$FirstName', '$SurName', '$Club', '$Nationality', '$DOB', '$WorkVisa')";
mysqli_query($con,$sql);
echo "row added";

mysqli_close($con);

?>

HTML FORM:
<html>
<body>
<form action="processForm.php" method="get">
Enter Player FirstName:<input type="text" name="firstname"/>
<br/>
Enter Player SurName:<input type="text" name="surname"/>
<br/>
Enter Current Club:<input type="text" name="club"/>
<br/>
Enter Player Nationality:<input type="text" name="nationality"/>
<br/>
Enter Player DOB:<input type="text" name="dob"/>
<br/>
Enter Yes/No for WorkVisa:<input type="text" name="workvisa"/>
<br/>
<input type="submit"/>
</form>
</body>
</html>

phranque

11:09 pm on Dec 12, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, SimonH24!


do the urls for those GET requests include all those parameters?

SimonH24

11:46 pm on Dec 12, 2014 (gmt 0)

10+ Year Member



Yes i have a database set up on HeidiSQL and i'm trying to get this to work to be able to input all this info to add a new row to the table

penders

1:58 am on Dec 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



do the urls for those GET requests include all those parameters?


Yes...


Well, assuming your PHP SCRIPT is "processForm.php" then, from what you have posted, you would only get "undefined index" notices if the URLs do not include all those parameters. So, something is amiss.

When you check URL parameters (ie. from the $_GET superglobal) you need to check for it's existence first, to avoid such notices... for example:
$FirstName = isset($_GET['firstname']) ? $_GET['firstname'] : null;


However, this is not going to solve your problem, if you are expecting these values to be there. (You still need to abort your script.)

(A bit of an aside, but... unless your script has some authorisation/strong validation that we aren't seeing then it would be a huge security vulnerability in the real world.)

lucy24

2:14 am on Dec 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it's for a college project

I really, really hope that isn't a synonym for "someone please do my homework for me" ;)

For testing purposes you could throw in a bunch of "echo" lines that spit out the values of assorted variables. Then you can pin down exactly what is happening or failing to happen.

:: detour to refresh memory on difference between GET and POST as applied to the submission method of an html form ::

Hm, kinda late in the semester for GET isn't it?