I am trying to add data into 2 tables. the data arrives into my table called companies with no issues. however the data does not arrive into the table users. I do not get an error message and the page loads the admin page afterwards.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<title>Create a Company</title>
<link rel="stylesheet" type="text/css" href="/css.css"/>
</head>
<body>
<h2 class="header"> Create a Company </h2>
<form action="processcompany.php" method="post">
<input class="entry" placeholder="Account No" name="accountno" type="text"><br>
<input class="entry" placeholder="Company Name" name="companyname" type="text" required="required"><br>
<input class="entry" placeholder="GST/VAT/ABN/TAX No" name="taxno" type="text" required="required"><br>
<input class="entry" placeholder="Address Line 1" name="address1" type="text" value=""><br>
<input class="entry" placeholder="Address Line 2" name="address2" type="text" value=""><br>
<input class="entry" placeholder="Suburb/County" name="suburb" type="text" value=""><br>
<input class="entry" placeholder="State" name="state" type="text" value=""><br>
<input class="entry" placeholder="Post/Zip Code" name="postcode" type="text" value=""><br>
<input class="entry" placeholder="Country" name="country" type="text" value=""><br>
<input class="entry" placeholder="Primary Contact" name="primarycontact" type="text" value=""><br>
<input class="entry" placeholder="Primary Email" name="primaryemail" type="text" value=""><br>
<input class="entry" placeholder="Subscription Type" name="subscriptiontype" type="text" value=""><br>
<input class="entry" placeholder="Subscription Status" name="subscriptionstatus" type="hidden" value="Active"><br>
<input class="entry" placeholder="Subscription End Date" name="subscriptionenddate" type="text" value=""><br><br><br>
<input class="entry" placeholder="login Email Address" name="loginname" type="text" value=""><br>
<input class="entry" placeholder="First and Last Name" name="counttypename" type="text" value=""><br>
<input class="entry" placeholder="User Type" name="usertype" type="hidden" value="Company Administrator"><br>
<input class="entry" placeholder="User Status" name="status" type="hidden" value="Active"><br>
<input class="entry" placeholder="Password" name="password" type="text" value=""><br>
<input class="button" type="submit">
</form>
</body>
</html>
this is my script file
<?php
include 'db.php';
$sql = "INSERT INTO `companies`
( `accountno`, `companyname` ,
`taxno` , `address1`, `address2`, `suburb` ,
`state` , `postcode`, `country`, `primarycontact` ,
`primaryemail`, `subscriptiontype` ,
`subscriptionstatus`, `subscriptionenddate`,
`datecreated` )
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
$stmt = $conn->prepare($sql);
if ( ! $stmt ) {
echo $stmt->error;
exit;
}
$stmt->bind_param('isssssssssssss',
$_POST['accountno'],
$_POST['companyname'],
$_POST['taxno'],
$_POST['address1'],
$_POST['address2'],
$_POST['suburb'],
$_POST['state'],
$_POST['postcode'],
$_POST['country'],
$_POST['primarycontact'],
$_POST['primaryemail'],
$_POST['subscriptiontype'],
$_POST['subscriptionstatus'],
$_POST['subscriptionenddate']
);
$stmt->execute();
if ( ! $stmt ) {
echo $stmt->error;
exit;
}
$sqla = "INSERT INTO `users`
( `accountno`, `loginname` ,
`password` , `countteamname`, `status`, `usertype` ,
`datecreated` )
VALUES (?,?,?,?,?,?,NOW())";
$stmta = $conn->prepare($sqla);
if ( ! $stmta ) {
echo $stmta->error;
exit;
}
$stmta->bind_param('isssss',
$_POST['accountno'],
$_POST['loginname'],
$_POST['password'],
$_POST['countteamname'],
$_POST['status'],
$_POST['usertype']
);
$stmta->execute();
if ( ! $stmta ) {
echo $stmta->error;
exit;
}
mysqli_close($conn);
header('location: admin.php');
?>
my db values are
userid, accountno, loginname, password, countteamname, status, counttype, piid, datecreated
counttype and piid are not being added at this moment to the table. userid is an auto increment number by mysql.
once i get this uploaded, I will work on securing the password using hash.
I have been trying to figure this out on my own for hours. I hope you can help.
By the looks of it your only adding to the one table with the query
what about concatenating two queries
$sql = "QUERY;";
$sql .= "QUERY;";
use the .= to concatenate both queries.
Related
My form won't post inserted data into my database, i know this is a very basic problem but I am only just starting to learn to code
connect_to_mysql.php:
<?php
$db_host="localhost";
$db_username="ajamesbird";
$db_pass="";
$db_name="test";
$db_connect = mysql_connect("$db_host","$db_username","$db_pass")or die
("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
login.php
<html>
<?php include "C:\Users\andrew\Documents\Websites\Seller\storescripts\connect_to_mysql.php";?>
<?php
if(isset($_POST['loginform'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$sql = ("INSERT INTO users (id, access_level, username, firstname,
lastname, email, password, dob, date_added, activated)
VALUES ('NULL','NULL','$username','$firstname','$lastname','$email', '$password', '$dob', now(), '0')") or die (mysql_error());
if(!mysql_query($db_connect, $sql)){
die('Error inserting into database');
}
}
?>
<head>
<link href="style/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="login.php" enctype="multipart/form-data" name="loginform" id="loginform" method="post">
<input name="username" type="text" id="username" size="63" class="form-control" value="Username" required/>
<input name="firstname" type="text" id="firstname" size="63" class="form-control" value="First name" required/>
<input name="lastname" type="text" id="lastname" size="63" class="form-control" value="Last name" required/>
<input name="email" type="email" id="email" size="63" class="form-control" value="Email" required/>
<input name="password" type="password" id="password" size="63" class="form-control" value="Password" required/>
<input name="dob" type="text" id="dob" size="63" class="form-control" value="Date of Birth" required/>
<input type="submit" name="button" id="button" size="64" value="Sign Up" />
</form>
</body>
</html>
Thank you in advance
Try to move name="loginform" from and put it in hidden input
<html>
<?php include "C:\Users\andrew\Documents\Websites\Seller\storescripts\connect_to_mysql.php";?>
<?php
if(isset($_POST['loginform'])){
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$sql = ("INSERT INTO users (id, access_level, username, firstname,
lastname, email, password, dob, date_added, activated)
VALUES ('NULL','NULL','$username','$firstname','$lastname','$email', '$password', '$dob', now(), '0')") or die (mysql_error());
if(!mysql_query($db_connect, $sql)){
die('Error inserting into database');
}
}
?>
<head>
<link href="style/css.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="login.php" enctype="multipart/form-data" method="post">
<input name="username" type="text" id="username" size="63" class="form-control" value="Username" required/>
<input name="firstname" type="text" id="firstname" size="63" class="form-control" value="First name" required/>
<input name="lastname" type="text" id="lastname" size="63" class="form-control" value="Last name" required/>
<input name="email" type="email" id="email" size="63" class="form-control" value="Email" required/>
<input name="password" type="password" id="password" size="63" class="form-control" value="Password" required/>
<input name="dob" type="text" id="dob" size="63" class="form-control" value="Date of Birth" required/>
<input type="submit" name="button" id="button" size="64" value="Sign Up" />
<input type="hidden" name="loginform">
</form>
</body>
</html>
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>
I'm a newbie in HTML and PHP, so please do help. The values entered in the form are not getting stored in the database. I've checked for the database connection and it is connecting. I have no idea what's wrong with the code.
<?php
include("con.php");
$msg="";
if(isset($_POST["sub_btn"])) {
$id_length=6;
$id=crypt(uniqid(rand(),1));
$id=strip_tags(stripslashes($id));
$id=str_replace(".","",$id);
$id=strrev(str_replace("/","",$id));
$id=substr($id,0,$id_length);
$userid=$id;
$fname=$_POST["fname"];
$lname=$_POST["lname"];
$street=$_POST["street"];
$city=$_POST["city"];
$pin=$_POST["pin"];
$mail=$_POST["mail"];
$phone=$_POST["phone"];
$password=$_POST["pwd"];
$passconf=$_POST["pwdc"];
$mail_check="SELECT mail FROM userdata WHERE mail='$mail'";
$res=mysqli_query($db,$mail_check);
$row=mysqli_fetch_array($res,MYSQLI_ASSOC);
if(mysqli_num_rows($res)==1) {
$msg= "This email is already registered. Please login or use another email ID.";
}
else if (empty($fname) ||empty($lname) ||empty($street) ||empty($city) ||empty($pin) ||empty($mail) ||empty($phone) ||empty($password) ||empty($passconf)) {
//Checks for any blank field.
$msg="Cannot leave the field blank!";
}
elseif($password!=$passconf) {
//Checks for matching password.
$msg= "Passwords don't match!";
}
else {
$query=mysqli_query($db,"INSERT INTO userdata(userid, fname, lname, street, city, pin, mail, phone, password) VALUES('$userid','$fname','$lname','$street','$city','$pin','$mail','$phone','$password')");
if($query) {
$msg= "Thank you! You are now registered.";
//Or give another link to redirect.
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Signup</title>
<link rel="stylesheet" type="text/css" href="signupcss.css">
<link rel="icon" href="pageicon.png">
</head>
<body style="background:#212934;">
<div class="search_box" style="background:#85A25B;">
<form name="signup" action="index.php" method="post">
<h1 style="text-align:center;">SIGNUP</h1>
<h6 style="margin-left:36px;" >The <span style="color:#D62F0B">*</span> indicates required field.</h6>
<hr class="fieldlen_long" style="text-align:center;">
<ul class="form_style">
<br><list><label>FULL NAME<span class="required"> *</span></label>
<input type="text" name="fname" class="fieldlen_split" placeholder="First Name" />
<input type="text" name="lname" class="fieldlen_split" placeholder="Last Name" /></list><br>
<list><label>ADDRESS</label></list>
<list><input type="text" name="street" class="fieldlen_long" placeholder="Street" /></list>
<list><input type="text" name="city" class="fieldlen_split" placeholder="City" />
<input type="text" name="pin" maxlength="6" placeholder="Pincode" /></list>
<br>
<list><label>EMAIL ID <span class="required"> *</span></label>
<input type="email" name="mail" class="fieldlen_mail" placeholder="Email ID" /></list>
<br>
<list><label>PHONE<span class="required"> *</span></label><input type="text" name="phone" maxlength="10" placeholder="Phone" /></list>
<br>
<list><label>PASSWORD<span class="required"> *</span></label><input type="password" name="pwd" class="field-divided" placeholder="Password"/> <input type="password" name="pwdc" class="field-divided" placeholder="Confirm Password"/></list>
<br>
<div class="submitbutton">
<list><input type="submit" name="sub_btn" value="SUBMIT" /></list></div>
</ul>
</form>
</div>
I have a user registration MySql database that I want users to enter data into using a form. This table has a column that I've prepopulated with unique codes. The idea is that each user gets a unique code when they register.
First, is there a better way to handle this than the setup I have?
Second, if there isn't a better way, how do I update just one row of data for each form submission?
Here's what I'm currently doing:
Register Form
<form action="confirm.php" method="post" onsubmit="return Validate();">
<p>First Name: <input type="text" name="firstname" /></p>
<p>Last Name: <input type="text" name="lastname" /></p>
<p>Address: <input type="text" name="address" /></p>
<p>City: <input type="text" name="city" /></p>
<p>State: <input type="text" name="state" /></p>
<p>Zip Code: <input type="text" name="zipcode" /></p>
<p>Phone Number: <input type="text" name="phone" /></p>
<p>Email: <input type="text" name="email" /></p>
<p><input type="submit" /></p>
</form>
Confirm.php
<form action="register.php" method="post" onsubmit="return Validate();">
<p>First Name: <input type="text" name="firstname" id="firstname" size="36" value="<?php echo $_POST['firstname']; ?>"> </p>
<p>Last Name: <input type="text" name="lastname" id="lastname" size="36" value="<?php echo $_POST['lastname']; ?>"> </p>
<p>Address: <input type="text" name="address" id="address" size="36" value="<?php echo $_POST['address']; ?>"> </p>
<p>City: <input type="text" name="city" id="city" size="36" value="<?php echo $_POST['city']; ?>"> </p>
<p>State: <input type="text" name="state" id="state" size="36" value="<?php echo $_POST['state']; ?>"> </p>
<p>Zip Code: <input type="text" name="zipcode" id="zipcode" size="36" value="<?php echo $_POST['zipcode']; ?>"> </p>
<p>Phone Number: <input type="text" name="phone" id="phone" size="36" value="<?php echo $_POST['phone']; ?>"> </p>
<p>Email: <input type="text" name="email" id="email" size="36" value="<?php echo $_POST['email']; ?>"> </p>
<input type="hidden" name="redirect_values" value="true">
<input type="submit" name="confirm" value="Confirm Information">
<input type="button" name="return" value="Change Information" onClick="javascript: window.history.back(-1)";>
</form>
Register.php
<?php ob_start();
$url = 'registercomplete.php';
require("register_dbinfo.php");
$con=mysqli_connect($host,$username,$password,$database);
if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: ".mysqli_connect_error();}
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$city = mysqli_real_escape_string($con, $_POST['city']);
$state = mysqli_real_escape_string($con, $_POST['state']);
$zipcode = mysqli_real_escape_string($con, $_POST['zipcode']);
$phone = mysqli_real_escape_string($con, $_POST['phone']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$sql="INSERT INTO users (id, firstname, lastname, address, city, state, zipcode, phone, email)VALUES ('','$firstname', '$lastname', '$address', '$city', '$state', '$zipcode', '$phone', '$email')";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));}
mysqli_close($con);
while (ob_get_status())
{ob_end_clean();}
header( "Location: $url" );
?>
Database columns:
id|promo|tickets|firstname|lastname|address|city|state|zipcode|phone|email
I'm not sure if I've understood your question.
If id is INT, make the attribute UNIQUE with A_I (Auto Increment).
If you need a unique string for promo, you may want to use an hashing function (like SHA-256) in order to avoid string collisions.
In addition to your question I suggest you to not concatenate escaped string since this technique will not prevent SQL injection. You should use prepared statements and stored procedures.
Here's the form where the user inputs the data in it.
<form action="addemployee.php" method="POST" id="addEmployeeToDataBase">
<input type="text" name="username" placeholder="UserName..." class="txtInput" required="" />
<input type="text" name="fullname" placeholder="Full Name..." class="txtInput" required="" />
<br />
<input type="email" name="email" placeholder="Email Address..." class="txtInput" required="" />
<input type="password" name="password" placeholder="Password..." class="txtInput" required="" />
<br />
<select name="dept" id="txtInput">
<option value="-1">Select Department</option>
<option value="Back-Office">Back-Office</option>
<option value="HR">HR</option>
<option value="Marketing">Marketing</option>
<option value="Sales">Sales</option>
<option value="Technical">Technical</option>
</select>
<br />
<input type="text" name="address" placeholder="Address..." class="txtInput" required=""/>
<input type="text" name="city" placeholder="City..." class="txtInput" required="" />
<br />
<input type="text" name="pincode" placeholder="Pin/Postal/Zip Code..." class="txtInput" required="" />
<input type="text" name="country" placeholder="Country..." class="txtInput" required="" />
<br />
<p style="font-size: 17px;" />Date of Joining:<input type="date" name="joiningdate" placeholder="Joining Date" class="txtInput" required="" />
<br />
<input type="submit" name="submit" id="submit" value="Add Employee"/>
Now I want to use the sql statement where the inputted data will send to the different tables simultaneously.
Table employee (emp_id, username, email, password, dept, address, city, pincode, country and joiningdate).
Table dept (dept_id, dept_name, dept_head, and emp_id).
This form basically inserts into the employee table.
Now the "question" is that I want to insert the data with single form into employee and dept tables respectively, where the input data of dept from the form should also insert into dept_name column of the dept table and should also update the emp_id of the dept table with the inputted data.
Please guide me through this.
$query1= "INSERT INTO employee ( username, email,...)
VALUES ('".$_POST["username"]."', ...)";
if($result1 = mysql_query($query1))
{
$emp_id = mysql_insert_id();
$query2= "INSERT INTO dept ( emp_id, dept_name, ...)
VALUES ('".$emp_id."', '".$_POST["dept_name"]."',...)";
if($result2 = mysql_query($query2))
{
//success msg
}
}
First insert data into employee table after that using mysql function last_insert_id you can get emp_id value and inter into department table.
Assuming, emp_id is auto incremented value.
This might help (I am using Ubuntu 12.04 and LAMP it is working for me)
$sql1 = "INSERT INTO users_account_details (uname,email,pass1,pass2)
VALUES('$_POST[uname]','$_POST[email]','$_POST[pass1]',DES_ENCRYPT('$_POST[pass2]'))";
$sql2 = "INSERT INTO users_personal_details (name,gender,dob,address,mobile)
VALUES('$_POST[name]','$_POST[gender]','$dob','$_POST[address]','$_POST[mobile]')";
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}