0

I have a function that inserts a load of values into a database. Not a problem but I have a condition that changes the mysql statement. The problem is is the way wordpress constructs the statement, below is what I've tried...

public function sqlInsertTicket($event, $customer, $ticket){

    $reference = 0;

    if ($customer['gender']=="F"){
        $table = "f_ti";
        $sql ='"ft_num"=>"'.$ticket['number'].'","booking_id"=>"'.$ticket['booking'].'"';
    }
    if ($customer['gender']=="M"){
        $table = "m_ti";
        $sql ='"ticket_num"=>"'.$ticket['number'].'","mbooking_id"=>"'.$ticket['booking'].'"';
    }
    $this->edb->insert(
                $table,
                array(
                        $sql,
                        'event_id'                  =>  $event['id'],
                        'original_ev'               =>  $event['id'],
                        'age'                       =>  $customer['age']
                    )
            );
    return $this->edb->insert_id;
}

I've removed a lot of the values but you can see what I'm trying to do. The $sql variable is what changes. I could write the whole thing out twice but, I've got lots of these to do and it would save a lot of time in the long run.

Wordpress is basically treating the $sql as 0 for the column name and doing crazy things with the values.

Thanks Joe

2 Answers 2

1

You are creating a string ....

$sql ='"ft_num"=>"'.$ticket['number'].'","booking_id"=>"'.$ticket['booking'].'"';

... but expecting it to operate as an array...

array(
    $sql,
    'event_id' =>  $event['id'],

This has nothing to do with how WordPress does anything. This is pure PHP. You have literally created this:

array(
    [] => "ft_num"=>"'.$ticket['number'].'","booking_id"=>"'.$ticket['booking'].'",
    'event_id' =>  $event['id'],

You need to build the array dynamically.

 $dynargs = array(
     'event_id'                  =>  $event['id'],
     'original_ev'               =>  $event['id'],
     'age'                       =>  $customer['age']
 )

if ($customer['gender']=="F"){
    $table = "f_ti";
    $dynargs['ft_num'] = $ticket['number'];
    $dynargs['booking_id'] = $ticket['booking'];
} elseif ($customer['gender']=="M"){
    $table = "m_ti";
    $dynargs['ticket_num'] = $ticket['number'];
    $dynargs['mbooking_id'] = $ticket['booking'];
}
$this->edb->insert(
            $table,
            $dynargs
        );
return $this->edb->insert_id;

I wrote that on the spot. It may be buggy but that is the idea.

1
  • I read through my question and had the same brainwave! But your getting the kudos! I've been awake for far too long, thank you!
    – user35683
    Jul 23, 2013 at 19:58
1

I'd say you should start with the fact that the two situations are exclusive, so either the gender is M or F.

if ($customer['gender']=="F"){
    $table = "f_ti";
    $sql ='"ft_num"=>"'.$ticket['number'].'","booking_id"=>"'.$ticket['booking'].'"';
} else if ($customer['gender']=="M"){
    $table = "m_ti";
    $sql ='"ticket_num"=>"'.$ticket['number'].'","mbooking_id"=>"'.$ticket['booking'].'"';
}
1
  • I'm confused, that is already the case?
    – user35683
    Jul 23, 2013 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.