Welcome Guest, Not a member yet? Register   Sign In
str_replace does not work inside query
#1

[eluser]thefatladysingsopera[/eluser]
I am trying to replace some text that appears on a message like this


Code:
$combined_message = $t_message . ' ' . $t_signature;
$current_id = $this->ion_auth->get_user_id();
$findme   = '_names';
    $pos = strpos($combined_message, $findme);

    if ($pos === false) {
        echo "string not found";
        }

    else {
    $rowcount = $this->db->count_all('send_q');
    $query = $this->db->query("select sender from send_q");
    foreach ($query->result() as $row){
    $new_message = str_replace("_names",$row->sender,$combined_message);
    echo $new_message . "<br/>";//this echos all corectly

    /**
    Insert Into Outgoing Table
    */
    $this->db->query('INSERT INTO outgoing_sms (dest_msisdn,text_message,sender_name,service_id)
    SELECT receiver,"'.$new_message.'","'.$das_sender_name.'", "'.$das_service_id.'" FROM send_q');

     /**
     Insert Into Logs Table
     */
    $this->db->query('INSERT INTO all_sent_messages (the_receiver_name,the_receiver_number,the_message,the_status,the_user_id,the_timestamp)
     SELECT distinct sender,receiver,"'.$new_message.'","sent", "'.$current_id.'",now() FROM send_q');
     /**
     Delete From send queue where user_id = $user-id
     */
     $this->db->query('delete from send_q where user_id = "'.$current_id.'"');

    }
    }

When i output
Code:
$new_message
,all names i get from the database can be shown but not inserted into the database like i want to in the code.

What could be the reason for
Code:
$new_message
not working inside query?.
#2

[eluser]Tim Brownlaw[/eluser]
Code:
/**
    Insert Into Outgoing Table
    */
    $this->db->query('INSERT INTO outgoing_sms (dest_msisdn,text_message,sender_name,service_id)
    SELECT receiver,"'.$new_message.'","'.$das_sender_name.'", "'.$das_service_id.'" FROM send_q');

     /**
     Insert Into Logs Table
     */
    $this->db->query('INSERT INTO all_sent_messages (the_receiver_name,the_receiver_number,the_message,the_status,the_user_id,the_timestamp)
     SELECT distinct sender,receiver,"'.$new_message.'","sent", "'.$current_id.'",now() FROM send_q');

Well here's the thing... Can you tell me that these two insert statements actually work?
I don't know how you can tell that your $new_message is not working!

What is the SQL generated in both cases? Does it look right?

Can you take those generated SQL Statements and use them in say phpmyadmin and test them out?

You seriously need to read up on how to perform inserts and the like.

#3

[eluser]thefatladysingsopera[/eluser]
The two inserts actually work.The problem is i am copying rows from one table to another if you look carefully so the problem is not really me not knowing how insert works.
#4

[eluser]Tim Brownlaw[/eluser]
Ok I did a search on your Inserts and yes, that syntax is apparently correct! So my apologies on that issue.

I'll stick to one point each reply as you didn't take the time to answer the other questions.

What do you get when you change...
Code:
Insert Into Outgoing Table
    */
    $this->db->query('INSERT INTO outgoing_sms (dest_msisdn,text_message,sender_name,service_id)
    SELECT receiver,"'.$new_message.'","'.$das_sender_name.'", "'.$das_service_id.'" FROM send_q');
To
Code:
$sql = "INSERT INTO outgoing_sms (
         dest_msisdn,
         text_message,
         sender_name,
         service_id)
         SELECT
          receiver,
          $new_message,
          $das_sender_name,
          $das_service_id
            FROM send_q
       ";
    var_dump($sql); // Let's see what the actual SQL generated is!
//$this->db->query($sql); // Perform the Insert





Theme © iAndrew 2016 - Forum software by © MyBB