Insert Data to database not working - php

i have this html form
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Please Input Below Information</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form method="POST" action="model.php" class="form-horizontal">
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Model Code *</label>
<div class="col-sm-10">
<input type="text" name="ModelCode" class="form-control" id="inputEmail3" placeholder="e.g KTI">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Model Name *</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="ModelName" id="inputPassword3" placeholder="e.g 0002">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Units *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelUnit" style="width: 100%;">
<option value="" selected disabled>Choose Model Unit</option>
<?php
$sql = mysqli_query($con, "select departmentname from department");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['departmentname']; ?>"><?php echo $row['departmentname']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Hour Theroy *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelHourT" style="width: 100%;">
<option value="" selected disabled>Choose Model Hour</option>
<?php
$sql = mysqli_query($con, "select GroupTeory from grouptheory");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['GroupTeory']; ?>"><?php echo $row['GroupTeory']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Hour Practice *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelHourP" style="width: 100%;">
<option value="" selected disabled>Choose Model Practice</option>
<?php
$sql = mysqli_query($con, "select GroupPractical from grouppractical");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['GroupPractical']; ?>"><?php echo $row['GroupPractical']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Model Type *</label>
<div class="col-sm-10">
<input type="text" name="ModelType" class="form-control" id="inputEmail3" placeholder="e.g KTI">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Semister *</label>
<div class="col-sm-10">
<select class="form-control" style="width: 100%;" name="SemsterID">
<option value="" selected disabled>Choose Model Type</option>
<?php
$sql = mysqli_query($con, "select Semester from semester");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['Semester']; ?>"><?php echo $row['Semester']; ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<input type="submit" name="submit" class="btn btn-info pull-right" value="Submit" />
</div>
<!-- /.box-footer -->
</form>
</div>
and this is my php insert function
include("Connection.php");
if(isset($_POST['submit']))
{
echo "<script>alert($query1)</script>";
$ModelCode = mysqli_real_escape_string($con,$_POST['ModelCode']);
$ModelName = mysqli_real_escape_string($con,$_POST['ModelName']);
$ModelUnit = mysqli_real_escape_string($con,$_POST['ModelUnit']);
$ModelHourT =mysqli_real_escape_string($con,$_POST['ModelHourT']);
$ModelHourP = mysqli_real_escape_string($con,$_POST['ModelHourP']);
$ModelType = mysqli_real_escape_string($con,$_POST['ModelType']);
$SemsterID = mysqli_real_escape_string($con,$_POST['SemsterID']);
$query1 = mysqli_query($con, "INSERT INTO 'model' (ModelCode,ModelName,ModelUnit,ModelHourTheory,ModelHourPractical,ModelType,StageID) VALUES ('$ModelCode','$ModelName','$ModelUnit','$ModelHourT','$ModelHourP','$ModelType' ,'$SemsterID')");
echo "<script>alert($query1)</script>";
}
and it doesn't work .... i didn't no what's happen !! even i have been compare fields of the table with the name of the database ... but it still doesn't work ... anyone knows what is problem ?

You don't should use single quote for table name (in this case single quote is for literal value )
Assuming that you table is named model you should use
$query1 = mysqli_query($con,
"INSERT INTO model (ModelCode, ModelName,ModelUnit,
ModelHourTheory,ModelHourPractical,ModelType,StageID)
VALUES ('$ModelCode','$ModelName','$ModelUnit',
'$ModelHourT','$ModelHourP','$ModelType','$SemsterID')");

Related

Get the selected dropdown related value into the text box

I want to generate the salary after user selects the designation dropdown value from the database.
my db strucuture is as follows:
employee:
emp_id dep_id
1 1
2 1
designation:
dep_id des_name salary
1 accountant 20000
This is my html code:
<div class="form-group">
<label class="control-label col-lg-2" for="initial">Designation :</label>
<div class="col-lg-3">
<select class="form-control input-sm" name="classification" id="classification" onchange="changeValue();">
<?php while($row3 = mysqli_fetch_array($result3)):; ?>
<option><?php echo $row3[2]; ?></option>
<?php endwhile; ?>
</select>
<span class="error_form" id="classification_error_message"></span>
</div>
<label class="control-label col-lg-2" for="basicSalary">Basic Salary :</label>
<div class="col-lg-3">
<input type="text" class="form-control input-sm" name="basicSalary" id="basicSalary" placeholder="0.00">
</div>
</div>
You can do this by using this code butt this code from php codeigniter. Now u can changing it according to your requirements.
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">select Categories <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select class="form-control" name="cat" id="catogires">
<!-- <option value="">Select categories</option>-->
<?php
$data=$this->Product_Model->cat_select();
foreach($data as $row)
{ ?>
<option <?php if ($product->cat_id==$row->cat_id) echo "selected='selected'"; ?> value="<?php echo $row->cat_id?>"><?php echo $row->cat_name;?></option>
<?php
}
?>
</select>
</div>
</div>

