Information does not go into my table - php

I am having trouble with a some php code. I was able to get the code to work on my test website. I am now trying to register a user with a popup register menu. When I enter the information and click on my register button nothing is sent to my database table.
Heres my code.
<!doctype html>
<html lang="en">
<head>
<title>untitled.com</title>
<meta name="description" content="">
<link rel="stylesheet" type="text/css" href="frontPage.css">
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script src="js/general.js" type="text/javascript">
</script>
</head>
<body>
<h1> untitled </h1>
<h2> description of website</h2>
<div id="wrapper">
<nav id="nav">
<ul id="navigation">
<a class="button" href="" >Login</a>
<div class="popup">
CLOSE
<form>
<P><span class="title">Username</span> <input name="" type="text" /></P>
<P><span class="title">Password</span> <input name="" type="password" /></P>
<P><input name="" type="button" value="Login" /></P>
</form>
</div>
</ul>
</nav>
<?php
require('db.php');
if (isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];;
$query = "INSERT into `users` (username, password, email, city, state, zip, phone) VALUES ('$username', '".md5($password)."', '$email', '$city', '$state', '$zip', '$phone' )";
$result = mysql_query($query);
if($result){
echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<a class="Regbutton" href="#" >Register</a>
<div class="Regpopup">
CLOSE
<form>
<P><span class="title">Username</span> <input name="username" type="text" /></P>
<P><span class="title">Password</span> <input name="password" type="password" /></P>
<P><span class="title">Email</span> <input name="email" type="email" /></P>
<P><span class="title">City</span> <input name="city" type="text" /></P>
<P><span class="title">State</span> <input name="state" type="text" maxlength="2" /></P>
<P><span class="title">zip</span> <input name="zip" type="text" maxlength="5" /></P>
<P><span class="title">phone</span> <input name="phone" type="text" maxlength="15"/></P>
<P><input type="submit" name="submit" value="Register" /></P>
</form>
</div>
<?php } ?>
</body>
</html>

To get the value from input field by post method, it is important to define form method. Example
<form action="" method="POST">
</form>

Related

Insert value of multiple checkbox in sqlite database using php pdo

