Value doesn't get saved in database - php

Hi there so i have my code running and actually works pretty ok, i don't have any error, but what makes me come here it's that one of the parts to fill up in my formulary "nombre" doesn't appear in the database row but my other info "edad" it does, what i'm doing bad? here is my code
<?php
include("conectar.php");
if(isset($_REQUEST['nombre'])){
$n= $_REQUEST['nombre'];
}
if(isset($_REQUEST['edad'])){
$e= $_REQUEST['edad'];
mysqli_query($con,"INSERT INTO team VALUES (NULL,'$n','$e')");
echo "<script> alert('Se inserto registro');</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base de datos</title>
</head>
<body>
<h1>Gestion de Base de Datos</h1><hr>
<form action="index.php" method="post">
<input type="text" name="nombre" placeholder="Introduce nombre" required><br>
</form>
<form action="index.php" method="post">
<input type="text" name="edad" placeholder="Introduce edad" required><br>
<input type="submit" value="insertar">
</form>
here is a photo of what happens when i insert this 2 required texts
http://postimg.org/image/9etdzqloz/

You missed a } after $e=... . So $n was unknown.
The working code:
<?php
include("conectar.php");
if(isset($_REQUEST['nombre'])){
$n= $_REQUEST['nombre'];
}
if(isset($_REQUEST['edad'])){
$e= $_REQUEST['edad'];
} /* <== here */
mysqli_query($con,"INSERT INTO team VALUES (NULL,'$n','$e')");
echo "<script> alert('Se inserto registro');</script>";
//} not here ;)
?>
EDIT
The formis wrong too :
<form action="index.php" method="post">
<input type="text" name="nombre" placeholder="Introduce nombre" required><br>
<!-- DELETE THIS :
</form>
<form action="index.php" method="post">
-->
<input type="text" name="edad" placeholder="Introduce edad" required><br>
<input type="submit" value="insertar">
</form>

why form tag is two times. try putting on same tag
<form action="index.php" method="post">
<input type="text" name="nombre" placeholder="Introduce nombre" required><br>
<input type="text" name="edad" placeholder="Introduce edad" required><br>
<input type="submit" value="insertar">
</form>

Related

php form handling input value post self some characters error

i post data into form action to php self some characters make errors. This charactor are " or \ if u put " input name return into \ how to fix it
<form id="form1" name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<label for="name"></label>
<input name="name" type="text" id="name" value='<? print htmlspecialchars($_POST['name']); ?>' />
<br />
<input type="submit" name="button" id="button" value="Submit" />
<? print htmlspecialchars($_POST['name']); ?>
</form>
Im not exactly sure what the problem is but I think you are getting errors when returning special characters into an input field. If so try this: http://php.net/manual/en/function.htmlentities.php you will need to convert the special characters into html entities so the data isnt read as code which will cause issues.
I have just tested this by changing the message input to <?php echo htmlentities($_POST['name']); ?> and the characters are displaying as normal.
Use this code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name"></label>
<input name="name" type="text" id="name" value="<?php echo htmlentities($_POST['name']); ?>" />
<br />
<label for="message"></label>
<textarea name="message" id="message" cols="45" rows="5"><?php echo $message; ?></textarea>
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
echoed (<?php echo htmlentities($_POST['name']); ?>);
</body>
</html>

POST not working with no signs of error

haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...

Notice: Undefined index: username in C:\xampp\htdocs\Registration\scripts\validate.php on line 6 [duplicate]

