0

Hello, I need some help here. I cannot make the right query.

In my code, I want to insert a data into my database. But the id_number will have different values. Id_number have different values because its from a group. So with that group of id_number I want to insert an action, to every id_number belonging in a same group.
So I want my db table to look like this after inserting data.

 Actiondone || Date accomplish || idnumber  || complaintid
 ========== || =============== || ========= ||==========
 Done       || 2014-01-01      || 201010005 || 1
 ========== || =============== || ========= ||==========
 Done       || 2014-01-01      || 20101178  || 2
 ========== || =============== || ========= ||==========
 Done       || 2014-01-01      || 201010023 || 3

Here is what I have now.

<?php
    $con=mysqli_connect("localhost","root","root","sample6");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $sql="INSERT INTO admin_actions (action_done, date_accomplished, id_number, complaint_id)
          SELECT id_number AS id, complaint_id AS cid FROM complaints WHERE group_id=1
           VALUES('done','2014-01-16', 'id', 'cid')";

    if (!mysqli_query($con,$sql))
      {
      die('Error: ' . mysqli_error($con));
      }
    echo "1 record added";

    mysqli_close($con);


    ?>
2
Contributors
1
Reply
21
Views
3 Years
Discussion Span
Last Post by urtrivedi
1
$sql="INSERT INTO admin_actions (action_done, date_accomplished, id_number, complaint_id)   SELECT 'done', current_date(),id_number, complaint_id  FROM complaints WHERE group_id=1";
Votes + Comments
Thank you for your answer. It works! :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.