How do I insert an array of values into different columns of a mysql table?

后端 未结 3 887
滥情空心
滥情空心 2020-12-21 11:58

I am getting values in an array like this:

 Array
(
    [0] => English
    [1] => Arabic
)

I am having 2 columns in database like bel

相关标签:
3条回答
  • 2020-12-21 12:20

    If you want to insert new record you must give every column its value , not just language1, language2

    Maybe some columns has default values in DB, if it is, you can skip theme in sql statment.

    Try this:

    INSERT INTO table_name
    VALUES (value1, value2, value3,...)
    

    Or visit this page maybe help you: http://www.w3schools.com/php/php_mysql_insert.asp

    0 讨论(0)
  • 2020-12-21 12:20

    If you want your array values to be properly formatted, you could just use implode(). Consider this example:

    $values = array('English', 'Arabic');
    $statement = 'INSERT INTO contact (`language1`, `language2`) VALUES ("' . implode('", "', $values) . '")';
    echo $statement;
    

    Sample Output:

    INSERT INTO contact(`language1`, `language2`) VALUES ("English", "Arabic")
    

    Important Note: You must remember that column count must have the same count as the values inside your query or else it will not work (count of columns must match the number of elements inside the array or you will have a mismatch).

    0 讨论(0)
  • 2020-12-21 12:37
    <?php
    $languages = array(
    '0' => 'English',
    '1' => 'Arabic'
    );
    
    mysqli_query($connection,"INSERT INTO contact(lanuage1 ,language2) VALUES ('$languages[0]', '$languages[1]')");
    ?>
    
    0 讨论(0)
提交回复
热议问题