将简单列表插入MySQL表(php) [英] Inserting a simple list into MySQL table (php)

查看:107
本文介绍了将简单列表插入MySQL表(php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一番寻找解决方案后.我以为是时候问一下人们对stackoverflow的信任了.

After a bit of hunting around for a solution. I thought it was time to ask the brains trust here on stackoverflow.

我正在寻找一种以字符串形式获取列表的方法,使用逗号将其分割并插入到mySQL表的列中.

I'm looking for a way to take a list, in the form of a string, split it using commas and insert into columns in a mySQL table.

列表类似于:

  1. Tim Moltzen,2.Joel Reddy,3.Blake Ayshford,4.Chris Lawrence,5.James Tedesco,6.Benji Marshall,7.Braith Anasta,8.Aaron Woods,9. Robbie Farah,10. Jack Buchanan ,11. Bodene Thompson,12.利亚姆·富尔顿,13.亚当·布莱尔,14.班·默多克·马西拉,15. Ava Seumanufagai 16.玛特·贝尔,17.埃迪·佩蒂伯恩
  1. Tim Moltzen, 2. Joel Reddy, 3. Blake Ayshford, 4. Chris Lawrence, 5. James Tedesco, 6. Benji Marshall, 7. Braith Anasta, 8. Aaron Woods, 9. Robbie Farah, 10. Jack Buchanan, 11. Bodene Thompson, 12. Liam Fulton, 13. Adam Blair, 14. Ben Murdoch Masila, 15. Ava Seumanufagai 16. Matt Bell, 17. Eddy Pettybourne

从该列表中,需要拆分两件事.数字和名称.

From that list, two things are needed to be split. The number and the name.

因此,该数字将被插入"player_number"列中,而名称将被插入"player_name"中.当然,逗号仅用于间隔,而不必对表感兴趣.

So, the number would be inserted into the "player_number" column and the name into "player_name". Of course the commas are only used for spacing, and don't need to be interested into the table.

我们将非常感谢您提供有关如何拆分字符串然后将其插入表格的任何帮助.

Any assistance on how to split the string and then insert into the table would be extremely appreciated.

:

我将看到可以使用explode并运行循环将其插入表中的方法.

I'll see what I can work with using explode and running a loop to insert them into the table.

推荐答案

$string="1. Tim Moltzen, 2. Joel Reddy, 3. Blake Ayshford, 4. Chris Lawrence, 5. James Tedesco, 6. Benji Marshall, 7. Braith Anasta, 8. Aaron Woods, 9. Robbie Farah, 10. Jack Buchanan, 11. Bodene Thompson, 12. Liam Fulton, 13. Adam Blair, 14. Ben Murdoch Masila, 15. Ava Seumanufagai 16. Matt Bell, 17. Eddy Pettybourne";

$string=explode(', ',$string);
foreach($string as $val)
    {
    $val=explode('. ',$val);
    mysql_query('INSERT INTO yourtable (col_number,col_name) VALUES ("'.$val[0].'.","'.$val[1].'")';
    }

我不明白您为什么要在数字中插入句点,因为这意味着该列不必要地是varchar而不是INT.无论如何,这是您所要求的.

I don't understand why you want to insert the period along with the number, as this would mean that the column has to unnecessarily be varchar instead of INT. Anyway, it is as you asked.

根据需要将mysql_query更改为mysqli_query.

要在数字之间爆炸,请使用:

$string=preg_split('/ ?[0-9]+\.? /', $string, NULL, PREG_SPLIT_NO_EMPTY);

但是现在您的每个名字都没有数字了.因此,您将无法像这样插入它.

But now you don't have any numbers for each name. So you won't be able to insert it like this.

这篇关于将简单列表插入MySQL表(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