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 7849707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:36:44+00:00 2026-06-02T18:36:44+00:00

i have a form where i post the data to mysql. the query should

  • 0

i have a form where i post the data to mysql. the query should insert the data from the form into table1, but also include data from another table2 where the ID that is send from the form is equal to the ID in table2?

i use the old mysql connection, i know, not the best 🙂 and php!

hope someone can help, thanks 🙂
Martin

think maybe I should give some more info 🙂

table1 is called: books

from the form, i have the following value: itemCode, itemQty, ownerID
i have 2 static value: status, type

the values from table2 that must be inserted into table1 is:
title, description, price, frontcover

from table2 the field isbn should be equal to itemCode from form.

here is what i have tried so far:

    $bookid=$_POST['itemCode'];
$itemQty=$_POST['itemQty'];
$status='2';
$ownerID = $user->id;

$query="INSERT INTO books (name, description, price, picture, status, ownerID, itemqty, type, studie, isbn) SELECT (title, description, price, frontcover FROM isbnbooks WHERE isbn=$itemCode), $status, $ownerID, $itemQty, '1', '1', $bookid)";

UPDATE:
I have also tried this one here:

$bookid=$_POST['itemCode'];
        $itemQty=$_POST['itemQty'];
        $status='2';
        $ownerID = $user->id;

        $data2 = mysql_fetch_array (mysql_query("SELECT * FROM isbnbooks WHERE isbn = $bookid"));
        $title = $data2[title];
        $description = $data2[description];
        $price = $data2[price];
        $picture = $data2[frontcover];

        $query="INSERT INTO books (name, description, price, picture, status, ownerID, itemqty, type, studie, isbn) 
        VALUES ($title, $description, $price, $picture, $status, $ownerID, $itemQty, '1', '1', $bookid)";
        mysql_query($query) or die("Opps some thing went wrong");
  • 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-02T18:36:45+00:00Added an answer on June 2, 2026 at 6:36 pm

    If you have values 'a' and 'b' to go into columns f1 and f2 of table1; and in f3 you want the value of table2.field where table2.id is 123, you can prepare and execute a SQL statement along these lines:

    INSERT INTO table1 (f1, f2, f3)
      SELECT 'a', 'b', field FROM table2 WHERE id = 123;
    

    Further to seeing the code in your updated question, the problem with your first attempt is that you’re trying to mix INSERT ... SELECT with INSERT ... VALUES; sadly they are mutually exclusive. However, you could write instead:

    INSERT INTO books (
      name,
      description,
      price,
      picture,
      status,
      ownerID,
      itemqty,
      type,
      studie,
      isbn
    )
      SELECT
        title,
        description,
        price,
        frontcover,
        :status,  -- use prepared statements to prevent SQL injection
        :ownerID, -- see http://bobby-tables.com/ for more info
        :itemQty,
        '1',      -- do you really want a string containing a number?
        '1',
        :bookid
      FROM isbnbooks
      WHERE isbn=:itemCode;
    

    Your second attempt looks as though it ought to work (although you really should use prepared statements, see above!); what problems are you having with it?

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

Sidebar

Related Questions

I have populated an html form with MySQL data from a table. I have
I have a form on which I POST the data with PHP. When the
I have written a form using the jQuery .post() function to post the data
I have a html form with the data by this post method 'form id='form1'
I have this HTML: <form action='uploadhandle.php' method='POST' enctype=multipart/form-data> <input type='file' class='fileinput' id='photo1' name='photo1'> <input
I use a form where i have listed the data from database like title,
When I try to enter data from a form I have made it adds
When I try to enter data from a form I have made it adds
I have a form I need to insert some information into one table. Then
I have following HTML table: <form method=post action=update-table.php> <table class=table-data style=width: 960px;> <thead> <tr>

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.