phporcaffeine Posted March 13, 2006 Share Posted March 13, 2006 Any explanations?<?php$query_assm = "INSERT INTO shops ('name', 'location', 'manager', 'phone',";$query_assm = rtrim($query_assm, ",");echo $query_assm;//Still echos the trailing comma?>Am I running into a regex type of thing here?Do I need a ' \ ' deliminater?I am pulling my friggin' hair out and I am running out of Jolt! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 13, 2006 Share Posted March 13, 2006 I just tried your code. It works fine for me.How did you generate the string? I assume it's generated, since you wouldn't just create one with a trailing comma.Ken Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted March 13, 2006 Author Share Posted March 13, 2006 Here is the whole method, minus the method name:------P.S the "$get[]" is the $_GET array but I use a data cleaning method that puts $_GET into $get------[code]if ($get[0]['maintenancechg']) { $post = $_POST; array_pop($post); $assm = "INSERT INTO " . $get[1]['type'] . " ("; foreach ($post as $key => $value) { $assm .= "'$key', "; } $assm = rtrim($assm, ","); $assm .= ") VALUES ("; foreach ($post as $key => $value) { $assm .= "'$value', "; } $assm = rtrim($assm, ","); $assm .= ")"; echo $assm; die(); $sql_manager->__Construct("breakdown");[/code]AAAAAAAAAAHHHHHHHHHHHHHHHHit's the FREAKING space char " , " ISNOT "," AAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHI can't rtrim "," when ", " is what is at the endThanks for the help bro Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 13, 2006 Share Posted March 13, 2006 That's what I thought you were doing...Instead of[code]<?php $assm = "INSERT INTO " . $get[1]['type'] . " ("; foreach ($post as $key => $value) { $assm .= "'$key', "; } $assm = rtrim($assm, ","); $assm .= ") VALUES ("; foreach ($post as $key => $value) { $assm .= "'$value', "; } $assm = rtrim($assm, ","); $assm .= ")"; echo $assm;?>[/code]use a temporary array and the implode() function:[code]<?php $assm = "INSERT INTO " . $get[1]['type']; $tmp = array() foreach ($post as $key => $value) { $tmp[] = $key; } $assm .= "(`" . implode("`,`",$tmp) . "`) VALUES "; $tmp = array(); foreach ($post as $key => $value) { $tmp[] = mysql_real_escape_string($value); } $assm .= "('" . implode("','",$tmp) . "'); echo $assm;?>[/code]Or you can use the "set field = 'value'" variant:[code]<?php $assm = "INSERT INTO " . $get[1]['type'] . " SET "; $tmp = array() foreach ($post as $key => $value) { $tmp[] = "`" . $key . "` = '" . mysql_real_escape_string($value) . "'"; } $assm .= implode(',',$tmp); echo $assm;?>[/code]I usually use something similar to the latter example.Ken Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted March 13, 2006 Author Share Posted March 13, 2006 I like your way better because it's not a "Kentucky Hack", like my way. Your way leaves no doubt where my way is sort of like "damage control".-Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.