Hi I have multiple check boxes name make and female I want that if a person select make and don't select female it input nothing or false in database now if I don't select example: female it return nothing in female row but it also get a error
Undefined index: Female in /storage/sdcard1/www/3/signup/index.php on line 11
but I don't want this error and I want the if female not select it insert false and don't show error I don't want the selected check box value to be true because it is wrote as on in database
Code index.php
<?php
if(isset($_POST['submit'])){
$conn = new PDO("sqlite: sign.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$Email = $_POST['Email'];
$First = $_POST['First'];
$Last = $_POST['Last'];
$Password = $_POST['Password'];
$Male = $_POST['Male'];
$Female =$_POST['Female'];
$Dateofb = $_POST['Dateofb'];
require_once 'imp.php';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css" >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post" >
<div class="container" >
<div class="form" >
<input type="email" class="first" id="Email" name="Email" placeholder="Email" required="required">
<input type="text" class="second" id="First" name="First" placeholder="First name" required="required">
<input type="text" class="last" id="Last" name="Last" placeholder="Last name" required="required">
<input type="password" class="pass" name="Password" id="Password" placeholder="Password" required="required">
<div class="day" >
<p class="bd" >Birthday Date:</p>
<input type="date" class="date" id="Dateofb" name="Dateofb" >
</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="Male" name="Male">
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="Female" name="Female" >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit" name="submit" >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
Code imp.php
<?php
$sql = "INSERT INTO signup (`Email`, `First`, `Last`, `Password`, `Male`, `Female`, `Dateofb`) VALUES ('$Email', '$First', '$Last', '$Password', '$Male', '$Female', '$Dateofb');";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn->connection = null;
?>
The problem is that only checked boxes are submitted.
You need to check if a box's name is set in $_POST and IF NOT:
manually assign a value you wish to use instead
I.e. use this short hand if/else
$Female = (isset ($_POST['Female'])) ? 1 : 0;

HTML Form + PHP + MYSQL 500 Error

I am receiving an internal 500 error when trying to insert data from HTML form into MySQL database via PHP. I am hosting a site with an Apache 2 webserver on an Ubuntu server (droplet using DigitalOcean). PHP and MySQL are both installed on the server. HTML below and PHP below. I have tested with a PHP echo with the variable $name, which was successful, but I am thrown the error whenever I try to connect to mysql. I know 500 is an internal error, but am not sure where to go from here.
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>who this</title>
<link rel="stylesheet" type="text/css" href="STYLE.css">
</head>
<body>
<h1 id="header2">me</ h1>
<div id="back">
back
</div>
<div id="me-intro">
<p>tell me about yourself</p>
</div>
<div id="joke">
<form action="process.php" method="POST">
<div id="one">
<p>name: <input type="text" name="name" id='first'></p>
</div>
<div id="two">
<p>gender: <input type="text" name="gender" id='second'></p>
</div>
<div id="three">
<p>age: <input type="text" name = "age" id='third'></p>
</div>
<div id="four">
<p>occupation: <input type="text" name="occupation" id='fourth'></p>
</div>
<div id="five">
<p>education: <input type="text" name="education" id='fifth'></p>
</div>
<div id="six">
<p>car you drive: <input type="text" name="car" id='sixth'></p>
</div>
<div id="seven">
<p>phone number: <input type="text" name="phone" id="seventh"></p>
</div>
<div id="eight">
<p>address: <input type="text" name="address" id="eighth"></p>
</div>
<div id="nine">
<p>social security number: <input type="text" name="ssn" id="ninth">
</div>
<div id="ten">
<input type="submit">
</div>
</form>
</div>
</body>
</html>
process.php below
<?php
$conn = new mysqli("localhost", "root", "password_here", "database_here");
$name = $_POST["name"];
$gender = $_POST["gender"];
$age = $_POST["age"];
$occupation = $_POST["occupation"];
$education = $_POST["education"];
$car = $_POST["car"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$ssn = $_POST["ssn"];
if($conn -> connect_error) {
die("err: dis not working: " . $conn->connect_error);
}
$sql = "INSERT INTO table_here (name, gender, age, occupation, education, car, phone, address, ssn) VALUES ('$name', '$gender', '$age', '$occupation', '$education', '$car', '$phone', '$address', '$ssn')";
if($conn->query($sql) === TRUE){
echo "success";
} else {
echo "error: did not submit form to mysql";
}
$conn->close();
?>

Form Data will not parse into database

I have the following form asking for simple information, yet the data will not parse to my database. I have similar forms on other parts of my site, and they are connected to the database and retrieving data perfectly. Have pasted the code below and believe that MYSQL script is written as it is supposed to be. Why is the data not being retrieved once a use presses the submit button?
<?php
// Written by KV Ghumaan
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
?>
<?php
// Parse the form data and add inventory item to the system
if(isset($_POST['fName'])){
$id = mysql_real_escape_string($_POST['id']);
$fName = mysql_real_escape_string($_POST['fName']);
$lName = mysql_real_escape_string($_POST['lName']);
$address = mysql_real_escape_string($_POST['address']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zipCode = mysql_real_escape_string($_POST['zipCode']);
//Add this product into the database now
$sql = mysql_query("INSERT INTO checkoutInfo (id, fName, lName, address, city, state, zipCode)
VALUES('$id','$fName','$lName','$address','$city','$state','$zipCode')") or die (mysql_error());
$pid = mysql_insert_id();
}
?>
<html>
<head>
<!--JQUERY-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="jqueryui/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="jqueryui/jquery-ui.css"/>
<link rel="stylesheet" type="text/css" href="jqueryui/jquery-ui.structure.css"/>
<link rel="stylesheet" type="text/css" href="jqueryui/jquery-ui.theme.css"/>
<!--GOOGLE FONT & EXTERNAL LIBS-->
<link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style/style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CHECKOUT</title>
</head>
<body>
<?php include_once("template_header.php");?>
<table width="100%" height="76vh" border="10%" cellspacing="10" style="padding: 2vh 10vw 0vh 10vw; border-style:none;">
<tr height="75%" cellspacing="0" style="border-style:none;">
<td width="30%" style="border-style:none; padding-top: 10vh;">
<form class="form" action="confirmation.php" enctype="multipart/form-data" name="checkoutForm" id="checkoutForm" method="POST">
<div id="billingQuestions">
<h3><strong>BILLING ADDRESS:</strong></h3>
<div id="formQuestions">
<label>First Name:*</label><br />
<input type="text" name="fName" size="40vw" required>
</div>
<div id="formQuestions">
<label>Last Name:*</label><br />
<input type="text" name="lName" size="40vw" required>
</div>
<div id="formQuestions">
<label>Address 1:*</label><br />
<input type="text" name="address" size="40vw" required>
</div>
<div id="formQuestions">
<label>Address 2:*</label><br />
<input type="text" size="40vw">
</div>
<div id="formQuestions">
<label>City: *</label><br />
<input name="city" type="text" size="40vw" required>
</div>
<div id="formQuestions">
<label>State: *</label><br />
<input name="state" type="text" size="40vw" required>
</div>
<div id="formQuestions">
<label>Zip Code: *</label><br />
<input name="zipCode" type="text" size="40vw" required>
</div>
<input type="submit" value="Submit"/>
</div>
</form>
</td>
</tr>
</table>
<?php include_once("template_footer.php");?>
</body>
</html>

registration is successful but there is no data in data base

when I insert data using the code I got the message that you are registered successfully i.e. data is inserted into table but I am unable to find data in my local database.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
if (isset($_REQUEST['username'])) {
$username = "username";
$email = "email";
$password = "password";
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
$result = mysqli_query($con, $query);
if($result) {
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
} else {
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php }
?>
</body>
</html>
You have to learn more about php before you can ask.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
if (isset($_POST['username'])){
$username= $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$trn_date = date("Y-m-d H:i:s");
$query = "INSERT into `users` (username, password, email, trn_date)
VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
$result = mysqli_query($con,$query);
if($result){
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Registration</h1>
<form name="registration" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php } ?>
</body>
</html>
Several issues:
The action tag in the <form> is empty. It works but you should specify the script name.
You assign to $username, $email and $password the wrong values. They should receive the value from $_REQUEST.
mysqli_query returns TRUE when there was an error. Therefore you get the message displayed it was successful.
Check the MySQL error and then you have the solution for your problem.

Information is not saved in mysql database after submitting in form

This is the code for saving the information into mysql database from a FORM.
In the HTML section the form is being handled i.e. retrieving required data from user.
In the PHP section storing data has been handled.
But the problem is it doesn't store data.
I'm using XAMPP server.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="signup.php" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>
I don't understand what can be the problem.
Thanks all. I got the problem.
Actually the sequence of the column in my database was not matching with the query in php code.
I have solved this by changing the variable sequence in the query which is maintained in the database.
$query = mysql_query("INSERT INTO user (`name`, `mail`, `pass`, `address`, `phone`)VALUES('".$name."', '".$mail."', '".$pass."', '".$address."', '".$phone."')");
Here is the code and it will work for your..
I have passed connection link in your mysql_query. and used PHP_SELF for current page.
<html>
<head>
<title>signup</title>
<link rel="stylesheet" href="css/insert.css" />
</head>
<body>
<div class="maindiv">
<!--HTML form -->
<div class="form_div">
<div class="title"><h2>Insert Data In Database Using PHP.</h2> </div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- method can be set POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<br />
<input class="input" type="text" name="name" value="" />
<br />
<label>Email:</label><br />
<input class="input" type="text" name="mail" value="" />
<br />
<label>Phone:</label><br />
<input class="input" type="text" name="phone" value="" />
<br />
<label>Password:</label><br />
<input class="input" type="text" name="pass" value="" />
<br />
<label>Address:</label><br />
<textarea rows="5" cols="25" name="add"></textarea>
<br />
<input class="submit" type="submit" name="submit" value="Insert" />
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "root", "buet2010");
//Selecting Database from Server
$db = mysql_select_db("tanni", $connection);
if(isset($_POST['submit'])){
//Fetching variables of the form which travels in URL
$name = $_POST['name'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$pass = $_POST['pass'];
$add = $_POST['add'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query($db, "INSERT INTO user (name, mail, phone, pass, add)VALUES('$name', '$mail', '$phone', '$pass', '$add')",$connection);
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
//Closing Connection with Server
mysql_close($connection);
?>
</form>
</div>
</div>
</body>

Categories