Undefined index php - php

My issue is that I am making a dating site form with following categories
firstname,lastname,username,password,email,mysex,yoursex,relationship,date of birth, and country
I did the whole php code so when submitted would send info to sql server. But there is some error on php half, date of birth(DOB) day,month,year. And mysex and yoursex values are not submitting. Sql gives undefined index. Here is my code.(The falf that is not submitting)
<form action="process.php" method="post" id="register" class="col-xs-12">
<input class="register-switch-input" type="radio" name="mysex" value="hombre" id="me-male" > <label class="register-switch-label" for="me-male"> Hombre </label>
<input class="register-switch-input" type="radio" name="mysex" value="mujer" id="me-female"> <label class="register-switch-label" for="me-female"> Mujer </label> <br>
<input type="radio" name="yoursex" value="hombre" id="your-male" checked> <label for="your-male"> Hombre </label>
<input type="radio" name="yoursex" value="mujer" id="your-female"> <label for="your-female"> Mujer </label>
<input type="radio" name="yoursex" value="cualquiera" id="cualquiera"> <label for="cualquiera">Cualquiera </label> <br>
<label class="form-label">Nacimiento:</label>
<select name="DOBMonth" >
<option> - Month - </option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
""
<select name="DOBDay" >
<option> - Day - </option>
""
<select name="DOBYear">
<option> - Year - </option>
<option value="2003">2003</option>
The "" is because the selects have a million options and are unnessecary.
Here is my php code
<?php
$servername = "localhost";
$username = "diego966";
$password = "ddddd966";
$dbname= "signup";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$mysex = $_POST["mysex"];
$yoursex = $_POST["yoursex"];
$relationship = $_POST['relationship'];
$DOBday = $_POST['DOBday'];
$DOBmonth = $_POST['DOBmonth'];
$DOByear = $_POST['DOByear'];
$country = $_POST['country'];
$sql="INSERT INTO accounts(firstname, lastname, username, password, email, mysex, yoursex, relationship, DOBday, DOBmonth, DOByear, country)
VALUES ('$firstname', '$lastname','$username', '$password', '$email','$mysex', '$yoursex', '$relationship', 'DOBday', 'DOBmonth ','DOByear', '$country')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

As partly mentioned in the comments:
DOBMonth is DOBmonth in PHP
DOBYear is DOByear in PHP
DOBDay is DOBday in PHP
your radio-field needs to be defined by name="yoursex[]" in HTML and then you check in PHP if isset($_POST["yoursex"]) and then you can use $_POST["yoursex"][0]
Sanitizing has been mentioned but can not be mentioned enough

Related

How do you trigger the next record in the PHP array via the HTML submit button?

