Solved

delete the last comma

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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