input to sql not ok with 'copy-paste'

i have a php form, that adds a 'review' to a 'guide' in an SQL database.
i finally figured out my problem, and it is when i use copy paste in the input areas.
what can cause this??
any help appreciated!!
this is the link
this is my code:
<body >
<form class="form-horizontal" method="POST" >
<div class="form-group" >
<div class="col-sm-1">
<a href="." > <img src="Pictures/Design/בחזרה לאתר.png" height="100" width="100"/> </a>
</div>
<div class="col-sm-10">
<h2>הוספת ביקורת ל: <?php if(isset($_SESSION ['name'])){echo $_SESSION ['name'];} else {echo $gname;}?></label>
</h2>
</div>
</div>
<hr>
<div class="form-group">
<div class="col-sm-12 " >
<div class="col-sm-4 " align="right">
<?php $val="uploads/guides/".$row['Picture'];?>
<image src='<?php echo $val;?>' height="130px" width="100px">
</div>
<div class="col-sm-7 " align="right">
<div><label class=" control-label" ><?php if(isset($_SESSION ['name'])){echo $_SESSION ['name'];} else {echo $gname;}?></label><label class=" control-label" >&nbsp&nbsp&nbsp:שם המדריך</label></div>
<div><label class=" control-label" ><?php if(isset($_SESSION ['attraction'])){echo $_SESSION ['attraction'];} else {echo $gattraction;}?></label><label class=" control-label" >&nbsp&nbsp&nbsp:אטרקציה</label></div>
<div><label class=" control-label" ><?php if(isset($_SESSION ['email'])){echo $_SESSION ['email'];} else {echo $gemail;}?></label><label class=" control-label" >&nbsp&nbsp&nbsp:אימייל</label></div>
<div><label class=" control-label" ><?php if(isset($_SESSION ['phone'])){echo $_SESSION ['phone'];} else {echo $gphone;}?></label><label class=" control-label" >&nbsp&nbsp&nbsp:טלפון</label></div>
</div>
</div>
</div>
<hr>
<div class="form-group">
<div class="col-sm-12 " >
<div class="col-sm-6 col-sm-offset-6" align="right">
<label class=" control-label" >שמך המלא</label>
</div>
<div class="col-sm-12">
<input type="text" class="form-control" name="name" placeholder="" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 " >
<div class="col-sm-6 col-sm-offset-6" align="right">
<label class=" control-label" >מתי השתמשת בשירותי המדריך</label>
</div>
<div class="col-sm-6 " align="right">
<select class="form-control" name="year">
<option value="בחר/י שנה">בחר/י שנה</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select>
</div>
<div class="col-sm-6 " align="right">
<select class="form-control" name="month">
<option value="בחר/י חודש">בחר/י חודש</option>
<option value="ינואר">ינואר</option>
<option value="פברואר">פברואר</option>
<option value="מרץ">מרץ</option>
<option value="אפריל">אפריל</option>
<option value="מאי">מאי</option>
<option value="יוני">יוני</option>
<option value="יולי">יולי</option>
<option value="אוגוסט">אוגוסט</option>
<option value="ספטמבר">ספטמבר</option>
<option value="אוקטובר">אוקטובר</option>
<option value="נובמבר">נובמבר</option>
<option value="דצמבר">דצמבר</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 " >
<div class="col-sm-6 col-sm-offset-6" align="right">
<label class=" control-label" >ציון 1-5</label>
</div>
<div class="col-sm-3 col-sm-offset-9 " align="right">
<select class="form-control " name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 " >
<div class="col-sm-6 col-sm-offset-6" align="right">
<label class=" control-label" >כמה שילמת ומה זה כלל</label>
</div>
<div class="col-sm-12">
<textarea class="form-control" name="pay" cols="40" rows="2"></textarea>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 " >
<div class="col-sm-6 col-sm-offset-6" align="right">
<label class=" control-label" >פרטו את חוויתכם עם המדריך בהרחבה</label>
</div>
<div class="col-sm-12">
<textarea class="form-control" name="exp" cols="40" rows="5" required></textarea>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 " >
</div>
</div>
<div class="form-group" >
<div class="col-sm-12 col-sm-offset-5"><button type="submit" class="btn btn-primary btn-sm" name="addreview" value="Submit Record">הוספת ביקורת</button></div>
</div>
<?php
if(isset($_POST['addreview'])){
$name = $_POST['name'];
$name = str_replace("'","''",$name);
$monthh = $_POST['month'];
$yearr = $_POST['year'];
$pay = $_POST['pay'];
$pay = str_replace("'","''",$pay);
$exp= $_POST['exp'];
$exp = str_replace("'","''",$exp);
$rating=$_POST['rating'];
$query1="INSERT INTO review (ReviewerName,ServiceMonth,ServiceYear,Rating,Payment,Experience,Guide_ID) VALUES ";
$query1.="('$name','$monthh','$yearr','$rating','$pay','$exp','$guideid')";
$result=mysqli_query($conn,$query1);
if($result){
?>
<script type="text/javascript">
window.location = "./review_added_successfully.php";
</script>
<?php
}
else{
echo "not submitted";
}
} ?>
</form>
</body>

