i want to update the fields below but my form is not working. it is not storing data what should i do. i used jquery accordion fo r the fields so it will click the item he wants to edit then update the fields then submit. but it is not working.
VIEW
foreach($people as $row){
echo "<h3>".$row->service."</h3>";
echo "<form action='".base_url()."some_controller/updateCI' method='post'> <div>Service ID: <input type=text name=id value='".$row->id."' size=27px/><br>Service Name: <input type=text name=name value='".$row->service."'><input type='button' class='classname' value='Save'/></form></div>";
}
?>
CONTROLLER
public function updateCI(){
$this->load->model('some_model');
$id = $this->input->post('id');
$servName = $this->input->post('name');
$success = $this->some_model->updateCI($id,$servName);
if($success == TRUE)
$this->editCI_page(TRUE);
else $this->editCI_page(FALSE);
}
MODEL
public function updateCI($id,$servName){
//$name = $this->db->escape_str($name);
$appID = $this->db->escape_str($id);
$ciName = $this->db->escape_str($servName);
$queryStr = "UPDATE appwarehouse.service SET id='$appID',service='$ciName' WHERE id = '$appID';";
$query = $this->db->query($queryStr);
return $query;
}
You can do something like this in your model:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
I would recommend you to use the active record which are available for codeigniter. For more information visit the below link:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Related
I'm using Laravel 8 framework.
Using a query to get values from the table in and display it in the checkbox, but when I select more than one it just displays single, not multiple.
This is Blade PHP code with query.
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Students:</strong>
<div style="margin-top: 5px" >
<?php $conn = new mysqli('localhost', 'root', '', 'rp') or die ('Cannot connect to db');
$result = $conn->query("select users.id, users.name from users, model_has_roles where model_has_roles.model_id=users.id and model_has_roles.role_id=14;
");
echo "<html>";
echo "<body>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['name'];
echo '<form><input type=checkbox name="student_id[]" value="'.$id.'">'.$name.'</form>'; }
//echo "</select>";
echo "</body>";
echo "</html>";?>
He is my function code in Controller
public function update(Request $request, Ticket $ticket){
request()->validate([
'gpname' => 'required',
'detail' => 'required',
'semester' => 'required',
'gender' => 'required',
// 'status',
// 'Assigned',
]);
$users= Auth::user();
$rr= $users->roles->pluck('name')->toArray();
// dd($users->id);
// $data= $request->all();
//$ticket = new Ticket;
if($rr[0]=='Admin'){
$data= $request->all();
//dd($data['Assigned']) ;
$ticket->gpname=$data['gpname'];
$ticket->detail=$data['detail'];
$ticket->semester=$data['semester'];
$ticket->gender=$data['gender'];
//dd("hhh");
$student_id = array("student_id");
if(isset($_POST['submit'])){
if(!empty($_POST['student_id'])){
$student_id = $_POST['student_id'];
foreach($student_id as $id){
// echo $id.'<br/>';
dd($data['student_id']);
}
}
}
dd($data['student_id']);
//dd( $data['student_id[]']);
//$ticket->studentnames=$users->empid;
$ticket->user_id=$users->id;
//$ticket->Assigned=$data['Assigned'];
$ticket->save();
and as you see if I make dd($data['student_id']); as a comment it's never going to the previous one inside foreach()
How I can get multiple values from the checkbox?
dd(); is killing the loop and returning only the first value you should echo your results instead of dd(); them or use.
if(!empty($_POST['student_id'])){
$student_id = $_POST['student_id'];
$array_test = []
foreach($student_id as $id){
if($id == $data['student_id']){
array_push($id, $array_test)
}
}
dd($array_test);
}
Since i don't know what are you trying to do so i will drop the second thought
if(!empty($_POST['student_id'])){
$student_id = $_POST['student_id'];
$array_test = []
foreach($student_id as $id){
array_push($id, $array_test)
}
dd($array_test);
}
I've small blog users can add new post and others can comment on this post ( Post is working perfectly, comments is working too )
all posts is showing and all comment is showing but each Separated , how can I display it related.
function getPostInfoo(){
if($this->num_members < 0){
$q = "SELECT * FROM ".TBL_POSTS;
$result = mysqli_query($this->connection, $q);
$fields = array();
while($fetch=mysqli_fetch_array($result)){
echo "
<h3>".$fetch['post']."</h3>
<p>".$fetch['username']."</p>";
}
$this->num_members = mysqli_fetch_array($result);
}
//return
return $fields;
}
function getComment(){
$q = "SELECT ".TBL_POSTS.".*, ".TBL_COMMENTS.".*
FROM ".TBL_POSTS."
INNER JOIN ".TBL_COMMENTS." ON ".TBL_COMMENTS.".postid=".TBL_POSTS.".postid";
$result = mysqli_query($this->connection, $q);
/* Error occurred, return given name by default */
if(!$result || (mysqli_num_rows($result) < 1)){
return NULL;
}
$fields = array();
while($fetch=mysqli_fetch_array($result)){
$fields[] = "<p><a href='#'>".$fetch['username'].": </a>".$fetch['comment']."<br>
<small class='text-muted'>".$fetch['cmntdate']."</small>
</p>
";
}
/* Return result array */
return $fields;
}
test.php
echo "<div>";
$myList2 = $database->getPostInfoo();
if (is_array($myList2) || is_object($myList2)){
foreach($myList2 as $arrayItem2){
echo $arrayItem2;
}
}else {
echo "No Posts yet.";
}
echo "</div>
<div>";
$myList = $database->getComment();
if (is_array($myList) || is_object($myList)){
foreach($myList as $arrayItem){
echo $arrayItem;
}
}else {
echo "No comments yet.";
}
<form method='post' action='functions.php' method='POST'>
<div class='form-group' >
<textarea class='form-control' name = 'comment' placeholder='Write Comment'></textarea>
<input type='hidden' name='postid' value='1'>
<input type='hidden' name='comment_sub' value='1'>
<input type='submit' value='Add Comment!'>
</div>
</form>
</div>";
what I need to do is to display each comments on its own post & show comment textarea on each post
Please see the image https://i.stack.imgur.com/A4D55.jpg
You need to be getting one post at a time and then fetch its comments.
So your test.php should look this test.php?id=post_id
Where post_id is the unique identifier of your post based on your db structure
Could be e.g is 1 or 2 or if you are using auto-increment id
Then you can add the following code to at the beginning of your test.php to get the post id
$post_id = isset($_GET['id']) ? $_GET['id'] : '';
The fetch the post and it's comment from your table using the value of $post_id
Hope it helps
Try this function:
function getPostsInfo(){
$q = "SELECT ".TBL_POSTS.".`id`,".TBL_POSTS.".`post`, ".TBL_POSTS.".`username`, ".TBL_COMMENTS.".`username` as comment_username, ".TBL_COMMENTS.".`comment`, ".TBL_COMMENTS.".`cmntdate`
FROM ".TBL_POSTS."
LEFT JOIN ".TBL_COMMENTS." ON ".TBL_COMMENTS.".postid=".TBL_POSTS.".postid";
$result = mysqli_query($this->connection, $q);
$posts = [];
while($fetch=mysqli_fetch_array($result)){
if(isset($posts[$fetch["id"]])){
$posts[$fetch["id"]]["comments"][] = [
"username" => $fetch["comment_username"],
"comment" => $fetch["comment"],
"cmntdate" => $fetch["cmntdate"]
];
}else{
$posts[$fetch["id"]] = [
"post" => $fetch["post"],
"username" => $fetch["username"],
"comments" => [[
"username" => $fetch["comment_username"],
"comment" => $fetch["comment"],
"cmntdate" => $fetch["cmntdate"]
]]
];
}
}
return $posts;
}
I want to give user possibility to delete other users. I made a script where user get list over all users with delete button. Button start the script but doesn't delete user from the list. Here is my code:
function get_userlist(){
$select_users = $this->db->prepare("SELECT user_id, fname, lname, email
FROM user");
$select_users->execute();
echo "<tbody>";
while($row = $select_users->fetch(PDO::FETCH_ASSOC)){
echo "<form action='' method=''><tr><td>".$row["user_id"]."</td><td>".$row["fname"]."</td><td>
".$row["lname"]."</td><td>".$row["email"]."</td><td>
<input type='hidden' name='user_id' value=".$row["user_id"] . " >
<input type='submit' name='submit_delete' value='Delete'></td>
</tr></form>";
}
echo "</tbody></table>";
}
function delete_user($user_id)
{
$delete_user = $this->db->prepare("DELETE FROM user WHERE user_id = :user_id");
$delete_user->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$delete_user->execute();
}
And file where I use those functions:
$user = new USER($con);
$user->get_userlist();
if(isset($_POST['submit_delete'])){
$user_id = $_POST['user_id'];
$user->delete_user($user_id);
}
What am I doing wrong?
You are not passing $user_id variable to your delete_user function. Edit your code like this:
function delete_user($user_id)
{
$delete_user = $this->db->prepare("DELETE FROM user WHERE user_id = :user_id");
$delete_user->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$delete_user->execute();
}
how to make changes to the data 'title' and 'description' after the image is loaded, for example, add a button to "change"?
This is all done up and running
https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-MySQL-database-integration
Here is my code, but here it is necessary to make a new method to "upgrade" and synchronize it with html:
html:
name <br /><input name="title[]" value="{%=file.title||''%}">
description<br /> <input name="description[]" value="{%=file.description||''%}">
php:
public function update($print_response = true) {
$response = parent::update(false);
foreach ($response as $name => $update) {
if ($update) {
$sql = 'update set `title`=? ,`description`=? '
.$this->options['db_table'].'` WHERE `name`=?';
$query = $this->db->prepare($sql);
$query->bind_param('sss',$file->title,$file->description, $name);
$query->execute();
}
}
return $this->generate_response($response, $print_response);
}
JS?
My assignment is to use CodeIgniter (an MVC framework) and connect the form that's in the view to the database. I have done most of the code, and I believe most of it is correct. I finally got the view page (form) to load, but I cannot get the Register function in the controller to load. Does anyone see any errors in my code? It would help me a great deal.
form.php (view):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php //echo $title;?></title>
</head>
<body>
<h1>Register for a Dorm</h1>
<form method="post"
action="http://ispace.ci.fsu.edu/~trm07/assignment4/index.php/controller/register/">
<fieldset>
<label>First Name</label>
<input type="text" name="first_name"/>
<label>Last Name</label>
<input type="text" name="last_name"/>
</fieldset>
<label>Undergraduate Level:</label>
<?php
//dropdown list for level
echo '<select name="level">';
$level = array('Freshman','Sophomore', 'Junior', 'Senior');
foreach ($level as $selection) {
echo '<option value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
?>
<label>Gender:</label>
<?php
//dropdown list for gender
echo '<select name="gender">';
$gender = array('Male','Female');
foreach ($gender as $gend_selection) {
echo '<option
value="'.$gend_selection.'">'.$gend_selection.'</option>';
}
echo '</select>';
//radio buttons for dorms
$requested_dorm = array('Landis','Salley','DeGraff','Cawthon','Reynolds');
echo("<fieldset><legend>Requested Dorm</legend>");
foreach ($requested_dorm as $dorm_names){
echo "<input type='radio' name='dorm_name' value='$dorm_names' />
$dorm_names <br />";
}
?>
<br><input type="submit" value="Register">
</body>
</html>
controller.php
<?php
class Controller extends CI_Controller {
function index()
{ //$data['title'] = "Register for a Dorm";
$this->load->view('form');
}
function show()
{
$this->load->model('model');
$dorms = $this->model->get_dorms();
foreach($dorms as $dorm){
if($dorm['dorm_name'] == $dorm_name)
$chosen_dorm = $dorm['dorm_name'];
}
}
function register()
{
$this->load->library('form_validation');
$view_data = array('message' => '');
//If the form was submitted, process it
if (count($_POST) > 0)
{ $dorm_name = $this->input->post('dorm_name');
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$level = $this->input->post('level');
$gender = $this->input->post('gender');
{
//Validate the input
$this->form_validation->set_rules('first_name', 'First Name',
'required|strip_tags|trim');
$this->form_validation->set_rules('last_name', 'Last Name',
'required|strip_tags|trim');
$val_result = $this->form_validation->run();
//Add the data to the database
if ($val_result == TRUE)
{
$this->load->model('model');
$db_result = $this->model->add_student_to_dorm($_POST['dorm_name'],
$_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);
if ($db_result == TRUE)
{
$view_data['message'] = "Added student to the dorm!";
//$view_data['message'] = "Added" . "$_POST['first_name']" . " " .
"$_POST['last_name']". " to " . "$_POST['dorm_name']" . " hall.";
}
else
{
$view_data['message'] = "An error occured adding the student to the dorm!";
}
}
}
$this->load->view('form',$view_data);
//$this->load->model('model');
//$this->model->get_students();
}
}
}
?>
model.php:
<?php
class Model extends CI_Model {
//function to take student info posted from form, and adds to database
public function add_student_to_dorm()
{
$this->load->database($dorm_name, $first_name, $last_name, $level, $gender);
$data = array
(
'dorm_name' =>$dorm_name,
'student_fname' => $first_name,
'student_lname' => $last_name,
'student_level' => $level,
'student_gender' => $gender,
);
$result = $this->db->insert('student', $data);
return $result;
}
//get dorm table results in an array from database, return its rows
public function get_dorms()
{
$this->load->database();
$dorms = $this->db->get('dorm');
$rows = $dorms->result_array();
return $rows;
}
//get student table results in an array from database, return its rows
public function get_students()
{
$this->load->database();
$students = $this->db->get('student');
$stu_rows = $students->result_array();
return $stu_rows;
}
}
?>
Here you called add_student_to_dorm() function in the controller page with parameters but in model you doesnot take that parameters that is
$db_result = $this->newmodel->add_student_to_dorm($_POST['dorm_name'],
$_POST['first_name'], $_POST['last_name'], $_POST['level'], $_POST['gender']);
this is the function in controller but your model is only have
public function add_student_to_dorm() no variables So change
public function add_student_to_dorm() to
public function add_student_to_dorm($dorm_name, $first_name, $last_name, $level, $gender)
and also change
$this->load->database($dorm_name, $first_name, $last_name, $level, $gender); to
$this->load->database(); in model then it is working