Multi-fields form with image input - php

I have a form like this and I would like to know if there is a way to add an image input and upload it to the server.
I would like to be able to upload images to [ROOT]/upload_img/ but, to be honest I don't know how to do it and most of the code parts I found don't work with mine...
Se here I am.
Here's my code :
<html>
<head></head>
<body>
<div id="main">
<div id="login">
<form action="" method="post">
<label>Titre de l'annonce :</label>
<input type="text" name="i_title" id="name" required="required" placeholder=""/>
<br />
<br />
<label>Adresse : </label>
<input type="text" name="i_adress" id="email" required="required" placeholder=""/>
<br/>
<br />
<label>Ville :</label>
<input type="text" name="i_city" id="city" required="required" placeholder="Please Enter Your City"/>
<br/>
<br />
<label>Surface du logement entier :</label>
<input type="text" name="i_surf_room" id="surf_room" required="required" placeholder=""/> En m2
<br/>
<br />
<label>Surface de la chambre :</label>
<input type="text" name="i_surf_home" id="surf_home" required="required" placeholder=""/>
<br/>
<br />
<label>Description :</label>
<input type="text" name="i_description" id="description" required="required" placeholder=""/>
<br/>
<br />
<label>Date de début de disponibilité :</label>
<input type="month" name="i_start_date" id="start_date" required="required" placeholder=""/>
<br/>
<br />
<label>Date de fin de disponibilité :</label>
<input type="month" name="i_end_date" id="end_date" required="required" placeholder=""/>
<br/>
<br />
<label>Photographies du logement</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="reset"> -
<input type="submit" name="submit"/>
<br />
</form>
</div>
</div>
<?php
if(isset($_POST["submit"])) {
$hostname='xxxx';
$username='xxxx';
$password='xxx';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=xxxx",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO students (title, adress, city, surf_home, surf_room, description, start_date, end_date)
VALUES ('".$_POST["i_title"]."','".$_POST["i_adress"]."','".$_POST["i_city"]."','".$_POST["i_surf_home"]."','".$_POST["i_surf_room"]."','".$_POST["i_description"]."','".$_POST["i_start_date"]."','".$_POST["i_end_date"]."')";
if ($dbh->query($sql)) {
echo "
<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
} else {
echo "
<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
</body>
</html>

Related

get addition without button click

Can anyone help me to get the addition of Basic Salary and Allowance I and insert it in Total Day Rate without clicking a button. here's my php code.
Here's a Screen Shot of my UI.
Code :
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("laboursalary", $connection);
if(isset($_POST['submit'])){
$ID = $_POST['ID'];
$Name = $_POST['Name'];
$Location = $_POST['Location'];
$Category = $_POST['Category'];
$LabourSupplier = $_POST['LabourSupplier'];
$Home = $_POST['Home'];
$Mobile = $_POST['Mobile'];
$BasicSalary = $_POST['BasicSalary'];
$Allowance1 = $_POST['Allowance1'];
$Allowance2 = $_POST['Allowance2'];
$DayRate = $_POST['$DayRate'];
$OTrate = $_POST['OTrate'];
if($ID !=''||$Name !=''){
$query = mysql_query("insert into attendance(ID, Name, Location, Category,LabourSupplier,Home,Mobile,BasicSalary,Allowance1,Allowance2,DayRate,OTrate) values ('$ID','$Name','$Location','$Category','$LabourSupplier','$Home','$Mobile','$BasicSalary','$Allowance1','$Allowance2','$DayRate','$OTrate')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
mysql_close($connection);
?>
After I enter the values for Basic Salary and Allowance 1, I want to get the addition of those two in Day Rate automatically.
this is my HTML code.
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" name="Location" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Telephone:
<input class="input" type="text" name="Home" value="" />
</fieldset>
<fieldset>Mobile:
<input class="input" type="text" name="Mobile" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="number" name="BasicSalary" value="" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" name="Allowance1" value="" />
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" value="" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" value="" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="number" name="OTrate" value="" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Insert</button>
<button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
</fieldset>
</form>
As I understood your question, your HTML code should be like,
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" name="Location" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Telephone:
<input class="input" type="text" name="Home" value="" />
</fieldset>
<fieldset>Mobile:
<input class="input" type="text" name="Mobile" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="number" name="BasicSalary" value="0" id="bassal" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" name="Allowance1" value="0" id="all1" />
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" value="0" id="all2" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" value="" id="DayRate" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="number" name="OTrate" value="" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Insert</button>
<button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
</fieldset>
</form>
<script >
$(document).ready(function (){
$('#bassal').on('input', function() {
$('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
});
$('#all1').on('input', function() {
$('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
});
$('#all2').on('input', function() {
$('#DayRate').val(parseInt($('#bassal').val()) + parseInt($("#all1").val()) + parseInt($("#all2").val()));
});
});
</script>
I've added jQuery, that does your work, and you don't need to do addition on PHP side.
This is how it would be done using a submit for post as these are all _POST values... Though to accomplish this without pressing a button would be JS/Angular/J Query/AJAX...
....
$BasicSalary = $_POST['BasicSalary'];
$Allowance1 = $_POST['Allowance1'];
$Allowance2 = $_POST['Allowance2'];
$DayRate = $_POST['$DayRate'];
$OTrate = $_POST['OTrate'];
//Set a new variable with the addition of the two `Basic Salary` and `Allowance 1`
//for the insertion into your column `dayRate`
$adustedDayRate = $BasicSalary + $Allowance1;
if($ID !=''||$Name !=''){
if($query != false){
$query = mysql_query("INSERT INTO `attendance` (ID, Name, Location, Category,LabourSupplier,Home,Mobile,BasicSalary,Allowance1,Allowance2,DayRate,OTrate) values ('$ID','$Name','$Location','$Category','$LabourSupplier','$Home','$Mobile','$BasicSalary','$Allowance1','$Allowance2','$adustedDayRate','$OTrate')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}esle{ $_SESSION['err'] = "ERROR: ".--- MySQL error handle here --- }
}
else{
echo "Some Fields are Blank....!!</p>";
}
}
mysql_close($connection);
Using Angular, you could place a placeholder element in your form input for `DayRate, then add the call back for the ng-model element for the two inputs you wish to add. Something like this here:
<div ng-app="">
<p>BasicSalary : <input type="number" ng-model="BasicSalary" placeholder="Basic Salary"></p>
<p>AllowanceI : <input type="number" ng-model="AllowanceI" placeholder="Allowance I"></p>
<p>DayRate : <input type="number" ng-model="DayRate" placeholder="{{BasicSalary -- AllowanceI}}"></p>
Your code would look like:
<fieldset> Basic Salary:
<input class="input" type="number" ng-model="BasicSalary" name="BasicSalary" value="" />
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" ng-model="Allowance1" name="Allowance1" value="" />
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" value="" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" placeholder="{{ BasicSalary -- Allowance1 }}" value="" />
</fieldset>
Here is a working fiddle of the angular method, simply add the angular library to your server using composer or hosted links, no additional JS is required with the Angular library elements. You can change the value of DayRate as the two other combining inputs are being input.
https://jsfiddle.net/qkpfb90o/1/
Hope this helps!
You can also try with this.
<form id="details" action="" method="POST">
<fieldset> ID:
<input class="input" type="text" name="ID" value="" />
</fieldset>
<fieldset> Name:
<input class="input" type="text" name="Name" value="" />
</fieldset>
<fieldset> Location:
<input class="input" type="text" name="Location" value="" />
</fieldset>
<fieldset> Category:
<input class="input" type="text" name="Category" value="" />
</fieldset>
<fieldset> Labour Supplier:
<input class="input" type="text" name="LabourSupplier" value="" />
</fieldset>
<fieldset> Telephone:
<input class="input" type="text" name="Home" value="" />
</fieldset>
<fieldset>Mobile:
<input class="input" type="text" name="Mobile" value="" />
</fieldset>
<fieldset> Basic Salary:
<input class="input" type="number" name="BasicSalary" value="" id="BasicSalary" onkeyup="doSum()"/>
</fieldset>
<fieldset> Allowance I:
<input class="input" type="number" name="Allowance1" value="" id="Allowance1" onkeyup="doSum()"/>
</fieldset>
<fieldset>Allowance II:
<input class="input" type="number" name="Allowance2" />
</fieldset>
<fieldset>Total Day Rate:
<input class="input" type="number" name="DayRate" value="" id="DayRate" />
</fieldset>
<fieldset>OT Rate:
<input class="input" type="number" name="OTrate" value="" />
</fieldset>
<fieldset>
<button name="submit" type="submit" id="submit">Insert</button>
<button onclick="goBack()" name="Back" type="back" id="details-back">Back</button>
</fieldset>
<script>
function doSum() {
var Allowance1 = isNaN(document.getElementById('Allowance1').value) ? document.getElementById('Allowance1').value : 0;
var BasicSalary = isNaN(document.getElementById('BasicSalary').value) ? document.getElementById('BasicSalary').value : 0;
var tot = parseInt(Allowance1) + parseInt(BasicSalary);
document.getElementById('DayRate').value = tot;
}
</script>

Application form with phpMyAdmin database

So now I'm trying to make a application form for a trial drive lesson. I'm new at PHP, so I tried to take some code from my registration form that is working fully.
But nothing happens when i click submit(aanvraag_btn)
Here is the code:
<form class ="myform" action="index.php" method="post" >
<h3 class="center"> Vraag een proefrit aan! </h3>
<br></br>
<input name="naam" type="text" class="inputvalues" placeholder="Naam" required />
<br></br>
<input name="email" type="text" class="inputvalues" placeholder="Email"/>
<br></br>
<input name="Telefoonnummer" type="text" class="inputvalues" placeholder="Telefoonnummer" required />
<br></br>
<input name="aanvraag" type="button" id="aanvraag_btn" value="Aanvragen" required/> <br>
This is the PHP:
<?php
if(isset($_POST['aanvraag']))
{
$naam = $_POST['naam'];
$email = $_POST['email'];
$telefoonnummer = $_POST['telefoonnummer'];
$query= "insert into user values('$naam','$email','telefoonnummer')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script type="text/javascript"> alert ("Bedankt voor de aanvraag, we nemen zo snel mogelijk contact op.") </script>';
}
else {
echo '<script type="text/javascript"> alert ("Error>';
}
}
?>
Use type="submit"
<input name="aanvraag" type="submit" id="aanvraag_btn" value="Aanvragen" required/>
Try with this:
<form class ="myform" action="index.php" method="post" >
<h3 class="center"> Vraag een proefrit aan! </h3>
<br></br>
<input name="naam" type="text" class="inputvalues" placeholder="Naam" required />
<br></br>
<input name="email" type="text" class="inputvalues" placeholder="Email"/>
<br></br>
<input name="Telefoonnummer" type="text" class="inputvalues" placeholder="Telefoonnummer" required />
<br></br>
<input name="aanvraag" type="submit" id="aanvraag_btn" value="Aanvragen"/> <br>
</form>
1)Missing form close
2)missing type ="submit"
3)typo in element attribute near name="telefoonnummer"
<?php
if(isset($_POST['aanvraag']))
{
$naam = $_POST['naam'];
$email = $_POST['email'];
$telefoonnummer = $_POST['telefoonnummer'];
$query= "insert into user values('$naam','$email','telefoonnummer')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script type="text/javascript"> alert ("Bedankt voor de aanvraag, we nemen zo snel mogelijk contact op.") </script>';
}
else {
echo '<script type="text/javascript"> alert ("Error");</script>';
}
}
?>
<form class ="myform" action="index.php" method="post" >
<h3 class="center"> Vraag een proefrit aan! </h3>
<br></br>
<input name="naam" type="text" class="inputvalues" placeholder="Naam" required />
<br></br>
<input name="email" type="text" class="inputvalues" placeholder="Email"/>
<br></br>
<input name="Telefoonnummer" type="text" class="inputvalues" placeholder="Telefoonnummer" required />
<br></br>
<input name="aanvraag" type="submit" id="aanvraag_btn" value="Aanvragen"/> <br>
</form>

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.

Cannot store radio button value in table

I am trying to save the value of the radio buttons in my database table choice. I get the message Data saved successfully but no value is stored in the table.
Form:
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<center<legend>Choose in which category you'd like to be included</legend></center>
<p><input type="radio" name="choice[]" value="player" id="player" class="custom" />
<label for="player">Player</label>
<input type="radio" name="choice[]" value="coach" id="coach" class="custom" />
<label for="coach">Coach</label>
<input type="radio" name="choice[]" value="supporter" id="supporter" class="custom" />
<label for="supporter">Supporter</label>
<input type="radio" name="choice[]" value="sponsor" id="sponsor" class="custom" />
<label for="sponsor">Sponsor</label>
<input type="radio" name="choice[]" value="alumni" id="alumni" class="custom" />
<label for="alumni">Alumni</label>
<input type="radio" name="choice[]" value="other" id="o" class="custom" />
<label for="o">Other</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
</body>
</html>
PHP:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$choice = $mysqli->real_escape_string($_POST['choice']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`,`choice`) VALUES ('".$name."','".$email."','".$phone."','".$other."','".$choice."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
var_dump($_POST) to sere what data flooding in.
One thing more to check if its save or submit ? in $_POST['save']
EDIT
After getting your full form - the error lies in your center tag
Change <center<legend> TO <center><legend>
The error - ↑ in this tag

Why the mysql code is not submitting values in database

This is the code for the form:
<div id="regform">
<div id="regform-top">
<h2>User Registration</h2>
<p>Please complete this form</p>
</div>
<form id="register-form" name="register-form" action="submit.php" method="post"
class="validation">
<fieldset>
<table>
<tr>
<td>
<div class="fieldgroup">
<label for="user-name">User name*: </label>
<input type="text" id="name" name="name" value="" size="12" class="inpt" /><br
class="clear" />
</div>
<div class="fieldgroup">
<label for="password">Password*: </label>
<input type="password" id="password" name="password" value="" size="12" class="inpt"
/><br class="clear" />
</div>
</td>
<td id="form-note">
<br />
<p><strong>Form Instructions</strong></p>
<p>*Required Field</p>
</td>
</tr>
</table>
<table id="bottom-reg">
<tr>
<td><br />
<div class="fieldgroup">
<label for="firstname">First name*</label>
<input type="text" id="firstname" name="firstname" value="" size="12" class="inpt" />
<br class="clear" />
</div>
<div class="fieldgroup">
<label for="email">Email Address* </label>
<input type="text" id="email" name="email" value="" size="12" class="inpt" /><br
class="clear" />
</div>
<div class="fieldgroup">
<label for="address1">Address 1* </label>
<input type="text" id="address1" name="address1" value="" size="12" class="inpt" />
<br class="clear" />
</div>
<label for="address2">Address 2</label>
<input type="text" id="address2" name="address2" value="" size="12" class="inpt" />
<br class="clear" />
<label for="address2">Address 3</label>
<input type="text" id="address3" name="address3" value="" size="12" class="inpt" />
<br class="clear" />
<div class="fieldgroup">
<label for="country">Country*</label>
<input type="text" id="country" name="country" value="" size="12" class="inpt" /><br
class="clear" />
</div>
</td>
<td><br />
<div class="fieldgroup">
<label for="lastname">Last name*</label>
<input type="text" id="lastname" name="lastname" value="" size="12" class="inpt" />
<br class="clear" />
</div>
<div class="fieldgroup">
<label for="group">Group* </label>
<input type="text" id="name" name="group" value="" size="12" class="inpt" />
<br class="clear" />
</div>
<div class="fieldgroup">
<label for="city">City* </label>
<input type="text" id="city" name="city" value="" size="12" class="inpt" /><br
class="clear" />
</div>
<div class="fieldgroup">
<label for="city">State* </label>
<input type="text" id="state" name="state" value="" size="12" class="inpt" /><br
class="clear" />
</div>
<div class="fieldgroup">
<label for="zip">Zip*</label>
<input type="text" id="zip" name="zip" value="" size="12" class="inpt" /><br
class="clear" />
</div>
</td>
</tr>
</table>
<input type="submit" value="Register" class="submit-btn" />
</fieldset>
</form>
This is the code for submit.php:
<?php
$con = mysql_connect("localhost","viatechp_invacar","storefront72");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("viatechp_invacare", $con);
$sql="INSERT INTO registration(username,password,fname,lname,group,
address1,address2,address3, email,city,state,zip,country )
VALUES
('$_POST[name]','$_POST[password]', '$_POST[firstname]','$_POST[lastname]','
$_POST[group]','$_POST[address1]','$_POST[address2]','$_POST[address3]',
'$_POST[email]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]'
)" ;
$query = mysql_query($sql) or die(mysql_error());
$results = mysql_fetch_assoc($query);
if ($results) {
echo 'The query returned ' . $results[ 'registration' ];
} else {
echo 'The query did not return any results';
} ?>
echo $sql;
?>
It is showing an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group,address1,address2,address3,email,city,state,zip,country ) VALUES ( 'sdfdsf' at line 1
group is an SQL keyword. If this is the name of one of your fields you must enclose it with ` like so:
`group`,`address1`,...
This tells mySQL that it is a field name and not the keyword. It would be good practice to enclose all your fields within ` to prevent any errors like this you may not have noticed.
Spot on by pburgess. GROUP, ORDER are some common field names that we use while programming. Make sure to enclose these using backticks group, always a good practice.
$sql="INSERT INTO registration(username,password,fname,lname,`group`,
address1,address2,address3, email,city,state,zip,country )
VALUES
('$_POST[name]','$_POST[password]', '$_POST[firstname]','$_POST[lastname]','
$_POST[group]','$_POST[address1]','$_POST[address2]','$_POST[address3]',
'$_POST[email]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]'
)" ;
Try this
$sql="INSERT INTO registration(`username`,`password`,`fname`,`lname`,`group`,
`address1`,`address2`,`address3`, `email`,`city`,`state`,`zip`,`country` )
VALUES
('$_POST[name]','$_POST[password]', '$_POST[firstname]','$_POST[lastname]',
'$_POST[group]','$_POST[address1]','$_POST[address2]','$_POST[address3]',
'$_POST[email]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]')" ;
Because you got some field names which is reserved keywords like for example you have group
Your $_POST values is missing quotes, Try updating them
$sql="INSERT INTO registration
(`username`,`password`,`fname`,`lname`,`group`,`address1`,`address2`,`address3`, `email`,`city`,`state`,`zip`,`country`)
VALUES
('$_POST[\"name\"]','$_POST[\"password\"]',$_POST[\"firstname\"]','$_POST[\"lastname\"]','
$_POST[\"group\"]','$_POST[\"address1\"]','$_POST[\"address2\"]','$_POST[\"address3\"]',
'$_POST[\"email\"]','$_POST[\"city\"]','$_POST[\"state\"]','$_POST[\"zip\"]','$_POST[\"country\"]'
)" ;
Well it is too late but this might help someone. Whenever you want to check why your query doesnt' work.. Always try to echo your query and paste it to the phpmyadmin and it will throw the mySQL error which are more easy to understand that what's the issue in query

Categories