Is there a way for me to determine the value or array that has been passed in the form?

I am facing a challenge and need help solving it. I have a project that involves interacting with a database, where I created a CRUD HTML table for data manipulation. There are 15 tables in the relative database, and I need to insert, delete, and edit records within these tables. The main issue I'm encountering is how to ensure that when I insert values into a specific table in my HTML form, those values are accurately inserted into the corresponding table in the database.

Below is a snippet of the controller code:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
...

Additionally, here is a glimpse of the model code which is still a work in progress:

<?php

class Main_model extends CI_Model {

function getClass() {
    $query = $this->db->select('*')
            ->from('class')
            ->get();
    return $query->result();
}

...

The model consists of various functions for fetching data from different tables in the database.

Answer №1

What about including the data type with the information as well?

...
case 'case1':
 $data['table'] = $result..
 $data['table_type'] = 'case1';
break;
case 'case2':
 $data['table'] = $result..
 $data['table_type'] = 'case2';
break;
...

In your views, you can include the table_type variable in the submit button:

<input type="submit" name="<?=$table_type;?>" value="update">

or use a HIDDEN FIELD

<input type="hidden" name="<?=$table_type;?>" value="TRUE">

Then in your controller where you handle the POST data, you can check it like this:

if($this->input->post('case1'))
{
  //for case 1
  //insert/update to case1 table
}elseif($this->input->post('case2'){
  //for case 2
  //insert/update to case2 table
}else{
 //default
}

This is just a basic example, you can enhance security and make the POST value checking more flexible.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show information from multiple tables that have a common foreign key

I have a MySQL database with 5 tables storing information. Using PHP, I need to retrieve and display data from these tables in a linked manner. Each table has an 'academy_id' field as a foreign key, and each academy has a contact person. Some aca ...

Using MAMP and PHP in the Terminal

After setting up MAMP and using PHP successfully, I also tried running PHP commands in Terminal. However, now I am facing an issue where Terminal is unable to find the PHP command: "Terminal / PHP command not found" This has left me unable to work on any ...

Steps for logging into Laravel using both MD5 and Bcrypt hashing concurrently

I am currently working with a database where passwords are stored using MD5 encryption. Now, I need to go through the following steps: 1. User login (using bcrypt) 2. If the login fails, move on to step 3. Otherwise, log in and exit. 3. User login ...

Server-side magic: Requesting POSTed value from $_GET

My webpage contains the following JavaScript code in the header, utilizing POST method: function submitData() { var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://.../process.php?data=123', true); xhr.onreadystatechan ...

Tips for implementing Memcached with Doctrine2 and ZF2

After following the instructions in this article: , my code, directories, and configuration are all set up accordingly. My current config file resembles the following: <?php namespace Game; return array( // <snip> // Doctrine config ...

Can you place 4 table cells in the first row and 3 table cells in the second row?

Exploring the world of HTML and CSS designs for the first time. Here's a code snippet I've been working on: <html> <body> <table width="100%"> <tr> <td width="25%">&#160;</td> ...

Executing http GET/POST requests asynchronously using PHP with true asynchronous functionality

I am currently seeking a solution in PHP to perform the following tasks: http_get_or_post('an.url', 'or.two'); // Implement necessary operations here without worrying about HTTP processes occurring in the background. $r = wait_for_an ...

What is the process of running PHP in a .ctp file within the CakePHP framework?

I have recently started working with CakePHP and I have a question about how PHP code is executed in a file with the .ctp extension. Can you explain how PHP is processed within a .ctp file in CakePHP? Additionally, I'm also curious about executing PHP ...

Mastering the Art of Absolute Positioning in HTML Tables' Header Row

I have a table with 80 rows. How can I make the first row fixed? I attempted to add position: fixed; to the but unfortunately, it didn't work. Is there a way to achieve this using CSS or jQuery? <table> <thead> <tr> <td sty ...

Divergent outcomes observed in MSSQL between SSMS and Node JS script

Recently, I encountered an issue with a SQL query that retrieves customer transaction headers based on their card_number. The query should return an audit_number column. Surprisingly, when executing the query in SSMS software, the results are correct; howe ...

Navigating within a nested view using Angular's UI-Router

I've encountered a puzzling issue that I've been grappling with for the past three days. It seems like a straightforward problem, but for some reason, it's causing me a lot of trouble. The main parent view in my code contains two child view ...

What are the requirements for uploading files to a server directory?

Do you have a question regarding how to handle upload filename standards in your application? Let's say you have a system where various types of documents can be uploaded to the server and stored in a directory. Sometimes, the same document might even ...

Exploring the Depths of Laravel Eloquent: A Guide to Master

Seeking guidance as a beginner in Laravel, I have encountered an issue with laravel relationships. Could use some assistance from the experts here. Dealing with four tables: 1. Users 2. Teams 3. Games 4. Picks The Picks table serves as my interme ...

Align the responsive image in the center of a container

I am facing a challenge in centering an image wrapped by an anchor tag inside a div. I have tried using display flexbox and justify-content:center for the image, which works to some extent. However, the height of the image remains constant while the width ...

A guide on extracting data from various HTML elements effectively with JavaScript

I'm searching for a universal technique to extract values from multiple HTML elements. For instance: <div>Experiment</div> <select><option>Experiment</option></select> <input value="Experiment" /> These thr ...

Show a hierarchical representation of the tree structure from the object up to the second level only

Here is the table structure I am working with: |human_id | parent_id | name | I want to display the data in a tree-like format. For example: Alisa --Robin --Miranda --Mark Max Robin --Robert --John The current select statement I have includes joins and ...

How can I prevent opening duplicate tabs of the same website simultaneously?

Is there a way to prevent users from accessing the same website page from multiple tabs? Could this be accomplished using JavaScript? ...

Having difficulty submitting a form with ajax, while accomplishing the same task effortlessly without ajax

I have been experimenting with submitting a form using ajax to the same .php file. When I submit the form without ajax directly (form action), the database gets updated. However, when I try the same process with ajax, there is no change in the database. H ...

Selecting a webpage from a dropdown list to display its specific content within an iframe

I have a dropdown menu similar to this: <form name="change"> <SELECT NAME="options" ONCHANGE="document.getElementById('frame1').src = this.options[this.selectedIndex].value"> <option value="">Choose</option> <opti ...

A guide to displaying the date retrieved from a JSON data file API request using JavaScript

How can I display the date from a JSON data file API call using JavaScript? I am facing difficulty in showing the date on the dashboard display page. I am utilizing JavaScript with async-await for calling the API, and Bootstrap 4 for design. The function ...