Goal: I want to create an HTML form that displays pre-populated information from the 22 arrays from array_file.php.
First, I will go on index.php. On index.php, I will see a form with pre-populated data. I will not be able to edit the first and last name fields, but I will be able to edit the email field (if necessary).
Second, once everything looks okay, I will click the "Submit" button.
Third, if nothing is wrong (i.e., email field is populated), the "Submit" button should take me to the second record in the array.
Finally, once it has looped through all the arrays, it will provide a message, such as, "You're done!"
Current problem: My current index.php page shows all 22 pre-populated forms on one page. While I can edit and submit to the database using the individual "Submit" button, I'd rather be able to look at each pre-populated form one at a time.
Here is the code:
<?php
ob_start();
include 'array_file.php';
ob_end_clean();
?>
<?php
$i=1;
while ($i<=22){
?>
<form action="index.php" method="post">
<h2>Form</h2>
<label>First Name:</label>
<input class="input" name="first_name" type="text" value="<?php echo htmlentities($array[$i][1]) ?>" disabled><br>
<label>Last Name:</label>
<input class="input" name="last_name" type="text" value="<?php echo htmlentities($array[$i][2]) ?>" disabled><br>
<label>Email:</label>
<input class="input" name="email" type="text" value="<?php echo htmlentities($array[$i][3]) ?>"><br><br>
<input class="submit" name="submit" type="submit" value="Submit">
</form>
<?php
$i=$i+1;
}
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = mysqli_real_escape_string($conn,$_POST['email']);
if($email !=''){
//Insert Query of SQL
mysqli_query(#conn,"INSERT into form(form_first_name, form_last_name, form_email) values ('$first_name', '$last_name', '$email')");
echo "<br/><br/><span>Data inserted successfully!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some required fields are blank!</p>";
}
}
$mysqli->close(); // Closing Connection with Server
?>
Let me know if you need me to provide any more information. Thank you in advance!
I hope this code is what you need.
<?php
ob_start();
include 'array_file.php';
ob_end_clean();
if(isset($_POST['submit']) and isset($_POST[email])){
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = mysqli_real_escape_string($conn,$_POST['email']);
if($email !=''){
//Insert Query of SQL
mysqli_query(#conn,"INSERT into form(form_first_name, form_last_name, form_email) values ('$first_name', '$last_name', '$email')");
echo "<br/><br/><span>Data inserted successfully!</span>";
}
}
/// find which form will be published
if( isset($_SESSION["form"]) and $_SESSION["form"]<22){
$form=$_SESSION["form"]+1;
$_SESSION["form"]=$form;
}else{
$form=1;
$_SESSION["form"]=$form;
}
// determine which is the next form number
if($form<22){ $nextForm=$form+1; }else{ $nextForm="??"; }
<!-- form area !-->
<form action="index.php?form=<?php echo $nextForm; ?>" method="post">
<h2>Form</h2>
<label>First Name:</label>
<input class="input" name="first_name" type="text" value="<?php echo htmlentities($array[$form][1]) ?>" disabled><br>
<label>Last Name:</label>
<input class="input" name="last_name" type="text" value="<?php echo htmlentities($array[$form][2]) ?>" disabled><br>
<label>Email:</label>
<input class="input" name="email" type="text" value="<?php echo htmlentities($array[$form][3]) ?>"><br><br>
<input class="submit" name="submit" type="submit" value="Submit">
</form>

when i insert data in db i get num1 in all column

i try to creat a table with html and php
when i insert data into my db i get num 1 like a values in all column
this my code
<html dir="rtl">
<form action="" method="post">
<label for="Nom">الاسم:</label>
<center><input type="text" name="Nom"></center>
<label for="Cin">البطاقة الوطنية:</label>
<center><input type="text" name="Cin"></center>
<label for="Tel">الهاتف:</label>
<center> <input type="text" name="Tel"></center>
<label for="DATE_donation"> تاريخ التبرع:</label>
<center><input type="date" name="DATE_donation"></center>
<center><input type="submit" value="إدخال"></center>
</form>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ikhlas";
$con= mysqli_connect($servername, $username, $password, $dbname);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$Name =isset($_POST['Nom']);
$CIN = isset($_POST['Cin']);
$TEL = isset($_POST['Tel']);
$date = isset($_POST['DATE_donation']);
$sql="INSERT INTO persone(Nom, Cin, Tel, DATE_donation) value ('$Name','$CIN','$TEL','$date')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
?>
and this my ressult in dbenter image description here
Sure because you define all variable with $foo = isset($bar) instead of
if(isset($bar))
$foo = $bar;
Take a look to the doc about SQL injection too: http://php.net/manual/en/security.database.sql-injection.php
Remove your isset()
http://php.net/manual/en/function.isset.php
Replace to:
if(isset($_POST['إدخال']))
{
$Name = (!empty($_POST['Nom']))?$_POST['Nom']:"";
$CIN = (!empty($_POST['Cin']))?$_POST['Cin']:"";
$TEL = (!empty($_POST['Tel']))?$_POST['Tel']:"";
$date = (!empty($_POST['DATE_donation']))?$_POST['DATE_donation']:"";
}

Inserting to database on a single page form application

I've designed a form to insert data to a database on localhost.
<form action='' method='post'>
<input type='submit' name='CRUD' value='New Data'>
<br><br>
<input type='submit' name='CRUD' value='Retrieve Data'>
<br>
<hr>
</form>
<?php
error_reporting(0);
$x = $_POST['CRUD'];
if ($x == "New Data") {
require 'part1.php';
}
?>
I then made a form to insert the data on another file.
<form method='post'>
<label for='site'>Name: </label>
<input type='text' name='site'>
<br><br>
<label for='date'>Date: </label>
<input type='date' name='time'>
<br><br>
<label for='page'>Web URL: </label>
<input type='url' name='page'>
<br><br>
<label for='desc'>Description: </label>
<input type='text' name='desc'>
<br><br>
<input type='submit' name='finish' value='Go'><input type="reset">
</form>
<?php
if ( !empty( $_POST) ){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "assignment5";
$resource = $_POST['site'];
$date = $_POST['time'];
$url = $_POST['page'];
$explain = $_POST['desc'];
// Create connection
$conn = new mysqli('localhost', 'root', $password, $dbname) or
die("Unable to connect");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO thedata (date, Name, URL, Description)
VALUES ('$date', '$resource', '$url', '$explain')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$conn->close();
}
?>
On there own they work as intended but what I need is to have both forms on the same page. Doing this gives an error where default data is inserted and not the form's inputs.
If you want to put the 2 forms in the same page , you have to give each form a submit button .. be aware to use the same submit button to the same forms

PHP and html in the same html page

I have a php file "send.php":
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "RegisterDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$n=$_POST["name"];
$a=$_POST["age"];
$g=$_POST["gender"];
$gr=$_POST["graduate"];
$ad=$_POST["address"];
if($_SERVER["REQUEST_METHOD"]=="POST"){
$sql = "INSERT INTO RegTbl (name, age, gender,graduate,address)VALUES ('".$n."','".$a."','".$g."','".$gr."','".$ad."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
and a html file form.html
<html>
<body>
<form method="post" action="send.php">
Name <input type="text" name="name"/><br><br>
Age <input type="text" name="age"/><br><br>
Gender <input type="checkbox" name="gender" value="male"/> M <input type="checkbox" name="gender" value="female"/> F<br><br>
Graduate <input type="radio" name="graduate" value="yes"/> YES <input type="radio" name="graduate" value="no"/> NO<br><br>
Address <textarea name="address"></textarea><br><br>
<input type="submit" name="submit_button" value="Send"/>
</form>
</body>
</html>
And i am trying to embed both codes in a single html page:
<?php
if(isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "RegisterDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$n=$_POST["name"];
$a=$_POST["age"];
$g=$_POST["gender"];
$gr=$_POST["graduate"];
$ad=$_POST["address"];
if($_SERVER["REQUEST_METHOD"]=="POST"){
$sql = "INSERT INTO RegTbl (name, age, gender,graduate,address)VALUES ('".$n."','".$a."','".$g."','".$gr."','".$ad."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name <input type="text" name="name"/><br><br>
Age <input type="text" name="age"/><br><br>
Gender <input type="checkbox" name="gender" value="male"/> M <input type="checkbox" name="gender" value="female"/> F<br><br>
Graduate <input type="radio" name="graduate" value="yes"/> YES <input type="radio" name="graduate" value="no"/> NO<br><br>
Address <textarea name="address"></textarea><br><br>
<input type="submit" name="submit_button" value="Send"/>
</form>
</body>
</html>
But this is not working, can anybody help me to find the error? and i save this third file as test.html
You cannot execute php in an html file. Save the file with the .php extension.
If you put your php code in html and save the file as .html while running the html file the action will be required on a database which can be done only through php as php is server side and html is client side. So you need a server to run that which in your case is the localhost so you just save the file as .php and run it on the server so that your page can diretly interact with database.
Save it as test.php and run it. Because in html file php doesnt work

HTML Log In form connection to database

I am facing problems connecting my HTML form to database. I am very new at this. Please do help me.
This is the HTML Login form code
logout.html
<form name="form" onsubmit="submit1()" action="connectivity-sign-up.php" method="POST" >
<div id="errorBox"></div>
<input type="text" name="Name" value="" placeholder="First Name" class="input_name" >
<input type="text" name="LastName" value="" placeholder="Last Name" class="input_name" >
</div>
<div id="email_form">
<input type="text" name="Email" value="" placeholder="Your Email" class="input_email">
</div>
<div id="Re_email_form">
<input type="text" name="enterEmail" value="" placeholder="Re-enter Email" class="input_Re_email">
</div>
<div id="password_form">
<input type="password" name="Password" value="" placeholder="New Password" class="input_password">
</div>
<!--birthday details start-->
<div>
<h3 class="birthday_title">Birthday</h3>
</div>
<div>
<select name="birthday_month" >
<option value="" selected >Month</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
</select>
<select name="birthday_day" >
<option value="" selected>Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select name="birthday_year">
<option value="" selected>Year</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select>
</div>
<!--birthday details ends-->
<div id="radio_button">
<input type="radio" name="radiobutton" value="Female">
<label >Female</label>
<input type="radio" name="radiobutton" value="Male">
<label >Male</label>
</div>
<div>
<p id="sign_user" onClick="Submit()" value= "Submit" >Sign Up </p>
<input type="submit" value="Submit">
</div>
</form>
The Submit1() function is the validate function. When I click Submit it should first validate and then send the data to the form. Now the validate function works properly but how do I call it such that it will send the data once validated.And here is the PHP connectivity part
File name : connectivity-sign-up.php
<?php define('DB_HOST', 'localhost');
define('DB_NAME', 'customerdb');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
function Signin()
{
$fname = $_POST['Name'];
$lname = $_POST['LastName'];
$email = $_POST['Email'];
$password = $_POST['Password'];
$query = "INSERT INTO custtable (fname,lname,email,password) VALUES ('$fname','$lname','$email','$password')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
?>
When user click submit button call Signin() function.
if (isset($_POST['submit'])) {
Signin();
}
-
<?php define('DB_HOST', 'localhost');
define('DB_NAME', 'customerdb');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
if (isset($_POST['submit'])) {
Signin();
}
function Signin()
{
$fname = $_POST['Name'];
$lname = $_POST['LastName'];
$email = $_POST['Email'];
$password = $_POST['Password'];
$query = "INSERT INTO custtable (fname,lname,email,password) VALUES ('$fname','$lname','$email','$password')";
$data = mysql_query($query) or die(mysql_error());
if ($data) {
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
?>
Update html : <input type="submit" name="submit" value="Submit">
<!--birthday details ends-->
<div id="radio_button">
<input type="radio" name="radiobutton" value="Female">
<label>Female</label>
<input type="radio" name="radiobutton" value="Male">
<label>Male</label>
</div>
<div>
<p id="sign_user" onClick="Submit()" value="Submit">Sign Up </p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
When click Sign Up, the information will be send to connectivity-sign-up.php, and execute the code in connectivity-sign-up.php. And Signin() is not invoked, there is just a declare.
<?php define('DB_HOST', 'localhost');
define('DB_NAME', 'customerdb');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
function Signin()
{
$fname = $_POST['Name'];
$lname = $_POST['LastName'];
$email = $_POST['Email'];
$password = $_POST['Password'];
$query = "INSERT INTO custtable (fname,lname,email,password) VALUES ('$fname','$lname','$email','$password')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
//invoke Signin
Signin();
?>
If you remove the method declaration for Signin() and just have the entire page as a script, then the logic you have in the function will execute. This would result in the following file:
<?php define('DB_HOST', 'localhost');
define('DB_NAME', 'customerdb');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$fname = $_POST['Name'];
$lname = $_POST['LastName'];
$email = $_POST['Email'];
$password = $_POST['Password'];
$query = "INSERT INTO custtable (fname,lname,email,password) VALUES ('$fname','$lname','$email','$password')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
echo "YOUR REGISTRATION IS COMPLETED...";
}
?>
Another alternative is to actually call the function somewhere in the page like so:
Signin()
The better option would be executing the Signin() function somewhere in the page.And you would need to add a connetion between the connectivity-sign-up.php and logout.html file

Categories