PHP分割文本区域的内容并插入到mysql中

发布于 2024-12-26 10:14:08 字数 554 浏览 0 评论 0原文

我需要知道如何使用 php 编码执行以下操作请

  1. 从文本区域中获取插入的文本,
  2. 分割每行的内容
  3. 将每一行作为具有自己的 id 编号的自己的行插入数据库中

有人可以告诉我如何执行此操作吗?

谢谢

编辑________

这是我到目前为止所尝试过的

$is_first = true;


foreach (explode('\n', $_POST['code']) as &$voucher) {

    if ($is_first) $is_first = false; else $query .= ', ';

    $query .= '(' . mysql_real_escape($voucher) . ')';
    mysql_query('INSERT INTO back_codes (`code_id`, `code`) VALUES (NULL, "$voucher")');
}

I need to know how to do the following using php coding please

  1. take inserted text from a textarea
  2. split the content per line
  3. Insert each line into the database as its own row with own id number

Can anyone show me how to do this please?

thanks

EDIT________

THis is what I have tried so Far

$is_first = true;


foreach (explode('\n', $_POST['code']) as &$voucher) {

    if ($is_first) $is_first = false; else $query .= ', ';

    $query .= '(' . mysql_real_escape($voucher) . ')';
    mysql_query('INSERT INTO back_codes (`code_id`, `code`) VALUES (NULL, "$voucher")');
}

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

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

发布评论

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

评论(2

尴尬癌患者 2025-01-02 10:14:08
  1. 如果它是通过 POST 请求发送的,则使用 $_POST['field_name'] 引用它,如下所示:

    $textarea_value = $_POST['my_textarea'];
    
  2. 您可以使用 explode(),就像这样:

    $lines =explode("\n", $textarea_value);
    
  3. 迭代 explode() 的结果,然后将行逐一插入数据库。

    foreach ($lines as $line) {
        // 这里的$line是一个字符串
        // 基于它构建一个查询并执行它
    };
    
  1. Refer to it using $_POST['field_name'], if it has been sent with POST request, like that:

    $textarea_value = $_POST['my_textarea'];
    
  2. You can split strings using explode(), like that:

    $lines = explode("\n", $textarea_value);
    
  3. Iterate through result of explode() and then insert rows one-by-one to the database.

    foreach ($lines as $line) {
        // $line here is a string
        // build a query based on it and execute it
    };
    
南冥有猫 2025-01-02 10:14:08

我希望您通过提交 POST 表单数据从 textarea 发送数据。
假设您的文本区域如下所示:

<textarea name="data"></textarea>

那么在 PHP 中您可以使用以下代码:

if(isset($_POST["data"]))
{
  $line_data = explode("\n", $_POST["data"]);
  foreach($line_data as $key => $value)
{
  $sql = "INSERT INTO table (col) VALUE('{$value}')";
 mysql_query($sql);
}
}

I expect you sent data from textarea by submitting POST form data.
Let's say you textarea looks like this:

<textarea name="data"></textarea>

Then in PHP you can use following code:

if(isset($_POST["data"]))
{
  $line_data = explode("\n", $_POST["data"]);
  foreach($line_data as $key => $value)
{
  $sql = "INSERT INTO table (col) VALUE('{$value}')";
 mysql_query($sql);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文