Adding information to two tables in PHP

I have been searching around this forum, and while I have found similar questions that have already been answered, I am still struggling to figure this out (as always, I'm a complete newbie and need clear explanations).

In my database, I have tables users.db (userId, mmbrship, mmbrship_date) with a few more values but the ones I need to update are mentioned above.

Additionally, there is a payments.db ( id, txnid, payment_amount, payment_status, itemid, createdtime )

Within my functions.php file, I have the following code:

function updatePayments($data){ 
    global $link;
    if(is_array($data)){                
       $sql = mysql_query("INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
               '".$data['txn_id']."' ,
               '".$data['payment_amount']."' ,
               '".$data['payment_status']."' ,
             '".$data['item_number']."' ,
                '".date("Y-m-d H:i:s")."' 
    )", $link);

    return mysql_insert_id($link);
    }
}

My goal is to insert the values 'itemid' and 'createdtime' from payments.db into users.db in the columns 'mmbrship' and 'mmbrship_date'

Furthermore, I also need to add the 'userId' from users.db to payments.db (in order to link the user to their purchase).

This will allow me to check the users.db for information on whether a member has paid their membership fee. Currently, the payments.db inserts its own id, so connecting these two tables with the userId seems like a step in the right direction...

? Any assistance would be greatly appreciated :)

Answer №1

It appears that your database schema lacks a crucial connection between the user entity and the payment entity.

(In the context of ER modeling, an entity represents a distinct object or concept that we need to record information about and is significant...)

To determine the cardinality of the relationship between these entities, numerous questions must be considered:

  • Could one user be associated with multiple payments?

  • Is it possible for a user to have zero payments?

  • Can a single payment be linked to more than one user?

Based on factors like these, we can establish whether the connection between user and payment is one-to-one, one-to-many, or many-to-many, as well as if it's mandatory or optional.

My assumption is that in your setup, each payment will correspond to just one user. Therefore, a specific payment won't apply to multiple users, and a given user may have no payments, one payment, or multiple payments.

In such a scenario, a one-to-many relationship exists, and it's conventionally appropriate to include the primary key of the user entity as a column in the payment table, accompanied by a foreign key constraint.

For example:

  ALTER TABLE payment ADD `userId` INT UNSIGNED COMMENT 'foreign key reference to user' ;

  ALTER TABLE payment ADD CONSTRAINT FK_payment_user 
    FOREIGN KEY (userId) REFERENCES user (userId)
    ON UPDATE CASCADE ON DELETE RESTRICT ;

Prior to inserting a row into the payment table, you must identify which user it relates to and assign a value to the userId column to establish the association between this payment and its corresponding user.

If a payment isn't connected to any user, store a NULL value in the column. To mandate a payment being tied to a user, add a NOT NULL constraint on the column.

