POST data as array - php

Following is my form:
<form method="POST" action="../controller/assignsubteacher.php">
<table class="table table-bordered table-striped table-hover table-condensed" id="coursedetail" >
<thead>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</thead>
<tbody id="table_ajax">
</tbody>
<tfoot>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</tfoot>
</table>
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-block btn-flat">Submit</button>
</div>
</form>
and table in the form is populated by following response:
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
}
echo $tr;
I am not able to use the posted data to insert in to database. Is there any way i can send the data as Array and retrieve it.
following is the AJAX to populate the table body:
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
console.log(xhr.responseText);
Table.innerHTML=xhr.responseText;
}
};
I was thinking of using foreach to insert data in the POST controller, but have no idea how to achieve that.
any help would be appreciated.

Use SQL insert query to do so.
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
$sql = "INSERT INTO `table` (column1, column2, column3) VALUES ($list, $subject_name, $tr)";
//Replace columns & table name
if(mysqli_query($con, $sql)) {
echo "Inserted";
}
}
echo $tr;

Related

How to delete multiple rows from table using Angular.js?

I'm trying to delete multi rows from table using checkbox with Angular and php ,
but i'm not able to get that to work and my knowledge with Angular stoped here !
this is my table :
<div class="table-responsive" style="overflow-x: unset;">
<button class="btn btn-danger" ng-click="deleteBulk()" >Delete</button>
<table datatable="ng" dt-options="vm.dtOptions" class="table table-bordered table-striped" >
<thead>
<tr>
<th ><input type="checkbox" ng-model="master" > select</th>
<th>date</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
<th>action</th>
<th>action</th>
<th>action</th>
</tr>
</thead>
<tbody>
<tr id="<?php echo $row["id"]; ?>" ng-repeat="name in namesData " >
<td name="checkbox">
<input type="checkbox" name="arrExample"
ng-model="arrInput" ng-true-value="{{name.id}}"
ng-checked="master"
ng-click='pushInArray(name.id)'>
</td>
<td>{{name.date}}</td>
<td>{{name.first_name}}</td>
<td>{{name.last_name}}</td>
<td>{{name.age}}</td>
<td><button type="button" ng-click="fetchSingleData(name.id)" class="btn btn-warning btn-xs">edit</button></td>
<td><button type="button" ng-click="deleteData(name.id)" class="btn btn-danger btn-xs">delete</button></td>
<td><button type="button" ng-click="single_move(name.id)" class="btn btn-success btn-xs">send</button></td>
</tr>
</tbody>
</table>
and this is my Angular code :
$scope.exampleArray = [];
$scope.pushInArray = function(id) {
// get the input value
var inputVal = id;
var array = $scope.exampleArray.push(inputVal);
$scope.deleteBulk = function(){
if(confirm("Are you sure you want to delete this record?")){
$http({
method:"POST",
url:"insert.php",
data:{'id':$scope.exampleArray, 'action' : 'deleteBulk'}
}).success(function(data){
$scope.success = true;
$scope.error = false;
$scope.successMessage = data.message;
$scope.fetchData();
});
}
};
};
and this is my php code inside insert.php
if($form_data->action == "deleteBulk")
{
$query = "
DELETE FROM orders WHERE id='".$form_data->id."'
";
$statement = $connect->prepare($query);
if($statement->execute())
{
$output['message'] = 'done!';
}
}
can anyone tell me what i'm doing wrong please ?
thank you
You are deleting one row each time with a unique id. So code base is correct.
To delete multiple rows, use the checkbox in each row and user will select multiple rows to delete and then delete rows by multiple ids.
You will also need to update SQL query to delete multiple rows at a time.
Something like below
DELETE FROM orders WHERE id IN (1, 2, 3, 4, ..., 10);

How to make search with condition using ajax and codeigniter

