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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.