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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:12:20+00:00 2026-06-04T21:12:20+00:00

Basically, I’d like to insert into table, getting values id from the same table,something

  • 0

Basically, I’d like to insert into table, getting values id from the same table,something like this

$sql = ("INSERT INTO commande (idfichecmd,idproduit,idclt,qte,datecmd)
(SELECT MAX(idfichecmd)+1 ,'1' AS idproduit ,'$idC' AS idclt ,'$fb' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'2' AS idproduit ,'$idC' AS idclt ,'$g' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'3' AS idproduit ,'$idC' AS idclt ,'$pi' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'4' AS idproduit ,'$idC' AS idclt ,'$ft' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'5' AS idproduit ,'$idC' AS idclt ,'$sd' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'6' AS idproduit ,'$idC' AS idclt ,'$ad' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'7' AS idproduit ,'$idC' AS idclt ,'$vs' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'8' AS idproduit ,'$idC' AS idclt ,'$wi' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'9' AS idproduit ,'$idC' AS idclt ,'$equip' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'10' AS idproduit ,'$idC' AS idclt ,'$fo' AS qte,'$date' FROM commande),
(SELECT MAX(idfichecmd) ,'11' AS idproduit ,'$idC' AS idclt ,'$fh' AS qte,'$date' FROM commande)")or die(mysql_error());
$result = mysql_query($sql) or die("Unable to add commande : ". mysql_error());   
mysql_close();

owever, this one does not work, anyone know why?

  • 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-04T21:12:22+00:00Added an answer on June 4, 2026 at 9:12 pm

    You could use UNION:

    INSERT INTO commande (idfichecmd, idproduit, idclt, qte, datecmd)
      SELECT MAX(idfichecmd)+1,  1, '$idC', '$fb'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  2, '$idC', '$g'    , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  3, '$idC', '$pi'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  4, '$idC', '$ft'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  5, '$idC', '$sd'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  6, '$idC', '$ad'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  7, '$idC', '$vs'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  8, '$idC', '$wi'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  ,  9, '$idC', '$equip', '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  , 10, '$idC', '$fo'   , '$date' FROM commande
    UNION ALL
      SELECT MAX(idfichecmd)  , 11, '$idC', '$fh'   , '$date' FROM commande
    

    But it might be easier to first obtain the MAX(idfichecmd) in PHP, then use INSERT ... VALUES (remembering to perform the operation in a transaction in order to preserve atomicity):

    INSERT INTO commande (idfichecmd, idproduit, idclut, qte, datecmd) VALUES
      ($id+1, 1, '$idC', '$fb', '$date'),
      ($id  , 2, '$idC', '$g' , '$date'),
      -- etc.
    

    Or, if you are merely looking to give each new record an incremented idfichecmd, consider making the column AUTO_INCREMENT (whereby MySQL will do all of this for you automatically):

    ALTER TABLE commande MODIFY idfichecmd INT AUTO_INCREMENT;
    INSERT INTO commande (idproduit, idclut, qte, datecmd) VALUES
      (1, '$idC', '$fb', '$date'),
      (2, '$idC', '$g' , '$date'),
      -- etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically from a database I am getting data that is formatted like this nameofproject101
Basically I'm attempting to add rows to a table, I need to do this
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically, I would like a brief explanation of how I can access a SQL
Basically I want to achieve this workflow: Checkout from repository on windows system (or
Basically I have a few functions that look like this: class MyClass { void
Basically, I want an Html helper (something like @Html.MyEditor(m => m.Property) ) to produce
Basically, I'd like to know if this code is okay, <body id=some_id <!--[if lt
Basically I have 2 SQL queries from 2 different databases and I am trying
Basically, what signal does '0' represent, because here I see SIGNAL numbers starting from

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.