通过php从textarea字段的每一行插入数据库

通过php从textarea字段的每一行插入数据库,php,mysql,database,insert,textarea,Php,Mysql,Database,Insert,Textarea,如何从textarea字段在数据库中插入每一行都是数据库中的一个表 意味着我想通过php从textarea字段的每一行插入数据库 我的信息如下: Name | FullName | Age 和数据库表: id (auto insert), name, fullname, age 谢谢,拆分字符串,例如,拆分字符串,例如,我建议在行尾使用分隔符,然后使用explode()将其转换为数组,下面是您可以执行的操作 //Make sure your text have (,) comma at th

如何从textarea字段在数据库中插入每一行都是数据库中的一个表

意味着我想通过php从textarea字段的每一行插入数据库

我的信息如下:

Name | FullName | Age
和数据库表:

id (auto insert),
name,
fullname,
age

谢谢,拆分字符串,例如,拆分字符串,例如,我建议在行尾使用分隔符,然后使用explode()将其转换为数组,下面是您可以执行的操作

//Make sure your text have (,) comma at the end of every line
$text = 'My Name,
         My Full Name, 
         24';
//Convert it into an array
$text = explode(',', $text);
//fetch the value and assign it to variables then do an insert operation
//Use mysql_real_escape_string() to escape SQL injections.
$name = mysql_real_escape_string($text[0]);
$FullName = mysql_real_escape_string($text[1]);
$age = mysql_real_escape_string($text[2]);
然后您可以像这样创建查询

$query = "INSERT INTO persons(name, fullName, age) values($name, $fullName, $age)";
$result = mysql_query($query);

我建议在行尾使用分隔符,然后使用explode()将其转换为数组,下面是您可以做的

//Make sure your text have (,) comma at the end of every line
$text = 'My Name,
         My Full Name, 
         24';
//Convert it into an array
$text = explode(',', $text);
//fetch the value and assign it to variables then do an insert operation
//Use mysql_real_escape_string() to escape SQL injections.
$name = mysql_real_escape_string($text[0]);
$FullName = mysql_real_escape_string($text[1]);
$age = mysql_real_escape_string($text[2]);
然后您可以像这样创建查询

$query = "INSERT INTO persons(name, fullName, age) values($name, $fullName, $age)";
$result = mysql_query($query);
希望这有助于:

// assuming textarea name is $_POST['data']
$_POST['data'] = 'Jane | Jane Doe | 23
    zxc
sadas
 John | John Doe | 48
';

// Set and trim data 
$data = (array_key_exists('data', $_POST)) ? trim($_POST['data']) : '';

if ($data !== '') {
    $lines = explode("\n", $data);
    $sql = 'insert into table (name, fullname, age) values ';
    $sql_parts = array();

    foreach ($lines as $line) {
        $sql_part = array(
            'name' => null,
            'full_name' => null,
            'age' => null,
        );

        // If no divider found, skip this line
        if (strpos($line, ' | ') === false)
        {
            continue;
        }
        list($sql_part['name'], $sql_part['full_name'], $sql_part['age']) = explode(' | ', $line);

        // For the sake of this example
        // i will assume all fields are required
        // so let's check for that:
        foreach ($sql_part as $key => $item) {
            // Trim and sanitize the data:
            $item = mysql_real_escape_string(trim($item));

            // If any item is empty continue to the next line of data
            if ($item === '') {
                continue 2;
            }
            $sql_part[$key] = "'".$item."'";
        }
        $sql_parts[] = '('.implode(',', $sql_part).')';
    }

    if (empty($sql_parts) === false) {
        $sql .= implode(',', $sql_parts);
        mysql_query($sql);
    }
}
希望这有助于:

// assuming textarea name is $_POST['data']
$_POST['data'] = 'Jane | Jane Doe | 23
    zxc
sadas
 John | John Doe | 48
';

// Set and trim data 
$data = (array_key_exists('data', $_POST)) ? trim($_POST['data']) : '';

if ($data !== '') {
    $lines = explode("\n", $data);
    $sql = 'insert into table (name, fullname, age) values ';
    $sql_parts = array();

    foreach ($lines as $line) {
        $sql_part = array(
            'name' => null,
            'full_name' => null,
            'age' => null,
        );

        // If no divider found, skip this line
        if (strpos($line, ' | ') === false)
        {
            continue;
        }
        list($sql_part['name'], $sql_part['full_name'], $sql_part['age']) = explode(' | ', $line);

        // For the sake of this example
        // i will assume all fields are required
        // so let's check for that:
        foreach ($sql_part as $key => $item) {
            // Trim and sanitize the data:
            $item = mysql_real_escape_string(trim($item));

            // If any item is empty continue to the next line of data
            if ($item === '') {
                continue 2;
            }
            $sql_part[$key] = "'".$item."'";
        }
        $sql_parts[] = '('.implode(',', $sql_part).')';
    }

    if (empty($sql_parts) === false) {
        $sql .= implode(',', $sql_parts);
        mysql_query($sql);
    }
}

我会避免循环中的查询。如果知道这段代码,任何人都可以很容易地使运行它的系统过载。没有人关心注入?阅读本文:注射当然重要,但这并不是“做这件事的最佳选择是什么”,而是“如何分割数据并准备好插入数据库”。我会避免循环中的查询。如果知道这段代码,任何人都可以很容易地使运行它的系统过载。没有人关心注入?阅读以下内容:注射当然重要,但这并不是一个“最好的选择是什么”,而是一个更重要的“如何分割数据并准备好插入数据库”。如果您对任何人提供的解决方案感到满意,请不要忘了将其标记为已回答。如果您对任何人提供的解决方案感到满意,请不要忘记将其标记为已回答。否则没人会费心回答你。