Jump to content

Archived

This topic is now archived and is closed to further replies.

phporcaffeine

Funky rtrim() quark

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

AAAAAAAAAAHHHHHHHHHHHHHHHH

it's the FREAKING space char " , " ISNOT "," AAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

I can't rtrim "," when ", " is what is at the end

Thanks for the help bro

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.