简体   繁体   English

如何使用PHP删除mysql_query的最后一个逗号?

[英]How can I use PHP to strip the last comma of mysql_query?

I have a mysql_query where I need to strip the last comma behind the last item(s). 我有一个mysql_query,需要在最后一个项目后面去除最后一个逗号。

How can I do that? 我怎样才能做到这一点?

Here is the code snippet: 这是代码片段:

mysql_query('INSERT INTO `items` (itemName, itemDescription  ) VALUES ("' . $form_input0 . '","' . $form_input1 . '",);');

The part that gives the "form_input" is what I am concerned with 我关注的是提供“ form_input”的部分

"' . $form_input1 . '",

That trailing comma is what I need to strip ... 那末尾的逗号是我需要删除的...

Some folks have suggested that I edit the question and include all of the code I am working with. 有些人建议我编辑问题,并包括我正在使用的所有代码。 Maybe it will help ... 也许会有所帮助...

<?php

$host = '%id=server%';
$user = '%id=username%';
$password = '%id=password%';

$link = mysql_connect($host, $user, $password);

$selected = mysql_select_db('%id=database%', $link);

if(!isset($_POST['text-input']))

?>



    <form method="post">
        %slice%
        <input type="submit" value="Submit" />
    </form>
    %[if !edit]%



<?php
%[repeat items]%
$form_input%id=repeatIndex% =  $_POST['element-%id=repeatIndex%'] ;

%[endrepeat]%

mysql_query('INSERT INTO `%id=table%` (%[repeat items]%  %[endif]%%html="Edit Me"%%[if !edit]% %[endrepeat]% ) VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);');

?>
%[endif]%

As hookman has rightly mentioned in comments mysql extension is depreciated and should not be used. 正如胡克曼在评论中正确提到的那样, mysql扩展已弃用 ,不应使用。

EDIT Now that you code is edited. 编辑现在,您的代码已被编辑。 As you mentioned I don't know the API but I do know one possible solution. 正如您提到的, 我不知道API,但是我知道一种可能的解决方案。

  • Assign your query to a variable. 将查询分配给变量。
  • Replace the last part of the query so that the , is removed. 从而使更换查询的最后一部分,将被删除。

Try: 尝试:

...
%[endrepeat]%

$query = 'INSERT INTO `%id=table%` (%[repeat items]%  %[endif]%%html="Edit Me"%%[if !edit]% %[endrepeat]% ) VALUES (%[repeat items]%"' . $form_input%id=repeatIndex% . '",%[endrepeat]%);';
$query = preg_replace('/,\);$/',');',$query);
mysql_query($query);

....

The preg_replace will simply edit your query, so that any string that has ,); preg_replace将仅编辑您的查询,以便任何具有,);字符串,); at the end of it, will be replaced with ); 最后,将被替换为); . You can use that with other queries, just figure out how to transform them with RegEx. 您可以将其与其他查询一起使用,只需弄清楚如何使用RegEx进行转换即可。

I understand that your markup language (whatever it is that you use) has limitations that could prohibit you to do this utilizing it's standard functionality - in this case 'manually' (with regex or otherwise replace) editing the string is your only option (that or parsing it somehow). 我了解您的标记语言(无论您使用的是哪种语言)都有一些限制,可能会限制您利用其标准功能来执行此操作-在这种情况下,“手动”(使用正则表达式或其他方式替换)编辑字符串是您唯一的选择(或以某种方式解析它)。

Alternatively, this might work: 或者,这可能起作用:

...

%[repeat items]%
$values = array(
    mysql_real_escape_string($form_input%id=repeatIndex%),
    mysql_real_escape_string($form_input%id=repeatIndex%)
);
%[endrepeat]%
$query = 'INSERT INTO `%id=table%` (%[repeat items]%  %[endif]%%html="Edit Me"%%[if !edit]% %[endrepeat]% ) VALUES (' . "'" . implode("','", $values) . "')";

...
$sql = str_replace(',);', ');', $sql);

But i would do it like this: 但我会这样做:

$values = array(
    $form_input0,
    $form_input1
);
$sql = sprintf(
    'INSERT INTO `items` (itemName, itemDescription  ) VALUES ("%s")',
    implode('", "', $values)
);
$result = mysql_query($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM