-1

hey guys i have this problem.. basicly the first query is jsut for inserting and the 2nd query is for copying data from another table via foreign key. have any idea? im newbie.. :D

else if($payment_description == 'Monthly Subscription'){

$payment_amount = '750';
$sql = "INSERT INTO `paymentlog` (  payment_amount,payment_description,date_payment)
VALUES ( '$payment_amount', '$payment_description','$date_payment')";
$query_run = mysqli_query($conn, $sql);

$sql1 =  "INSERT INTO paymentlog (member_id, first_name, last_name)
SELECT member_id, first_name, last_name
FROM member
WHERE member_id = $id";
$query_run1 = mysqli_query($conn, $sql1);         

echo ("<script LANGUAGE='JavaScript'>
            window.alert('Monthly Payment is been added.');
            window.location.href='/PROJECT/MEMBERS/members.php';
            </script>");}
GMB
  • 216,147
  • 25
  • 84
  • 135
  • 2
    No. But you can use transactions. Also, before you take another step, see about the vital importance of using prepared and bound queries in order to prevent sql injection. – Strawberry Nov 13 '20 at 08:09
  • You could run a separate process for a parallel query. But that my not be a good idea. And again, use prepared statements to prevent SQL injection. – Markus Zeller Nov 13 '20 at 08:14
  • 1
    What is the reason for duplicating data from the `member` table into the `paymentlog` table, especially into a completely separate row from the payment details? At the moment, you're creating two rows in the same table, one with payment details and the other with member details. Is that what you want? – droopsnoot Nov 13 '20 at 08:32
  • 1
    You say you have this problem, but you don't tell us what the problem is. If it's that you insert two separate rows, you could combine your two INSERTs into a single query, if you _really_ have to copy the members data. Normally you'd just store the member-id in the paymentlog table, and use a JOIN to retrieve it when you need to. – droopsnoot Nov 13 '20 at 08:33
  • the scenario is if a member pay... the admin will input the amount, description etc... and that inputs will have the reference of the member via id... soo basicly it will just merge in a one row... im new in php sorry for confusion :D – Klint Cagz Nov 13 '20 at 08:38
  • Database design aside, I'm unsure of what the "execute two queries at the same time" part means or implies. What exact problem do you think that running queries sequentially can cause? – Álvaro González Nov 13 '20 at 08:40
  • my bad... i just dont know what is the correct way... – Klint Cagz Nov 13 '20 at 08:45
  • The "correct" way is to just store the payment details and the member id in the payments_log table, and you can retrieve the address details as required. There is usually no need to store the member name and address because you already have that in the members table. The only reason might be if you think you need to store the address at the time of the payment, in which case combine the two INSERT statements to insert your new data, and the data you retrieve from the members table. Experiment with the syntax in phpmyadmin before getting into PHP calling it. – droopsnoot Nov 13 '20 at 10:12

1 Answers1

2

I don't think your current code does what you want. You are (attempting to) insert two rows, while, as I understand your question, you want to create a single row in payment_log, with the amount, description and date given as input, and member information that needs to be retrieved from another table using another input paramter.

You can use the insert ... select syntax:

INSERT INTO `paymentlog` ( 
    member_id, 
    first_name, 
    last_name, 
    payment_amount,
    payment_description,
    date_payment
)
SELECT 
    member_id, 
    first_name, 
    last_name, 
    :payment_amount, 
    :payment_description,
    :date_payment
FROM member
WHERE member_id = :id

Important notes:

  • Use prepared statements! Do not concatenate variables in the query string, this is both inefficient and unsafe. Recommended reading: How can I prevent SQL injection in PHP

  • From a database design standpoint, you should not be duplicating information from table members in table payment_log; storing a reference to the primary key of member is sufficient

GMB
  • 216,147
  • 25
  • 84
  • 135