Jump to content

Funky rtrim() quark


phporcaffeine

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!
Link to comment
https://forums.phpfreaks.com/topic/4801-funky-rtrim-quark/
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
Link to comment
https://forums.phpfreaks.com/topic/4801-funky-rtrim-quark/#findComment-16889
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
Link to comment
https://forums.phpfreaks.com/topic/4801-funky-rtrim-quark/#findComment-16890
Share on other sites

Archived

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

×
×
  • Create New...

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.