Solved

delete the last comma

Posted on 2013-01-17
5
404 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
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 109

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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