I want to make search within specific condition using codeigniter framework
when I press inside my view the condition doesn't happen
($this->db->where('del_status','1');)
I think my problem in query to data base.
my data base model
public function SearchData($tablename,$data)
{
//select * from users where username like'%ss%' AND del_status ='1'
$this->db->select("*");
$this->db->from($tablename);
$this->db->like('username', $data);
$this->db->or_like('fullname', $data);
$this->db->or_like('email', $data);
$this->db->where('del_status','1');
$sql = $this->db->get();
return $sql->result();
}
this is my controller
public function Search()
{
if($this->input->post("action") =="searchonusers" )
{
$data=$this->input->post("data");
$this->load->model("DatabaseModel");
$serachdata=$this->DatabaseModel->SearchData("users",$data);
if(is_array($serachdata)&&count($serachdata)>0)
{
echo '<div class="col-lg-12">
<div class="userdataa table-responsive">
<table class=" table table-responsive table-bordered table-hover ">
<tr>
<th>id</th>
<th>user name</th>
<th>full name</th>
<th> image</th>
<th>email</th>
<th>type</th>
<th>status</th>
<th>date</th>
<th>time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
</tr>';
foreach ($serachdata as $user):
echo'<tr id="'."user".$user->id.'">
<td>'.$user->id.'</td>
<td>'.$user->username.'</td>
<td>'.$user->fullname.'</td>
<td><img class="img-responsive" height="40%" width="40%" src="'.base_url()."upload/".$user->image.'"></td>
<td>'.$user->email.'</td>';
$usertype='';
if($user->type== '1'){
$usertype=" <label class='label label-warning'>User</label>";
}else{
$usertype="<label class='label label-danger '> Admin</label>";
}
echo '<td>'.$usertype.'</td>';
$acativate='';
$status='';
$regstatus=$user->status;
if($regstatus == '0'){
$acativate='<button class="btn btn-success btn-xs showsta" status="'.$user->status.'" id="'.$user->id.'">enable</button>';
$status='<label class="label label-danger">disactive</label>';
}else{
$acativate='<button class="btn btn-xs btn-danger showsta" status="'.$user->status.'" id="'.$user->id.'">disable</button>';
$status='<label class=" label label-success">active</label>';
}
echo' <td>'.$status.'</td>
<td>'.$user->date.'</td>
<td>'.$user->time.'</td>
<td><button class="deleteuserfirst btn btn-danger btn-xs" id="'.$user->id.'"><span class="glyphicon glyphicon-trash"></span>delete</button></td>
<td><button type="button" class="btn btn-primary btn-xs edituser" id="'.$user->id.'" data-toggle="modal" data-target="#usereditorwe"><span class="glyphicon glyphicon-edit">Edit</button>
<td> '.$acativate.'</td>
</tr>';
endforeach;
echo'</table>
</div>
</div>';
}else{
echo '<div class="col-lg-12">
<div class="userdataa table-responsive">
<table class=" table table-responsive table-bordered table-hover">
<tr>
<th>id</th>
<th>user name</th>
<th>full name</th>
<th> image</th>
<th>email</th>
<th>type</th>
<th>status</th>
<th>date</th>
<th>time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
</tr>
<tr>
<td colspan="12">No Data Found</td>
</tr>
</table>
</div>';
}
}else{
exit();
}
}
}
this is my ajax code
$(".serchdata").on("click",function () {
var action="searchonusers";
var data=$("#dataa").val();
if(data ==""){
alert("Please Insert Your Search Data ... ")
}else{
$.ajax({
url:"<?php echo base_url("Users/Search")?>",
method: "post",
data:{action:action,data:data},
success:function (data) {
$(".userdata").remove();
$(".box").remove();
$("#pagination_link").remove();
$(".serchresult").html(data)
Table();
}
})
}
});
You are checking is_array on an object type result() :
if(is_array($serachdata)&&count($serachdata)>0)
to check for an array, use result_array() instead :
return $sql->result_array();
Please replace your model with that
public function SearchData($tablename,$data)
{
//select * from users where username like'%ss%' AND del_status ='1'
$this->db->select("*");
$this->db->from($tablename);
$this->db->like('username', $data);
$this->db->or_like('fullname', $data);
$this->db->or_like('email', $data);
$this->db->where('del_status','1');
$sql = $this->db->get()->result_array();
return $sql;
}

update multiple rows mssql with php on iis

i want update multiple rows my table sql server use php 7 on iis, i have script like this
this is my script
this my action when click "submit" button
$getResults= sqlsrv_query($conn, $tsql, $params);
$rowsAffected = sqlsrv_rows_affected($getResults);
if ($getResults == FALSE or $rowsAffected == FALSE)
die(FormatErrors(sqlsrv_errors()));
echo ($rowsAffected. " row(s) updated: " . PHP_EOL);
sqlsrv_free_stmt($getResults);
}
}
else
{
echo "Data belum berubah";
}
?>
this is my form
<form action='' method='POST'>
<div class="box-body">
<div style='overflow:auto;width:1300px;'>
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>my column name</th>
</tr>
</thead>
<?php
$tampil=sqlsrv_query($conn,'SELECT top 10 * FROM ws_manado ORDER BY Region', array(), array( "Scrollable" => 'static' ));
$count=sqlsrv_num_rows($tampil);
if (!$tampil)
{
printf("Error: %s\n", msql_error($conn));
exit();
}
$no=1;
while ($r=sqlsrv_fetch_array($tampil,SQLSRV_FETCH_ASSOC))
{
echo "$count
<tr>
<td id='Id[]' value=$r[Id]>$no</td>
<td id='Site_ID_Implementation[]' contenteditable='true'>$r[Site_ID_Implementation]</td>
<td id='Implementation_Date[]' contenteditable='true'>$r[Implementation_Date]</td>
<td id='PR_Number[]' contenteditable='true'>$r[PR_Number]</td>
<td id='PR_Amount[]' contenteditable='true'>$r[PR_Amount]</td>
</tr>";
$no++;
}
?>
</table>
</div>
<input type="submit" name="submit" id="submit" value="submit">
</div>
</form>
but when i click submit button my table not update