Update Database tables with values input by user in CodeIgniter using PHP

I'm trying to update one of my user tables in the Database with values taken from the the user. But for some reason it's not updating anything.
HTML Form
<form class="form-horizontal" method = "post">
<fieldset>
<!-- Form Name -->
<legend>User Details</legend>
<div>
<?php echo $this->session->flashdata('msg'); ?>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Full Name</label>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="something" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dob">Date of Birth</label>
<div class="col-md-4">
<input id="dob" name="dob" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Gender</label>
<div class="col-md-2">
<select id="gender" name="gender" class="form-control">
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="degree">Degree</label>
<div class="col-md-8">
<input id="degree" name="degree" type="text" placeholder="degree" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="specialization">Specialization</label>
<div class="col-md-8">
<input id="specialization" name="specialization" type="text" placeholder="specialization" class="form-control input-md">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="year">Degree Year</label>
<div class="col-md-2">
<select id="year" name="year" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="semester">Semester</label>
<div class="col-md-2">
<select id="semester" name="semester" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Upload Profile Picture</label>
<div class="col-md-4">
<input id="filebutton" name="filebutton" class="input-file" type="file">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
<button id="submit" name="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
studentDashboardController
public function saveUserDetails()
{
$this->load->model('userModel');
if (isset($this->session->userdata['logged_in']))
{
$username = ($this->session->userdata['logged_in']['username']);
}
$user = $this-> userModel-> getUserUid($username); //Gets the uid of the current user
$this->userModel->saveUserDetails($user);
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Successfully Inserted Data</div>');
$this->load->view('studentDashboard/common',$data);
redirect(base_url('index.php/studentDashboard/editProfile',$data1));
}
userModel
public function saveUserDetails($uid)
{
$data = array(
'name' => $this->input->post('name')
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
The sysuser has the uid and name fields.I' not sure what I'm doing wrong here. Any help in this regard will be appreciated
You dont send your post data to controller. Change $this->userModel->saveUserDetails($user); as
$this->userModel->saveUserDetails($user,$this->input->post('name'));
Now Model should be like
public function saveUserDetails($uid,$name)
{
$data = array(
'name' => $name
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
and make sure you have set correctly form action like
<form class="form-horizontal" method="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">

how to create multiple query in one. when i want fetch date from database which is search from user

here my html code.
i want fetch data from database. when a user search profile then i want display only searched data which enter in form.
the problem is query doesn't work. can you tell me can we run multiple query in single query.
<form method="get" action="search.php" enctype="multipart/form-data">
<div class="form_but1">
<label class="col-sm-2 control-lable1" for="sex">Gender : </label>
<div class="col-sm-7 form_radios">
<div class="select-block1">
<select name="gender">
<option value="">Select Gender</option>
<option value="">Male</option>
<option value="">Female</option>
</select>
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="form_but1">
<label class="col-sm-2 control-lable1" for="sex">Marital Status : </label>
<div class="col-sm-7 form_radios">
<div class="select-block1">
<select name="mstatus">
<option>Select status</option>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="Widow">Widow</option>
<option value="Widower">Widower</option>
<option value="Divorcee">Divorcee</option>
<option value="Seprated">Seprated</option>
</select>
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="form_but1">
<label class="col-sm-2 control-lable1" for="sex">Gotra : </label>
<div class="col-sm-7 form_radios">
<div class="select-block1">
<select name="gotra">
<option>Select gotra</option>
<option value="Aalakuntor">Aalakuntor</option>
<option value="Bantalor">Bantalor</option>
<option value="Batalor">Batalor</option>
<option value="Bayamuttalor">Bayamuttalor</option>
</select>
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="form_but1">
<label class="col-sm-2 control-lable1" for="sex">District / City : </label>
<div class="col-sm-7 form_radios">
<div class="select-block1">
<input type="text" class="form-control" placeholder="city" name="city" >
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="form_but1">
<label class="col-sm-2 control-lable1" for="sex">Education : </label>
<div class="col-sm-7 form_radios">
<div class="select-block1">
<input type="text" class="form-control" placeholder="education" name="education" >
</div>
</div>
<div class="clearfix"> </div>
</div>
<div class="form_but1">
<label class="col-sm-2 control-lable1" for="sex">Age : </label>
<div class="col-sm-7 form_radios">
<div class="col-sm-5 input-group1">
<input class="form-control has-dark-background" name="age1" id="slider-name" placeholder="28" type="text" >
</div>
<div class="col-sm-5 input-group1">
<input class="form-control has-dark-background" name="age2" id="slider-name" placeholder="40" type="text" >
</div>
<div class="clearfix"> </div>
</div>
<div class="clearfix"> </div>
</div>
<div class="form_but1">
<div class="col-sm-5 input-group1">
<div class="select-block1">
<input type="submit" class="btn btn-success" value="Search" name="search">
</div>
</div>
<div class="clearfix"> </div>
</div>
</form>
here my search.php code.
<div class="paid_people">
<h1></h1>
<div class="row_1">
<?php
include("includes/db.php");
global $con;
if(isset($_GET['search']))
{
$search_gender = $_GET['gender'];
$search_mstatus = $_GET['mstatus'];
$search_gotra = $_GET['gotra'];
$search_city = $_GET['city'];
$search_education = $_GET['education'];
$search_age1 = $_GET['age1'];
$search_age2 = $_GET['age2'];
$get_user = "SELECT * FROM users WHERE age BETWEEN '$search_age1' AND '$search_age2' AND gender like '$search_gender' AND mstatus like '$search_mstatus' AND gotra like '$search_gotra' AND education like '$search_education' AND city like'$search_city'";
$run_user = mysqli_query($con, $get_user);
while ($row_user = mysqli_fetch_array($run_user))
{
$u_id = $row_user['user_id'];
$u_fname = $row_user['first_name'];
$u_age = $row_user['age'];
$u_education = $row_user['education'];
$u_occupation = $row_user['occupation'];
$u_city = $row_user['city'];
$u_image = $row_user['photo'];
$profile_id = $row_user['profile_id'];
echo "<div class='col-sm-6 paid_people-left'>
<ul class='profile_item'>
<a href='view_profile.php?userdetail_id=$u_id'>
<li class='profile_item-img'>
<img src='admin_vadarshadi/users-photo/resized_$u_image' class='img-responsive' alt='$u_fname' />
</li>
<li class='profile_item-desc'>
<h4>Profile ID: $profile_id</h4>
<h4>Name: $u_fname Age: $u_age Yrs </h4>
<p>City: $u_city, Education: $u_education</p>
<h5>View Full Profile</h5>
</li>
<div class='clearfix'> </div>
</a>
</ul>
</div>";
}
}
?>
</div>
</div>

insertion failed in database

<?php
include('include/connection.php');
session_start();
$_SESSION['verify_remote_agent']=$_SERVER['HTTP_USER_AGENT'];
$r=session_id();
if(isset($next)) {
$city=$_POST['city'];
$from_city=$_POST['from_city'];
$to_city=$_POST['to_city'];
$query="INSERT INTO user_details values('$r','$city',$from_city,'$to_city')";
mysql_query($query);
}
?>
<div class="container">
<form action="index2.php" method="POST">
<div class="form-group city col-sm-4">
<select class="form-control" name="city">
<option value="choose">Choose City</option>
<option value="option[1]">Delhi/NCR</option>
<option value="others">Other</option>
</select>
</div>
<div class="form-group from_city col-sm-4">
<input type="text" class="form-control" name="from_city" placeholder="From Area">
</div>
<div class="form-group to_city col-sm-4">
<input type="text" class="form-control" name="to_city" placeholder="To Area">
</div>
<p class="text-right">
<input class="btn btn-primary" type="submit" value="Next Step" name="next">
</p>
</form>
</div>
Try this:
$query="INSERT INTO user_details values('".$r."','".$city."','".$from_city."','".$to_city."')";
mysql_query($query);

Categories