Undefiend index - php

When I send my form to another page it says: Notice: Undefined index: gebruikersnaamRegistreren in on line 55
this for every name of the inputs
HTML Code
<form action="RegistrerenSucces.php" method="POST">
<fieldset id="inputs">
<input id="gebruikersnaam" type="text" name="gebruikersnaamRegistreren" placeholder="Gebruikersnaam " required />
<input id="paswoord" type="password" name="paswoordRegistreren" placeholder="Paswoord" required />
<input id="voornaam" type="text" name="voornaamRegistreren" placeholder="Voornaam" required />
<input id="achternaam" type="text" name="achternaamRegistreren" placeholder="Achternaam" required />
<input id="email" type="email" name="emailRegistreren" placeholder="E-mail adres" required />
<input id="straat" type="text" name="straatRegistreren" placeholder="Straatnaam en huisnummer" required />
<input id="postcode" type="text" name="postcodeRegistreren" placeholder="Postcode" required />
<input id="gemeente" type="text" name="gemeenteRegistreren" placeholder="Gemeente" required />
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Registreren" />
</fieldset>
</form>
PHP Code
<?php
$dbhost='localhost';
$dbuser='root';
$dbpassword='usbw';
$dbdatabase='computingstore';
$paswoordEncrypted = hash('sha512',$_POST['paswoordRegistreren']);
print_r($_POST);
/*Database verbinden*/
$link = mysqli_connect($dbhost , $dbuser ,$dbpassword , $dbdatabase);
$query = ("INSERT INTO gebruiker (gebruikersnaam, paswoord, voornaam, achternaam, e-mail, straatnaam, postcode, gemeente)
VALUES
('".$_POST['gebruikersnaamRegistreren']."',
'".$paswoordEncrypted."',
'".$_POST['voornaamRegistreren']."',
'".$_POST['achternaamRegistreren']."',
'".$_POST['emailRegistreren']."',
'".$_POST['straatnaamRegistreren']."',
".$_POST['postcodeRegistreren'].",
'".$_POST['gemeenteRegistreren']."')");
mysqli_query($link,$query);
/*Verbing afsluiten*/
mysqli_close($link);
?>
How can i fix this error i have been looking for it for 2 days now.

Try to use isset to determine if the index is defined and assign an empty value if it is not, and see if the problem resolves.
For example, use the following in the second page:
isset($_POST['gebruikersnaamRegistreren']) ? $_POST['gebruikersnaamRegistreren'] : "";
instead of
$_POST['gebruikersnaamRegistreren']
repeat this for all the fields for which you are having problem .

Everything seems to work perfectly. Try to use prepare for your queries though. Nevertheless I recommend you to check for any javascript issues.

Related

$_POST variable contains no data

I'm having a basic form like this:
<form method="post" action="register.php" class="form">
<input id="a" type="text" placeholder="Cod acces" name="access-code" size="20" required /><br>
<input id="b" type="password" placeholder="Parola" name="password" autocomplete="new-password" size="20" required /><br>
<input id="c" type="password" placeholder="Confirma parola" name="re-password" autocomplete="new-password" size="20" required /> <br><br>
<input type="submit" value="Register" name="register" />
</form>
In register.php i have the following 3 lines of code:
$password = $_POST["password"];
$repassword = $_POST["re-password"];
$acces_code = $_POST["access-code"];
Even if this code is as simple as it looks, my $_POST variable is empty. Even weirder, if I press F12 to see the request data, all variables and it's values are there.
I'm using XAMPP on Windows.
Here is the code I'm also with Windows version 10 and Xampp Server.
In your index.php paste this code below
<html>
<body>
<form method="post" action="register.php" class="form">
<input id="a" type="text" placeholder="Cod acces" name="access-code" size="20" required /><br>
<input id="b" type="password" placeholder="Parola" name="password" autocomplete="new-password" size="20" required /><br>
<input id="c" type="password" placeholder="Confirma parola" name="re-password" autocomplete="new-password" size="20" required /> <br><br>
<input type="submit" value="Register" name="register" />
</form>
</body>
</html>
and then in your register.php paste this code below
<?php
if(isset($_POST['register'])){
echo $password = $_POST["password"]." ";
echo $repassword = $_POST["re-password"]." ";
echo $acces_code = $_POST["access-code"]." ";
}
Successfully getting and outputting your input on the form. Hope this will help you

Get the information from a form into a database

I'm trying to get the information from this form into a database but get an error as below
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
<form action= "" method= "POST">
<label for="title">Job title</label> <input type="text" name="title" /> <br>
<label for="refcode">Reference Code</label> <input type="text" name="refcode" /> <br>
<label for="salary">Salary</label> <input type="text" name="salary" /><br>
<label for="location">Location</label> <input type="text" name="location" /><br>
<label for="description"> Job description<textarea name="description" /> </textarea><br>
<input type="submit" value="Submit" name="submit" />
</form>
My PHP file
<?php
$stmt = $pdo->prepare('INSERT INTO jobs (title, refcode, salary, location, description)
VALUES(:title,:refcode,:salary,:location, :description) ');
$stmt-> execute($_POST);
?>
I think this is happen with the submit value of post variable, Didn't you try unset the element submit by post request.

Why are my form variables not passing through POST?

I am sending form data through POST, but the corresponding POST variables are not set, and do not.
Also, when I store POST data into local PHP variables, I seem to be unable to use those variables. (Once I resolve the first issue, I have a feeling I will be able to user the variables too.)
My error messages output by the second page (see below) is:
Notice: Undefined variable: postUsername in (...somepath)\scripts\create-member.php on line 10
(Form page) :
<form action="scripts/create-member.php" method="POST">
<input type="text" name"username" value="" placeholder="User Name"> <br />
<input type="password" name"password" value="" placeholder="Password"> <br />
<input type="password" name"passwordConfirm" value="" placeholder="Confirm Password"> <br />
<!-- ?type email or type text -->
<input type="email" name"email" value="" placeholder="Email" autofocus> <br />
<input type="submit" name="submitRegistration" value="Register!">
</form>
(Second page) scripts\create-member.php:
<?php
//!proper way to declare variables obtained from POST.
// Data from form "register.php"
if ( isset($_POST['username']) ) {
$postUsername = $_POST['username'];
}
echo $postUsername; // <-- this is line 10
?>
I've tried using isset() for the submit button too, but that didn't solve the problem.
I've simplified the code by a lot here, and ran it testing it too.
In your html code, you have missed = for name
name="username"
Instead of name"username"
Here's your fixed code.
<input type="text" name="username" value="" placeholder="User Name"> <br />
<input type="password" name="password" value="" placeholder="Password"> <br />
<input type="password" name="passwordConfirm" value="" placeholder="Confirm Password"> <br />
<!-- ?type email or type text -->
<input type="email" name="email" value="" placeholder="Email" autofocus> <br />
the ploblem is not at register.php
you can try to write like this:
if ( isset($_POST['username']) ) {
$postUsername = $_POST['username'];
echo $postUsername;
}
I had a similar problem posting to an index.php file in a folder.
<form id="register" name="register" method="POST" action="/register">
// BROKEN
all $_POST vars were empty until I added a trailing forward slash to the form post action
<form id="register" name="register" method="POST" action="/register/">
// WORKS

Mysql Empty Post Error

<form action="gonder.php" method="post">
<fieldset>
<label for="ad">Ad:</label>
<input type="text" id="ad" placeholder="Tam Adınızı Giriniz" />
<label for="soyad">Soyad:</label>
<input type="text" id="soyad" placeholder="Soyadınızı Giriniz" />
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Email Adresinizi Giriniz" />
<label for="mesaj">Mesaj:</label>
<textarea id="mesaj" placeholder="Mesajınız"></textarea>
<center></center>
<center><input type="submit" value="Onaya Gönder" /></center>
</fieldset>
</form>
This one is my post form and this one ise gonder.php
<?php
$con=mysqli_connect("localhost","root","","yeniziyaretci");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO mesajlar (ad, soyad, email, mesaj)
VALUES ('$_POST[ad]','$_POST[soyad]','$_POST[email]','$_POST[mesaj]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "";
mysqli_close($con);
header("location:index.php?cmd=1");
?>
When I tried to post. They dont give eny error but they send it to mysql as a empty . Like this.
Your input elements have no name attributes.
Also, please read up on the thing called 'sql injection'.
You must select the inputs by name not by id.
change
<input type="text" id="ad" placeholder="Tam Adınızı Giriniz" />
to
<input type="text" name="ad" id="ad" placeholder="Tam Adınızı Giriniz" />
You're using wrong and insecure way to query your database. To get the one you have working, just change your $sql declaration as follows:
$sql = sprintf('INSERT INTO mesajlar (ad, soyad, email, mesaj) VALUES ("%s", "%s", "%s", "%s")', $_POST['ad'], $_POST['soyad'], $_POST['email'], $_POST['mesaj']);
But I would suggest you to learn ways to avoid mysql injection.
EDIT
And yes, you're missing "name" attributes on your form.
Put this, it will work:
<form action="gonder.php" method="post">
<fieldset>
<label for="ad">Ad:</label>
<input type="text" id="ad" name=="ad" placeholder="Tam Adınızı Giriniz" />
<label for="soyad">Soyad:</label>
<input type="text" id="soyad" name="soyad" placeholder="Soyadınızı Giriniz" />
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Email Adresinizi Giriniz" />
<label for="mesaj">Mesaj:</label>
<textarea id="mesaj" name="mesaj" placeholder="Mesajınız"></textarea>
<center></center>
<center><input type="submit" value="Onaya Gönder" /></center>
</fieldset>
</form>
Either try
$sql="INSERT INTO mesajlar (ad, soyad, email, mesaj)
VALUES
('".$_POST[ad]."','".$_POST[soyad]."','".$_POST[email]."','".$_POST[mesaj]."')";
or
$sql="INSERT INTO mesajlar (ad, soyad, email, mesaj)
VALUES
('{$_POST[ad]}','{$_POST[soyad]}','{$_POST[email]}','{$_POST[mesaj]}')";
EDIT
Sorry I didn't notice but there is an issue in your html form too
you need to have name="" attributes into you form elements as suggested by Ø Hanky Panky Ø
<form action="gonder.php" method="post">
<fieldset>
<label for="ad">Ad:</label>
<input type="text" id="ad" name="ad" placeholder="Tam Adınızı Giriniz" />
</fieldset>
</form>

Undefined variable $submit

I'm getting an error which states:
Notice: Undefined variable: submit on line 4. I have no clue why since I have defined it with the submit button? Can someone please explain why? Don't really see the problem here.
<?php
if($submit)
{
$sql = "INSERT INTO personnel (first, last, username, department, email) VALUES ('$first','$last','$username','$department','$email')";
$add = mysql_query($sql);
echo "<div class='confirmation-box round'>Thank you! New user have been added</div>";
}
else
{
?>
<form method="post" action="useradd.php">
<fieldset>
<p>
<label for="simple-input">Firstname</label>
<input type="text" id="first" name="firstname" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Lastname</label>
<input type="text" id="last" name="lastname" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Username</label>
<input type="text" id="username" name="username" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Department</label>
<input type="text" id="department" name="department" class="round default-width-input" />
</p>
<p>
<label for="simple-input">Email</label>
<input type="text" id="email" name="email" class="round default-width-input" />
</p>
<input type="Submit" name="submit" class="button round blue image-right ic-add text-upper" value="Add">
</fieldset>
</form>
<?php
}
?>
(Sorry for stupid question, kinda new at php)
This is a typical old school case of register_globals usage. It decides whether or not register the EGPCS (Environment, GET, POST, Cookie, Server) variables as global variables. Nobody does that anymore.
Change it to this:
if (!empty($_POST['submit'])) {
I have no clue why since I have defined it with the submit button
No you did not.
The first instance of $submit which is if($submit) means:
If $submit is TRUE
... $submit has never been defined before.
You need to check post with following code
if(isset($_POST['submit']))
{
//Here will be your code
}
try this
And good practice to give proper name to submit button. Ex. 'cmdSubmit'
Best luck
Its a warning not an error.
You can just declare the varible with empty value or some default value.
you can use
if(isset($_POST['submit']))
{
//code goes here
}

Categories