1

i am getting data from a json file with curl and i want to add data id to my "tblcustomfieldsvalues" mysql table according to "tblclients" table. I am using mysqli_insert_id but i couldn't have success. When i add data its insert last id from tblclients to tblcustomfieldsvalues table. I believe i have to make an array to add them related table.

So, tblclients id(it's a primary key and autoincrement) value must be equal with $relid for each row. Here is the tblclients and tblcustomfieldsvalues codes for connect mysql from php.

$stmt = $mysqli->prepare("
    INSERT INTO tblclients(company,country,active,datecreated,default_currency,show_primary_contact,registration_confirmed,addedfrom)
    VALUES(?,?,?,?,?,?,?,?)
");


$stmt->bind_param("siisiiii",$title,$country,$active,$datecreated,$default_currency,$show_primary_contact,$registration_confirmed,$addedfrom);

$inserted_rows = 0;

foreach ($json['result'] as $product) {

    $title = $product['a'];
    $country = $product['b'];
    $active = $product['c'];
    $datecreated = $product['_date'];
    $default_currency = $product['d'];
    $show_primary_contact = $product['e'];
    $registration_confirmed = $product['f'];
    $addedfrom = $product['g'];

    $stmt->execute();
    $inserted_rows ++;
    
}

and

$stmt2 = $mysqli2->prepare("
    INSERT INTO tblcustomfieldsvalues(relid,fieldid,fieldto,value)
    VALUES(?,?,?,?)
");

$stmt2->bind_param("iiss", $relid,$fieldid,$fieldto,$value);
$inserted_rows = 0;


foreach ($json['result'] as $product) {

    $relid = mysqli_insert_id($mysqli);
    $fieldid = "1";
    $fieldto = "customers";
    $value = $product['z'];
    
    $stmt2->execute();
    $inserted_rows ++;
}

I am just new for coding, i made research and i couldn't figure that out. I might have other mistakes on my code. Please correct me if i am getting wrong with anything.

Thank you.

2
  • Can I suggest having the second loop inside the loop inserting the first set of rows. As you seem to understand the idea that the ID is the last row inserted, you could insert tho row to tblclients, get the ID (just once) and then insert the tblcustomfieldsvalues rows inside straight away. Commented Oct 16, 2022 at 13:59
  • Hello Nigel, thanks for reply. I did try that but i couldn't make possible what i thought. I might made wrong second loop and that is why it's not worked. Could you please help me that then i can create new loop in a loop? If you can give me example that will be awesome for me. Commented Oct 16, 2022 at 14:12

1 Answer 1

1

Please put the 2nd query below the 1st query within the loop (so that you can get the last insert id from $stmt->insert_id; and then use it in the 2nd query )

Just one foreach loop is enough, so the code is:

<?php

$stmt = $mysqli->prepare("
    INSERT INTO tblclients(company,country,active,datecreated,default_currency,show_primary_contact,registration_confirmed,addedfrom)
    VALUES(?,?,?,?,?,?,?,?)
");


$stmt->bind_param("siisiiii",$title,$country,$active,$datecreated,$default_currency,$show_primary_contact,$registration_confirmed,$addedfrom);

$stmt2 = $mysqli2->prepare("
    INSERT INTO tblcustomfieldsvalues(relid,fieldid,fieldto,value)
    VALUES(?,?,?,?)
");

$stmt2->bind_param("iiss", $relid,$fieldid,$fieldto,$value);
$inserted_rows = 0;


//$inserted_rows = 0;

foreach ($json['result'] as $product) {

    $title = $product['a'];
    $country = $product['b'];
    $active = $product['c'];
    $datecreated = $product['_date'];
    $default_currency = $product['d'];
    $show_primary_contact = $product['e'];
    $registration_confirmed = $product['f'];
    $addedfrom = $product['g'];

    $stmt->execute();

/////////////////

//  $relid = mysqli_insert_id($mysqli);
    $relid =$stmt->insert_id;

    $fieldid = "1";
    $fieldto = "customers";
    $value = $product['z'];
    
    $stmt2->execute();

    $inserted_rows++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Do you know any way to control if same $value data exist before? if yes can we update this data? if not can we continue write data? I have tried but i had failure. If you know any way can you please share this code with me? thank you very much
Also if $value data exist it s must be update tblclients too. i dont want to add extra data this table too.
you may change your query to insert into.... on duplicate key update ..... something like this SO post . But if you encounter further problems, please make a new question so that we can further advise. Thanks
Can you please help me with my other question? stackoverflow.com/questions/74091104/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.