The datatype of the userId column in the payment table should match the datatype of the userId column in the user table. (The specific datatype wasn't provided above.)


In relational databases, relationships between rows in different tables are established by storing shared values in columns of those tables.

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

Enhance your online shopping experience with a personalized touch by incorporating an Ajax add to cart button on

Currently, I am working on customizing the add to cart button on a single product page. After implementing an ajax call solution from LoicTheAztec's post here, I have successfully added the feature. The code works perfectly fine, but now I am faced w ...

Looking to retrieve the image source from a SQL database and display it in a view file using Ajax in CodeIgniter?

I am new to PHP and have successfully created an AJAX dynamic dependent dropdown in CodeIgniter. It is working fine, but now I want to show an image for the selected option when the last dropdown's option is selected. Please help me with this. View a ...

Looking for the location of the apc.php file?

I've successfully deployed the APC cache on AWS Elastic Beanstalk (confirmed via phpinfo()) How can I verify if APC is functioning properly? Where can I find the location of the apc.php file? Thank you ...

Can administration users view the same content and functionalities as other user types?

I am currently working on a Laravel web application that caters to two distinct types of users: customers admins Depending on their user type, they have access to different features and functionalities. Customers When customers log in, they are greete ...

Replacing a variable in LessPHP with PHP

I have been using LessPHP (v0.3.4-2) and I have read the documentation and searched for answers to this basic question, but I am still unable to find the correct and straightforward solution. Below is my .less file: @bgcolor :#ccc; .color(@color:#222) { ...

Create interactive HTML tables with detailed information using PHP and MySQL databases

I am attempting to achieve a similar result as shown here: but with the addition that my HTML tables will be filled dynamically. The scenario involves two tables: +------------------------+ | TB_ACTIVE_USERS | +------------------------+ | ID | ...

Develop conditional variables using if/else statements and utilize regular expressions for matching and replacing based on the variable string

Our platform offers a text area where users can input order data and replace variables with real information. For instance, the variable {{service_name}} could be substituted with "DJ Booth". Currently, I am working on displaying specific text based on t ...

Track user sessions and transfer username to final page

Is there a way to showcase the chosen username from a dropdown menu on the initial page and then also display it on the last page without having to pass variables between pages? Additionally, if I wish to incorporate a timer on a middle page, how can I sto ...

Generating a JSON form to enable users to search for specific dates within a given range by utilizing the "datetime" data type

Currently, I am in the process of developing a form that can search for specific data within a JSON's datetime value. The objective is to retrieve information from the JSON that falls between the first and last dates entered by the user. However, desp ...

I require assistance in determining the difference between two times as I am currently unable to obtain the results I

After experimenting with different methods, I have found some challenges in calculating the time difference between two values separated by a colon. The times are received through an XML feed, giving me the flexibility to manipulate and convert the strings ...

Exploring the Functions of Database Forge Class in CodeIgniter 3.1.1

I am currently in the process of developing a new application with CodeIgniter 3.1.1 and I am looking to customize the database columns by either adding new columns or deleting existing ones. Despite consulting the CodeIgniter documentation which led me to ...

Creating a Dynamic Content Management System with PHP and MYSQL: How to Implement Multiple Categories for

Looking for assistance in displaying all categories associated with a single article post. This is the database structure: blog_posts table postID int, primary, auto increment. postTitle varchar postCont text blog_categories table catID int, primary ...

Error 506 occurred when attempting to create a campaign using Mailchimp

I am currently working on setting up a Mailchimp campaign through the API and utilizing the campaignCreate() method. Here is the code snippet I have written: <?php $message = array( 'html'=>'Yo, this is the <b>html</b> p ...

Submitting form data in Angular is a straightforward process

I'm currently using the ng-flow library to upload images to my server. After a user drags and drops an image, I can successfully retrieve the HTML 5 file image in my controller. However, when trying to download it to the server along with other parame ...

Transmit the latest HTML Dom using jQuery AJAX, then store it using PHP

I am attempting to save the current DOM of the document that I am working on to the server using jQuery. However, I am facing an issue when trying to send the DOM value to a PHP file which is responsible for handling the save operation. Below is the code s ...

What are the potential ways in which an html IMG tag can disrupt the functionality of this

For years, I have been using this form code without any issues. The code functions perfectly and all the tags are correct. However, whenever I include an image tag, it displays a failure message and prevents the mail function in PHP from working. The hea ...

Changing images based on location in Magento - a step-by-step guide

I am looking to update the search image in Magento. Currently, when searching in Magento, the image file used is btn_search.gif located in skin/frontend/default/default/images. The file /app/design/frontend/default/default/template/catalogsearch/form.mini. ...

Identify and handle exceptions from nested PHP scripts

When I run nested scripts using include() within a master script, I am wondering if it is possible to detect and log any errors that may occur during the execution of the nested script, even if they were caught in a try/catch block within the nested script ...

Using Laravel 5.1 to Redirect and Pass Get Parameters

I am currently troubleshooting a redirect issue in Laravel 5.1 that is leading to the following error NotFoundHttpException in RouteCollection.php line 161: The specific redirect I am attempting to accomplish is as follows: http://example.com/tool/view. ...

Merging an array of objects into a single object using PHP

I have been dealing with a persistent headache issue related to the following code snippet in CodeIgniter PHP active record: $this->db->select('*'); $this->db->from('orders'); $this->db->join('order_detail&a ...