0

I'm sorry if the questions is a bit confusing, tried to make it as clear as possible.
Basically, I have this code:

for($i = 0;$i < 5;$i++)
    {
        $query = mysql_query("INSERT INTO words (lecture, pol, ukr) VALUES ($lecture, $pol_text[$i], $ukr_text[$i])", $db) or die("Data not inserted");
    }

And I want it to insert 5 elements of 2 arrays - $pol_text and $ukr_text, but not to do it manually, is there any way I could do it somewhat automatic?
Thank you!

Edit: Not quite sure if this will help, but submitting more code, that checks if submit is pressed:

<?  
if (isset($_POST['submit']))
{
    if (isset($_POST['pol_text'])){$pol_text = $_POST['pol_text'];}
    if (isset($_POST['ukr_text'])){$ukr_text = $_POST['ukr_text'];}
    if (isset($_POST['lecture'])){$lecture = $_POST['lecture'];}

    for($i = 0;$i < 5;$i++)
    {
        $query = mysql_query("INSERT INTO words (lecture, pol, ukr) VALUES ($lecture, $pol_text[$i], $ukr_text[$i])", $db) or die("Data not inserted");
    }

    /* DATA NOT INSERTED BECAUSE CAN'T BE WITH $I INSIDE QUERY?!?!?!?'*/
    unset($_POST['submit']);
}
?>
3
  • Can you post some example data? Commented Jul 7, 2014 at 15:20
  • @mattes well, I have 5 text fields, each one has name=pol_text[] and name=pol_text[] respectively. I'm submitting a form, and thinking on how can I automate this. I suppose I can just write it all manually 5 times, but for future reference, what if I'll have more than 5 fields, I'd like to know possible ways to automate this, thanks! Commented Jul 7, 2014 at 15:26
  • 1
    the php variables in the 'VALUES' part of the statement need to enclosed in single quotes (') to convert the inserted values into 'literal values' unless the database columns are numeric. You may need to include the variable name in 'braces' ({}) to ensure accurate value substitution. i.e. '{$pol_text[$i]}'. Commented Jul 7, 2014 at 15:47

1 Answer 1

1

You can loop around with foreach, and make sure you escape the values within the query:-

<?  
if (isset($_POST['submit']))
{
    if (isset($_POST['pol_text'])){$pol_text = $_POST['pol_text'];}
    if (isset($_POST['ukr_text'])){$ukr_text = $_POST['ukr_text'];}
    if (isset($_POST['lecture'])){$lecture = $_POST['lecture'];}


    foreach($ukr_text AS $key=>$value)
    {
        $query = mysql_query("INSERT INTO words (lecture, pol, ukr) VALUES ('".mysql_real_escape_string($lecture)."', '".mysql_real_escape_string($pol_text[$key])."', '".mysql_real_escape_string($ukr_text[$key])."')", $db) or die("Data not inserted");
    }

    unset($_POST['submit']);
}
?>

Or modified to only do a single insert of multiple rows:-

<?  
if (isset($_POST['submit']))
{
    if (isset($_POST['pol_text'])){$pol_text = $_POST['pol_text'];}
    if (isset($_POST['ukr_text'])){$ukr_text = $_POST['ukr_text'];}
    if (isset($_POST['lecture'])){$lecture = $_POST['lecture'];}

    $ins_array = array();

    foreach($ukr_text AS $key=>$value)
    {
        $ins_array[] = "('".mysql_real_escape_string($lecture)."', '".mysql_real_escape_string($pol_text[$key])."', '".mysql_real_escape_string($ukr_text[$key])."')";
    }

    if (count($ins_array > 0))
    {
        $query = mysql_query("INSERT INTO words (lecture, pol, ukr) VALUES ".implode(",", $ins_array), $db) or die("Data not inserted");
    }

    unset($_POST['submit']);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

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.