Was trying to make a student register page but the data won't insert into database and there was no error message shown so i'm not sure where the problem is. Any help is appreciated!
Here's the code:
<form onsubmit="return Add_Validate()" class="form-horizontal" action="AddStudent.php" method="POST">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="fa fa-plus"></i> Add Student</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-3 control-label">Student Name </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Name" placeholder="Student Name" name="Student_Name1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Gender </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<select class="form-control" id="Add_Student_Gender" name="Student_Gender1" required>
<option value="">~~SELECT~~</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div></div>
<div class="form-group">
<label class="col-sm-3 control-label">Address </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Address" placeholder="Address" name="Student_Address1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Contact Number </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Contact_Number" placeholder="Contact Number" name="Contact_Number1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Email" placeholder="Email" name="Student_Email1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Faculty </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<select class="form-control" name="Add_Faculty1" id="FacultyName" required>
<option value="">~~SELECT~~</option>
<?php $query_faculty="SELECT FacultyName FROM `faculty`";
$result_faculty = mysqli_query($connect, $query_faculty) or die(mysqli_error($connect));
while($row_faculty=mysqli_fetch_array($result_faculty)) { ?>
<option value="<?php echo $row_faculty['FacultyName'] ?>"><?php echo $row_faculty['FacultyName'] ?></option>
<?php } ?></select></div></div>
<div class="form-group">
<label class="col-sm-3 control-label">Username </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Username" placeholder="Username" name="Student_Username1" autocomplete="off" required>
</div></div>
<div class="form-group">
<label class="col-sm-3 control-label">Password </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="password" class="form-control" id="Add_Password" placeholder="Password" name="Student_Password1" autocomplete="off" required>
</div>
</div>
And SQL command:
<?php require 'php_action/db_connect.php';
if($_POST) {
$Student_Name= $_POST['Student_Name1'];
$Student_Gender= $_POST['Student_Gender1'];
$Student_Address= $_POST['Student_Address1'];
$Contact_Number= $_POST['Contact_Number1'];
$Student_Email= $_POST['Student_Email1'];
$FacultyName= $_POST['Add_Faculty1'];
$Student_Username= $_POST['Student_Username1'];
$Password= $_POST['Student_Password1'];
$sql = "INSERT INTO student (Student_Name,Student_Gender,Student_Address,Contact_Number,Student_Email,FacultyName,Student_Username,Password) VALUES ('$Student_Name', '$Student_Gender','$Student_Address','$Contact_Number','$Student_Email','$FacultyName','$Student_Username','$Password')";
if($connect->query($sql) === TRUE) {
echo "<SCRIPT>alert('Student successfully added!');document.location='Student_Register.php'</SCRIPT>";
}
else {
echo "<SCRIPT>alert('Student add unsuccessful!');document.location='Student_Register.php'</SCRIPT>";
}
}
$connect->close();
?>
Found the error, seems to be wrong column name, sorry the all the trouble.....
Admin please close my question.
You're not getting an error message because you never get the error message from MySQL and show it in your alert. $connect->error will contain the error message, and you can add that to your alert like this:
else {
echo "<SCRIPT>alert('Student add unsuccessful! Reason: ' + " . json_encode($connect->error) . ");document.location='Student_Register.php'</SCRIPT>";
}
Related
I am a beginner in web development, I used have an html elements, like textbox and other type of elements, I want to use them as my object and when they have a value and click a button, it will save the value of all the elements in mysql.
I have a code like this, but it cannot be inserted and anything does not happen when clicking a button.
Please help.
PHP:
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'ytp');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if(!$db ) {
die('Could not connect: ' . mysqli_error());
}
$fname = "";
$mname = "";
$lname = "";
$funame = "";
$cnum = "";
$bday = "";
$age = "";
$add = "";
if (isset($_POST['submit'])){
$fname = $_POST['fName'];
$mname = $_POST['mName'];
$lname = $_POST['lName'];
$funame = $_POST['fuName'];
$cnum = $_POST['Cnumber'];
$bday = $_POST['bday'];
$age = $_POST['age'];
$add = $_POST["address"];
}
$sql = "INSERT INTO employee ".
"(fName,mName,lName,fuName,cNumber,bDay,Age,Address) ".
"VALUES ('$fname','$mname','$lname','$funame','$cnum','$bday','$age','$add' )";
if (! mysqli_query($db , $sql)){
echo 'Cannot Insert';
}
else{
echo 'Success';
}
?>
HTML:
<div class="content">
<div class="row">
<div class="col-md-10">
<div class="card">
<div class="card-header">
<h5 class="title">Add User Information</h5>
</div>
<div class="card-body">
<form method="POST" action="php_functions\saveEmployee.php" name="INSERT">
<div class="row">
<div class="col-md-5 pr-md-1">
<div class="form-group">
<label>Company (disabled)</label>
<input type="text" class="form-control" disabled="" placeholder="Company" value="Benchmark Valuer's Inc.">
</div>
</div>
<div class="col-md-3 px-md-1">
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" placeholder="User ID" value="" id="id" name ="id" disabled>
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" placeholder="s.sample#gmail.com" id="email" name ="email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" placeholder="First Name" id="fName" name ="fName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Middle Name</label>
<input type="text" class="form-control" placeholder="Middle Name" id="mName" name ="mName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" id="lName" name ="lName">
</div>
</div>
<div class="col-md-4 pl-md-1" hidden>
<div class="form-group">
<label>Fullname</label>
<input type="text" class="form-control" placeholder="Full Name" id="fuName" name ="fuName">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Contact Number</label>
<input type="tel" class="form-control" placeholder="Contact Number" id="Cnumber" name="Cnumber">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Age</label>
<input type="number" class="form-control" placeholder="Age" id="age" name="age">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Birthday</label>
<input type="date" class="form-control" placeholder="Birthday" id="bday" name="bday">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" placeholder="Home Address" id="address" name ="address">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="Username" id="uName">
</div>
</div>
<div class="col-md-4 px-md-1">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" id="pWord">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>UserType</label>
<input type="number" class="form-control" placeholder="0" id="uType">
</div>
</div>
</div>
<div class="row" hidden>
<div class="col-md-12">
<div class="form-group">
<label>Image Path:</label>
<input type="text" class="form-control" placeholder="C:\\" id="imageURL">
</div>
</div>
</div>
</form>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-fill btn-primary" id="submit" name="submit">Save</button>
<button class="btn btn-fill btn-success" id="btnBrowse">Browse</button>
<button class="btn btn-fill btn-danger" id="btnCancel">Cancel</button>
</div>
</div>
</div>
</div>
</div>
Your HTML form is closing before submit button. You should close that after submit button and also need to manage hierarchy of opening form tag as below:
<form method="POST" action="php_functions\saveEmployee.php" name="INSERT">
<div class="card-body">
<div class="row">
<div class="col-md-5 pr-md-1">
<div class="form-group">
<label>Company (disabled)</label>
<input type="text" class="form-control" disabled="" placeholder="Company" value="Benchmark Valuer's Inc.">
</div>
</div>
<div class="col-md-3 px-md-1">
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" placeholder="User ID" value="" id="id" name ="id" disabled>
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" placeholder="s.sample#gmail.com" id="email" name ="email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" placeholder="First Name" id="fName" name ="fName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Middle Name</label>
<input type="text" class="form-control" placeholder="Middle Name" id="mName" name ="mName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" id="lName" name ="lName">
</div>
</div>
<div class="col-md-4 pl-md-1" hidden>
<div class="form-group">
<label>Fullname</label>
<input type="text" class="form-control" placeholder="Full Name" id="fuName" name ="fuName">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Contact Number</label>
<input type="tel" class="form-control" placeholder="Contact Number" id="Cnumber" name="Cnumber">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Age</label>
<input type="number" class="form-control" placeholder="Age" id="age" name="age">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Birthday</label>
<input type="date" class="form-control" placeholder="Birthday" id="bday" name="bday">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" placeholder="Home Address" id="address" name ="address">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="Username" id="uName">
</div>
</div>
<div class="col-md-4 px-md-1">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" id="pWord">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>UserType</label>
<input type="number" class="form-control" placeholder="0" id="uType">
</div>
</div>
</div>
<div class="row" hidden>
<div class="col-md-12">
<div class="form-group">
<label>Image Path:</label>
<input type="text" class="form-control" placeholder="C:\\" id="imageURL">
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-fill btn-primary" id="submit" name="submit">Save</button>
<button class="btn btn-fill btn-success" id="btnBrowse">Browse</button>
<button class="btn btn-fill btn-danger" id="btnCancel">Cancel</button>
</div>
</form>
Hope it helps you.
the form has been closed before the submit button.
the </form> tag should be placed after the <div class="card-footer">...</div>.
please try it. hope it will help.
If there are any errors while registration or login into the sit then it is not displaying the error message just printing the variable.Here is the code.Tried by doing echo as well but it is also not working.Tried with storing the result in a variable and displaying the message but still not worked.
<?php
include 'includes/db.php';
$match = '';
if(isset($_POST['submit_user']))
{
$email = $_POST['email'];
$check=mysqli_query($conn,"select * from users where user_email='$email'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0)
{
$msg = "Email Already Exists";
header('Location:registration.php?msg=$msg');
}
else
{
if($_POST['password'] == $_POST['con_password'])
{
$ins_sql = "INSERT INTO users (first_name, last_name, user_email, user_password, username) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[email]', '$_POST[password]', '$_POST[username]')";
$run_sql = mysqli_query($conn,$ins_sql);
}
else
{
$match = '<div class="alert alert-danger">Password doesn't match!</div>';
}
}
}
?>
<form class="form-horizontal" action="registration.php" method="post" role="form">
<div class="body">
<div class="row clearfix">
<div class="col-sm-6 col-xs-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="First Name" name="first_name" id="first_name" onkeypress="return checkSpcialChar(event)" required>
</div>
</div>
</div>
<div class="col-sm-6 col-xs-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="Last Name" name="last_name" id="last_name" required>
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="Enter Your Email" name="email" id="email" required>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="password" class="form-control" placeholder="Password" name="password" id="password" required>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="type" class="form-control" placeholder="Confirm Password" name="con_password" id="con_password" required>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" placeholder="Enter Username" name="username" id="username">
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group drop-custum">
<select class="form-control show-tick" name="user_role" id="user_role" required>
<option value="">-- Select User role --</option>
<option value="admin">Admin</option>
<option value="employee">Employee</option>
</select>
</div>
</div>
<div class="col-sm-12">
<button type="submit" class="btn btn-raised g-bg-cyan" name="submit_user" id="subject">Submit</button>
<button type="submit" class="btn btn-raised">Cancel</button>
</div>
<span>
<?php if(isset($_GET['msg']))
echo $_GET['msg'];
?>
</span>
</div>
</div>
</form>
For my website, when this particular page is clicked I want the form to be populated with data from the database. Here is the code for the form:
<form class="form-horizontal" role="form" method="post" action="{{url('/company-profile/update')}}">
{{ csrf_field() }}
#foreach($getAllDetails as $list)
<div class="form-group">
<div class="col-sm-10">
<label for="companyname" class="control-label">Company Name</label>
<input type="text" class="form-control" id="companyname" name="companyname" placeholder="Enter Company Name" value={{$list->companyName}}>
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-sm-4 col-md-3">
<label for="shortCode" class="control-label">Short Code</label>
<input class="form-control" id="shortCode" name="shortCode" placeholder="Short Code" value={{$list->shortCode}}>
</div>
<div class="col-xs-7 col-sm-6 col-md-7">
<label for="telnum" class="control-label">Telephone Number</label>
<input type="tel" class="form-control" id="telnum" name="telnum" placeholder="Tel. number" value={{$list->phoneNo}}>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<label for="emailid" class="control-label">Email</label>
<input type="email" class="form-control" id="emailid" name="emailid" placeholder="Email" value={{$list->emailAddress}}>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<label for="logoPath" class="control-label">Logo Path</label>
<input type="" class="form-control" id="logoPath" name="logoPath" placeholder="Enter Logo Path" value={{$list->logoPath}}>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<label for="feedback" class="control-label">Contact Address</label>
<textarea class="form-control" id="address" name="address" rows="2" value={{$list->contactAddress}}></textarea>
</div>
</div>
#endforeach
<div class="form-group">
<div class="col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
This issue is:
For example, if I want to fetch the company name {{$list->companyName}} only the first word gets displayed. For example, If the company's name is National Film Institute, only National gets displayed.
Here is code the for my index function in the controller:
public function index()
{
$data['getAllDetails']= DB::table('tblcompany')->get();
return view('companyProfile.companyProfile', $data);
}
Change
value={{$list->your_field}}
to
value="{{$list->your_field}}"
I am new to php. When form is submitted the data has to store on database. When I started to code it worked fine but later on data is not inserted into database I was trying to insert form details to database but unable to insert on regular basis. Sometimes,data is stored on database but most of the times, it doesn't store. so please help me....
php version:7
and MySQL database
form
<form role="form" class="register-form" method="post" action="aluminiregdb.php">
<center><h2>Alumni Registration</h2></center>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="first_name" id="first_name" class="form-control input-lg" placeholder="First Name" tabindex="1">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="last_name" id="last_name" class="form-control input-lg" placeholder="Last Name" tabindex="2">
</div>
</div>
</div>
<div class="form-group">
<input type="number" name="roll_number" id="roll_number" class="form-control input-lg" placeholder="Roll Number" tabindex="3">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" tabindex="4">
</div>
<div class="row">
<div class="col-xs-12 col-lg-12 col-sm-6 col-md-6 ">
<select class="form-group form-control input-lg" name="SelectInstitution">
<option value="select">Select Institution</option>
<option value="sdes">Sree Dattha institute of Engineering and science (SDES)</option>
<option value="sdgi">Sree Dattha group of istitutions Integrated Campus (SDGI)</option>
<option value="sdip">Sree Dattha institute of Pharmacy (SDIP)</option>
<option value="sdbn">Sree Dattha Brindavan institute of Teacher Education (SDBN)</option>
<option value="sdbd">Sree Dattha Brindavan institute of Diploma (SDBD)</option>
<option value="bits">Brindavan institute of Teacher Education (BITS)</option>
</select>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-lg-12 col-sm-6 col-md-6 ">
<select class="form-group form-control input-lg" name="SelectInstitutiondegree">
<option value="select">Select Degree Obtained</option>
<option value="B.Tech">B.Tech</option>
<option value="M.Tech">M.Tech</option>
<option value="MBA">MBA</option>
<option value="Polytechnic">Polytechnic</option>
<option value="B.Pharm">B.Pharm</option>
<option value="M.Pharm">M.Pharm</option>
<option value="Pharm.D">Pharm.D</option>
<option value="B.Ed">B.Ed</option>
</select>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group">
<input type="text" name="program" id="program" class="form-control input-lg" placeholder="Program : CSE or ECE or B.Pharm or M.Pharm or B.Ed etc..." tabindex="5">
</div>
</div>
</div>
<div class="form-group">
<input type="number" name="year" id="year" min="2000" class="form-control input-lg" placeholder="Completion Year Of Graduation" tabindex="6">
</div>
<div class="form-group">
<input type="date" name="dob" id="dob" class="form-control input-lg" placeholder=" Date of Birth : 1994-07-23" tabindex="7">
</div>
<div class="form-group">
<input type="number" name="mobile" id="mobile" class="form-control input-lg" placeholder="Mobile Number" tabindex="8">
</div>
<div class="form-group">
<textarea class="form-control form-control input-lg" name="adress" rows="6" data-rule="required" data-msg="Please write something for us" placeholder="Address"></textarea>
<div class="validation"></div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group input-lg">
<label for="Status">Current Status :  </label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Working" onclick="doClick(this)">Working
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Self-Employed" onclick="doClick(this)">Self-Employed
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Studying" onclick="doClick(this)">Studying
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Home Maker" onclick="doClick(this)">Home Maker
</label>
</div>
</div>
</div>
<div class="row" id="textbox" style="display:none">
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group">
<input type="text" name="org_name" id="org_name" class="form-control input-lg" placeholder="Organization Name" tabindex="9">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group">
<input type="text" name="designatin" id="designation" class="form-control input-lg" placeholder="Designation in your Company" tabindex="10">
</div>
</div>
</div>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-md-12"><input align="center" name="alumniregs" type="submit" value="Submit" class="btn btn-theme btn-block btn-lg" tabindex="11"></div>
</div>
</form>
dbconnection
if(isset($_POST['alumniregs'])){
if(isset($_POST['first_name'])){
$fname=$_POST['first_name'];
}
if(isset($_POST['last_name'])){
$lname=$_POST['last_name'];
}
if(isset($_POST['roll_number'])){
$roll=$_POST['roll_number'];
}
if(isset($_POST['email'])){
$email=$_POST['email'];
}
if(isset($_POST['SelectInstitution'])){
$institu=$_POST['SelectInstitution'];
}
if(isset($_POST['SelectInstitutiondegree'])){
$insttidegree=$_POST['SelectInstitutiondegree'];
}
if(isset($_POST['program'])){
$program=$_POST['program'];
}
if(isset($_POST['year'])){
$year=$_POST['year'];
}
if(isset($_POST['dob'])){
$dob=$_POST['dob'];
}
if(isset($_POST['mobile'])){
$mobile=$_POST['mobile'];
}
if(isset($_POST['adress'])){
$adress=$_POST['adress'];
}
if(isset($_POST['optradio'])){
$optradio=$_POST['optradio'];
}
if(isset($_POST['org_name'])){
$orgname=$_POST['org_name'];
}
if(isset($_POST['designatin'])){
$designatin=$_POST['designatin'];
}
$date = date('Y-m-d H:i:s');
$smt=$db->prepare("INSERT INTO `naik`.`aluminireg` (`fname`, `lname`, `rollno`, `email`, `Institution`, `degree`, `branch`, `year`, `dob`, `mobile`, `adress`, `presentstatus`, `orgname`, `designatin`,`date`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,'$date')");
$smt->bind_param('ssssssssssssss',$fname,$lname,$roll,$email,$institu,$insttidegree,$program,$year,$dob,$mobile,$adress,$optradio,$orgname,$designatin);
$smt->execute();
$smt->close();
if ($smt) {
echo "<script>alert('Item Entered Successful')</script>";
}
}
?>
config
<?php
$dbconnection=array(
'server'=>'localhost',
'user'=>'root',
'password'=>'java',
'dbname'=>'naik'
);
$db=new mysqli(
$dbconnection['server'],
$dbconnection['user'],
$dbconnection['password'],
$dbconnection['dbname']
);
//if($db!="null"){
// echo "<script>alert('connection Successful')</script>";
//}
if($db->connect_errno>0){
echo "database connection error".$db->connect_error;
exit;
}
//echo $db->host_info;
#echo $db->connect_errno;
?>
I work on from to add more than one device when user click on add more
But How can I group multiple fields with same name as array for every device from this form, my server side language is PHP.
<form>
<div class="row">
<div class="col-md-12">
<div class="widget stacked">
<div class="widget-header">
<i class="icon-hdd"></i>
<h3>Remove Devices</h3> <a class="btn label label-success add_new_device">Add more</a>
</div>
<div class="widget-content">
<div class="form-group">
<label for="device_name">Device Name</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[device_name][]" type="text">
</div>
<div class="form-group">
<label for="device_description">Device Description</label>
<input class="form-control" data-validation="length" data-validation-length="max255" data-validation-optional="true" name="device[device_description][]" type="text">
</div>
<div class="form-group">
<label for="device_url">Device Url</label>
<input class="form-control" data-validation="url" data-validation-optional="true" data-validation-help="Ex: http://000.000.000.000/index.cgi" name="device[device_url][]" type="url">
</div>
<div class="form-group">
<label for="device_ip4">IP4</label>
<input class="form-control" data-validation="length" data-validation-length="max15" data-validation-optional="true" data-validation-help="Ex: 000.000.000.000" name="device[device_ip4][]" type="text">
</div>
<div class="form-group">
<label for="device_ip6">IP6</label>
<input class="form-control" data-validation="length" data-validation-length="max45" data-validation-help="Ex: 0000:0000:0000:0000:0000:0000" name="device[][device_ip6]" type="text">
</div>
<div class="form-group">
<label for="device_username">Device Username</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_username]" type="text">
</div>
<div class="form-group">
<label for="device_password">Device Password</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_password]" type="text">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="widget stacked">
<div class="widget-header">
<i class="icon-hdd"></i>
<h3>Remove Devices</h3> <a class="btn label label-success add_new_device">Add more</a>
</div>
<div class="widget-content">
<div class="form-group">
<label for="device_name">Device Name</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[device_name][]" type="text">
</div>
<div class="form-group">
<label for="device_description">Device Description</label>
<input class="form-control" data-validation="length" data-validation-length="max255" data-validation-optional="true" name="device[device_description][]" type="text">
</div>
<div class="form-group">
<label for="device_url">Device Url</label>
<input class="form-control" data-validation="url" data-validation-optional="true" data-validation-help="Ex: http://000.000.000.000/index.cgi" name="device[device_url][]" type="url">
</div>
<div class="form-group">
<label for="device_ip4">IP4</label>
<input class="form-control" data-validation="length" data-validation-length="max15" data-validation-optional="true" data-validation-help="Ex: 000.000.000.000" name="device[device_ip4][]" type="text">
</div>
<div class="form-group">
<label for="device_ip6">IP6</label>
<input class="form-control" data-validation="length" data-validation-length="max45" data-validation-help="Ex: 0000:0000:0000:0000:0000:0000" name="device[][device_ip6]" type="text">
</div>
<div class="form-group">
<label for="device_username">Device Username</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_username]" type="text">
</div>
<div class="form-group">
<label for="device_password">Device Password</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_password]" type="text">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="widget stacked">
<div class="widget-header">
<i class="icon-hdd"></i>
<h3>Remove Devices</h3> <a class="btn label label-success add_new_device">Add more</a>
</div>
<div class="widget-content">
<div class="form-group">
<label for="device_name">Device Name</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[device_name][]" type="text">
</div>
<div class="form-group">
<label for="device_description">Device Description</label>
<input class="form-control" data-validation="length" data-validation-length="max255" data-validation-optional="true" name="device[device_description][]" type="text">
</div>
<div class="form-group">
<label for="device_url">Device Url</label>
<input class="form-control" data-validation="url" data-validation-optional="true" data-validation-help="Ex: http://000.000.000.000/index.cgi" name="device[device_url][]" type="url">
</div>
<div class="form-group">
<label for="device_ip4">IP4</label>
<input class="form-control" data-validation="length" data-validation-length="max15" data-validation-optional="true" data-validation-help="Ex: 000.000.000.000" name="device[device_ip4][]" type="text">
</div>
<div class="form-group">
<label for="device_ip6">IP6</label>
<input class="form-control" data-validation="length" data-validation-length="max45" data-validation-help="Ex: 0000:0000:0000:0000:0000:0000" name="device[][device_ip6]" type="text">
</div>
<div class="form-group">
<label for="device_username">Device Username</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_username]" type="text">
</div>
<div class="form-group">
<label for="device_password">Device Password</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_password]" type="text">
</div>
</div>
</div>
</div>
</div>
</form>
Name your fields like devices[0][description], devices[1][description] and so on.
If every object has the exact same number of fields and every one is a new record - you can use devices[][description] and browser will fill indexes for you, but this can lead to bugs in the future, if you decide to use same form for editing records.
Also see How to get form input array into PHP array