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

发布于 2024-11-13 07:22:36 字数 236 浏览 0 评论 0原文

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

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

我的信息如下:

Name | FullName | Age

和数据库表:

id (auto insert),
name,
fullname,
age

谢谢

How i can insert in database from textarea field that each line is a table in database

means I want insert in database from each line of textarea field by php

my information like this:

Name | FullName | Age

and database table:

id (auto insert),
name,
fullname,
age

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧 2024-11-20 07:22:36

我建议在行尾使用分隔符,然后使用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);

i would suggest using a delimiter at the end of the line and then convert it into an array using explode(), here is what you could do.

//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]);

and then you can create query like this.

$query = "INSERT INTO persons(name, fullName, age) values($name, $fullName, $age)";
$result = mysql_query($query);
山人契 2024-11-20 07:22:36
// assuming the text area value is in $_GET["text"]
$lines = explode("\n", $_GET["text"]);
foreach($lines as $line) {
    list($name, $fullName, $age) = explode(" | ", $line);
    mysql_query("INSERT INTO table (name, fullname, age) VALUES ('$name', '$fullName', $age)");
}
// assuming the text area value is in $_GET["text"]
$lines = explode("\n", $_GET["text"]);
foreach($lines as $line) {
    list($name, $fullName, $age) = explode(" | ", $line);
    mysql_query("INSERT INTO table (name, fullname, age) VALUES ('$name', '$fullName', $age)");
}
骑趴 2024-11-20 07:22:36

希望这有帮助:

// 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);
    }
}

Hope this helps:

// 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文