Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:16:04+00:00 2026-06-18T04:16:04+00:00

I am using PHP and MySQL and I have run into a small problem.

  • 0

I am using PHP and MySQL and I have run into a small problem. I have tried to research and figure out what to do, however, I just can’t figure it out. I would appreciate any help. Here is my problem…

I have 3 SQL tables:

Customers
    cID (primaryKey)
    email
    name
    password
    ...etc.

Products
    pID (primaryKey)
    Pname
    Ptype

CustomerRequests
    requestID(primarykey)
    cID (foreign key references customer table)
    pID (foreign key references products table)
    quantityRequested

I have an HTML form that I am processing with PHP. This is like the request form, so the user logs in and fills in the request form. The fields on this form are like:

selecting product name, selecting quantity etc. This is done for e.g. $quantity=$_POST['quantity']… so basically the form data is stored inside the variables.

Now, here is the part that I’m stuck on. How do I carry out a SQL INSERT query so that the database knows what the cID and pID are? I.e. What the foreign key values are?

For example:

$insert = mysql_query(INSERT INTO CustomerRequests VALUES ('THIS WOULD BE BLANK AS ITS AUTOINCREMENTED PROMARY KEY FOR REQUEST ID','HOW WOULD I GET THE cID AUTOMATICALLY HERE','HOW WOULD I GET THE pID HERE','".$quantity."'))

The query stores the user input into the database using an INSERT query. However, since the customerID and productID are foreign keys in a different table how do i write the PHP query?

Thanks for the help and please get back to me if you need further information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:16:05+00:00Added an answer on June 18, 2026 at 4:16 am

    I’m not sure why you think you have a problem. The cID should be known because the customer is logged in. The pID should be known because your form should be using a dropdown list of products with the pID being used as the value.

    Then it’s a simple matter of doing an INSERT using those values, i.e.

    <select id="product">
        <option value="pid001">Product One</option>
        <option value="pid002">Product Two</option>
        ... etc. ...
    </select>
    

    In other words, these values are all known before they hit your form.

    Don’t match product names with ID’s after the fact; that’s just adding extra work. If you have a value available then you should use it. Your form should already contain the association.

    Create the dropdown based on a SELECT query from your Products table. Use the pID for the value and the pName for the content. That way, the data sent in the form is the foreign key you need in your INSERT.

    <?php
        /* 
         * $arrProducts is the result from a query like
         * SELECT * FROM Products;
         */
        print '<select id="products">';
        foreach ($arrProducts as $arrProduct) {
            printf('<option value="%s">%s</option>', $arrProduct['pID'], $arrProduct['pName']);
        }
        print '</select>';
    ?>
    

    Similarly, when you log your user in, get their cID and insert it into the session.

    One other note, cease from using PHP’s mysql_ family of functions. These are deprecated and due to be removed from a future release. They are prone to SQL Injection. You should be using either mysqli_ or PDO in your application. Both offer parameterized queries and prepared statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a php/mySQL/ajax shoutbox and have run into a small snag. On
I've been building PHP tools with a MySQL backend. Using multi-curl I can run
I have run into a problem using PDO because an error was not caught.
using php and mysql I have two tables, a users table and a profiles
I am building a website using php and mysql.I have a login that is
Using PHP / MySQL all encoded up as UTF, we have recently had to
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
i have created a forum using php/mysql and users login through facebook using php
Assume that I have login to system using PHP + MySQL, then, after I
I am using php and mysql. I have a Database config file (db-config.php) which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.