I have a mysql_query where I need to strip the last comma behind the last item(s).
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_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.
EDIT Now that you code is edited. As you mentioned I don’t know the API but I do know one possible solution.
,is removed.Try:
The
preg_replacewill simply edit your query, so that any string that has,);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.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: