Solved

delete the last comma

Posted on 2013-01-17
5
412 Views
Last Modified: 2013-01-18
         $q = 'INSERT INTO '.$tablename.' (';
        foreach ($cols as &$columnname) {
          $q.=$columnname.',';
          }        
        $q = substr($q,'',-1);
        $q.=') VALUES (';
        foreach ($cols as &$columnname) {
        $q.='\''.mysql_real_escape_string($$columnname).'\',';        
        }
        $q = substr($q,'',-1);
        $q.=')';

echo $q;

Open in new window



this code worked with another page
but this line

$q = substr($q,'',-1);

is generating warning and there is no output

Warning: substr() expects parameter 2 to be long, string given in





I want to replace the last
,
with
nothing
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38791851
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38791853
or try this

substr_replace

$q = substr_replace($q,'',-1);
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 38791887
if (substr($string, -1, 1) == ',')
{
  $string = substr($string, 0, -1);
} // will check if last character is comma.

//if you want to remove all commas from the end of a line use the simpler:
$string = rtrim($string, ',');

//The rtrim function (and corresponding ltrim for left trim) is very useful as you can specify a range of characters to remove, i.e. to remove commas and trailing whitespace

$string = rtrim($string, ", \t\n");



// Please print value of $q before
echo "Before ".$q;
 $q = substr($q,'',-1);
        $q.=')';

echo "After =".$q;
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38792821
You should consider using array_map() and implode() to build query strings.  This will give you cleaner code.  Snippet is untested but probably valid in principle.  You might need to use backticks around column names, or not.

// COLUMN NAMES
$cnames = array
( 'name'
, 'email'
, 'phone'
)
;

// DATA TO BE INSERTED
$values = array
( "O'Reilly"
, "Bill.OReilly@Fox.com"
, "800-555-1212"
)
;

// ESCAPE THE DATA
$safe_values = array_map('mysql_real_escape_string', $values);

// BUILD THE QUERY PARTS
$cols = "`" . implode("`,`", $cnames)      . "`";
$vals = "'" . implode("','", $safe_values) . "'";

// BUILD THE QUERY STRING
$sql = "INSERT INTO $tablename ( $cols ) VALUES ( $vals )";

// SHOW THE QUERY
var_dump($sql);

Open in new window

Best regards, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 38792924
answered a small question and changed the way I insert into tables in the future

thanks
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question