This question already has answers here:
How to get input field value using PHP
(7 answers)
Closed 8 years ago.
I am trying to register a new user by posting their form data to the database via a php scriptregister.php but I am getting an array of errors when I hit register, the data is supposed to be validated by a second script called validate.php. My register.php is shown below. Same errors exist when the form is empty and when is filled.
<?php require('scripts/validate.php');?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Register</title>
<body>
<div id="mainWrapper">
<div id="register">
<?php if(isset($error)){echo "<div id='error'>".$error."</div>";}?>
<?php if(isset($success)){echo "<div id='success'>".$success."</div>";}?>
<form method="post" action="" >
<fieldset>
<legend>Register Here</legend>
<p>
<label for="Username">Username</label>
<input type="text" id="username"/>
</p>
<p>
<label for="Email">Email</label>
<input type="text" id="email"/>
<p>
<label for="Firstname">Firstname</label>
<input type="text" id="firstname"/>
</p>
<p>
<label for="Lastname">Lastname</label>
<input type="text" id="lastname"/>
</p>
<p>
<label for="Password">Password</label>
<input type="password" id="password"/>
</p>
<p>
<label for="Re-Type Password">Re-Type Password</label>
<input type="password" id="password2"/>
</p>
<p>
<label for="DOB">DOB</label>
<input type="text" id="dob">
</p>
<p>
<label for="Adress">Adress</label>
<input type="text" id="address"/>
</p>
<p>
<label for="Adress 2">Adress 2</label>
<input type="text" id="address2"/>
</p>
<p>
<label for="town">Town</label>
<input type="text" id="town"/>
</p>
<p>
<label for="county">County</label>
<input type="text" id="county"/>
</p>
<p>
<label for="Postalcode">PostalCode</label>
<input type="text" id="postcode"/>
</p>
<p>
<label for="contactno">Contact No.</label>
<input type="text" id="contact"/>
</p>
<input type="submit" name="submit" value="Register"/>
</fieldset>
</form>
</div>
</div>
</body>
</html>
And the validate.php is here
<?php include('connection.php');?>
<?php
if(isset($_POST['submit']))
{
$username=$_POST['username'];
$email=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$dob=$_POST['dob'];
$address=$_POST['address'];
$address2=$_POST['address2'];
$town=$_POST['town'];
$county=$_POST['county'];
$postcode=$_POST['postcode'];
$contact=$_POST['contact'];
$fetch=mysql_query("SELECT id FROM users WHERE email='$email'")or die(mysql_error());
$num_rows=mysql_num_rows($fetch);
if(empty($username)||empty($email) || empty($firstname) || empty($lastname) || empty($password) || empty($password2) || empty($dob) || empty($address) || empty($town)|| empty($postcode) || empty($contact))
{
$error= 'ALl * fields are required';
}
elseif (!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$error= 'A valid email is required';
}
elseif (!empty($contact))
{
if (!is_numeric($contact))
{
$error= 'Enter a valid contact No.';
}
}
elseif ($password !=$password2)
{
$error= "Passwords don't match";
}
elseif ($num_rows >=1)
{
$error='We already have this email registered,try a new one!';
}
else
{
$password=md5($password);
$sql=mysql_query("INSERT INTO user(username,email,firstname,lastname,password,dob,address,address2,town,county,postcode,contact)VALUES('$username','$email','$firstname','$lastname','$password','$dob','$address','$address2','$town','$county','$postcode','$contact')");
if($sql)
{
header("location:login.php");
}
}
}
?>
I'll greatly appreciate guys.
Give your inputs a name property.
E.g:
<input type="text" id="username" name="username" />
The issue is that it's looking for name, but it doesn't exist.
This goes for all of your input fields, not just that specific one and not just for type="text".
When you POST data using a form, you need to specify the name of the data using the name attribute. Replace all of the id attributes with name and your form will work. For example:
<input type="text" id="username"/>
should become:
<input type="text" name="username"/>
Replace all elements' id with name. When form is submitted the element's information is submitted with associated name and not id.
e.g. <input type="text" id="username" name="username"/>
This isn't a PHP error, it's a HTML one.
POSTS variables are stated using the name tag in HTML, not the id tag.
Your inputs should look like this:
<input type="text" name="username"/>
Just change the id tags to name tags, and it should work.
Change your HTML Form
<?php require('scripts/validate.php');?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Register</title>
<body>
<div id="mainWrapper">
<div id="register">
<?php if(isset($error)){echo "<div id='error'>".$error."</div>";}?>
<?php if(isset($success)){echo "<div id='success'>".$success."</div>";}?>
<form method="post" action="scripts/validate.php" >
<fieldset>
<legend>Register Here</legend>
<p>
<label for="Username">Username</label>
<input type="text" id="username" name="username"/>
</p>
<p>
<label for="Email">Email</label>
<input type="text" id="email" name="email"/>
<p>
<label for="Firstname">Firstname</label>
<input type="text" id="firstname" name="firstname"/>
</p>
<p>
<label for="Lastname">Lastname</label>
<input type="text" id="lastname" name="lastname"/>
</p>
<p>
<label for="Password">Password</label>
<input type="password" id="password" name="password"/>
</p>
<p>
<label for="Re-Type Password">Re-Type Password</label>
<input type="password" id="password2" name="password2"/>
</p>
<p>
<label for="DOB">DOB</label>
<input type="text" id="dob" name="dob">
</p>
<p>
<label for="Adress">Adress</label>
<input type="text" id="address" name="address"/>
</p>
<p>
<label for="Adress 2">Adress 2</label>
<input type="text" id="address2" name="address2"/>
</p>
<p>
<label for="town">Town</label>
<input type="text" id="town" name="town"/>
</p>
<p>
<label for="county">County</label>
<input type="text" id="county" name="county"/>
</p>
<p>
<label for="Postalcode">PostalCode</label>
<input type="text" id="postcode" name="postcode"/>
</p>
<p>
<label for="contactno">Contact No.</label>
<input type="text" id="contact" name="contact"/>
</p>
<input type="submit" name="submit" value="Register"/>
</fieldset>
</form>
</div>
</div>
</body>
</html>
as others have stated here, your form elements need a name attribute. the id attribute is great for referencing the form elements in JavaScript or CSS. but with $_POST variables you need to specify the name. Hope this gives an understanding to why you need the name attribute instead of the id attribute.

HTML form PHP post not working

I'm writing a little website for myself and I'm having trouble with what seems like a simple line of code:
form action="send.php" method="post"
Furthermore, even a simple line like form action="http://www.google.com"> isn't working. Here is my code:
<html>
<head>
<title>
AnonMail!
</title>
</head>
<body style="font-family:'Tahoma';>
<form action="send.php" method="post">
<label for="from">From: </label><input type="text" name="from" id="from"></input></br>
<label for="to">To: </label><input type="text" name="to" id="to"></input></br>
<label for="subj">Subject: </label><input type="text" name="subj" id="subj"></input></br>
<textarea rows=10 cols=100 name="message"></textarea></br>
<input type="submit" value="Send"/>
</form>
</body>
</html>
The error starts with your body tag.
You have not closed your double quotes in the style tag of your body.
Just close it properly and then run it.
I think that is only the problem.
Here's a form that should work:
<html>
<body>
<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p><input type="submit" value="Send it!"></p>
</form>
</body>
</html>
If you're still having troubles: http://myphpform.com/php-form-not-working.php
browsers show warnings when your action refers to an external source. Some browsers do not post form at all. This is unsafe for users.

Get response from form POST

I've looked all over the net for probably a common and simple task and have found nothing but deadends. I'm trying to grab a response from my own html page that uses POST to submit data to a website so I can parse it and show/print the parsed text on the same html page.
Here's what my html page looks like:
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://somesite.com"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
Just do <?php echo $_POST['username']; ?>
use the Predefined Variable $_POST
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action=""
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
<?php if("Send" == $_POST['submit']){ var_dump($_POST); } ?>
</head><body></body></html>
Would recommend that you read on http://www.php.net/form as it contains both examples and good comments. As your calling the fields username and password I guess you might also be looking at database connections, be very careful of SQL injections (http://php.net/manual/en/security.database.sql-injection.php).

Categories