Solved

delete the last comma

Posted on 2013-01-17
5
392 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
Comment Utility
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
or try this

substr_replace

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

Expert Comment

by:Insoftservice
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
answered a small question and changed the way I insert into tables in the future

thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now