Re: Inserting string - need to trim comma
| From: | John W. Holmes | Date: | Thu, 01 Apr 2004 19:21:46 +0000 |
| Subject: | Re: Inserting string - need to trim comma | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182067@lists.php.net to get a copy of this message | ||
From: "Matt Matijevich" <matijevich@alliancetechnologies.net>
> <snip>
> function insert($table, $data) {
> $sql = "INSERT INTO ".$table." SET ";
> foreach ($data as $k=>$v) {
> $sql.= $k." = '".$v."', ";
> }
> $sql.=";";
Take out the above line; you don't need to add the semi-colon
> $result = mysql_query($sql);
>
> }
>
> The only problem with the statement is the appended comma to each
> iteration - the statement generates a mysql error with the extra comma
> at
> the end of the statement. This seems like an easy fix -any ideas?
>
> Any help would be greatly appreciated...
> </snip>
>
> http://www.php.net/rtrim
rtrim() removes whitespace, not commas.
Use
$sql = substr($sql,0,-2);
to strip off the last two characters (comma and space).
---John Holmes...