Getting multi rows in the table even if my database has no value using angular.js and PHP

i have one problem in my angular.js code.I have a table which will filled up by database value. when initially my database has no value and table should display blank but in my case now the database is empty still the table is splitting into 5 rows which has no value.I am explaining my code below.
subject.html:
<table class="table table-bordered table-striped table-hover" id="dataTable">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-1 col-sm-1">
</colgroup>
<thead>
<tr>
<th>Sl. No</th>
<th>
<select style="width:100%; border:none; font-weight:bold;">
<option>Course Name</option>
</select>
</th>
<th>Semester</th>
<th>Subject Name</th>
<th>Short Name</th>
<th>Edit</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr ng-repeat="sub in subjectData" >
<td>{{ $index+1 }}</td>
<td>{{sub.course_name}}</td>
<td>{{sub.semester}}</td>
<td>{{sub.subject_name}}</td>
<td >{{sub.short_name}}</td>
<td>
<a href=''>
<input type='button' class='btn btn-xs btn-green' value='Edit'>
</a>
</td>
</tr>
</tbody>
</table>
subjectController.js:
var subApp=angular.module('GofastoHome');
subApp.controller('subjectcontroller',function($scope,$http){
$http({
method: 'GET',
url: "php/subject/readSubjectData.php",
}).then(function successCallback(response) {
console.log('response',response);
$scope.subjectData=angular.fromJson(response);
},function errorCallback(response) {
alert("could not fetch data");
});
})
Here initially my database has no value.When user is refreshing(i.e-subject.html) the page the table present in the html content is splitting into 5 rows with no data but 5 serial number is coming which i don't need.check my php file below.
readSubjectData.php:
$connect = mysqli_connect("localhost", "root", "******", "go_fasto");
$result = mysqli_query($connect, "select * from db_subject order by subject_id desc ");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print json_encode($data);
My requirement is table will be splitted only when database has some value.When data database is blank it should not which i am getting in my current code.Please help me.
Try this:
Add script ng-show="showtable" to table tag which will become like this:
<table ng-show="showtable" class="table table-bordered table-striped table-hover" id="dataTable">
Then change your controller script like below:
var subApp=angular.module('GofastoHome');
subApp.controller('subjectcontroller',function($scope,$http){
$http({
method: 'GET',
url: "php/subject/readSubjectData.php",
}).then(function successCallback(response) {
console.log('response',response);
$scope.subjectData=angular.fromJson(response);
if($scope.subjectData.length > 0) //
$scope.showtable = true; // add these lines
else //
$scope.showtable = false; //
},function errorCallback(response) {
alert("could not fetch data");
});
})

View in laravel error undefined variable

I am a newbie in laravel and php. I cannot show the results of query in laravel. I wrote this in my program.
routes.php
Route::get('books/see', function()
{
return View::make('books.absen');
});
Route::post('books/absen','BookController#absen');
BookController.php
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return Redirect::to('books/see');
}
absen.blade.php
<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
<?php
for( $i=1; $i<19; $i++ )
{
?>
<option>
<?php echo $i;
}
?>
</option>
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
#foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
#endforeach
</tbody>
</table>
And error is Undefined variable: results (View: ...\absen.blade.php) I very tired with this. Help please
Instead of redirecting, you should render the view in the absen() action. When doing the redirect the data you just selected from the db is all gone.
Try this:
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return View::make('books/see')->with('results', $results);
}
Also you need to check if $results exists in your view, since you also want to display it when no results are available
#if(isset($results))
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
#foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
Attention
André Daniel is very right with his comment. Your code is prone to SQL injection. You should really take a look at Laravels ORM Eloquent or the query builder. At the very minimum, use bindings for parameters:
DB::select(DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = ?"), array($ruang));
Here's an example with the query builder (Thanks #AndréDaniel)
DB::table("book")->where("ta", $ruang)->get()
You have to make your view with your variable.
Here is what you should do :
routes.php
Route::get('books/see', function()
{
return View::make('books.absen');
});
Route::post('books/absen','BookController#absen');
BookController.php
public function absen()
{
$ruang = Input::get('ruangn');
$results = DB::select( DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));
return View::make('books.absen')->with(array('results' => $results));
}
absen.blade.php
<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
<?php
for( $i=1; $i<19; $i++ )
{
?>
<option>
<?php echo $i;
}
?>
</option>
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">
#if(isset($results))
<table class="table table-bordered table-striped">
<thead>
<th>No</th>
<th>Name</th>
<th>ISBN</th>
<th>TA</th>
<th>TB</th>
</thead>
<tbody>
#foreach ($results as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->isbn}}</td>
<td>{{$value->ta}}</td>
<td>{{$value->tb}}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
By the way, you should use Laravel form functions Forms & HTML

Categories