PHP for循环将名称输入数据库的问题

时间:2013-12-06 05:12:05

标签: php mysql for-loop

我使用以下代码将提交的名称输入数据库。正确运行时,代码应捕获表单中提交的名称和其他信息,并在数据库中创建三个唯一条目。这不会发生。而是代码捕获三包中的姓氏并将其信息输入数据库。您可以在此处查看表单beta website。表单上禁用了付款处理脚本。我需要在for循环代码中更改什么才能解决此问题?非常感谢任何帮助。谢谢。 我添加了下面脚本中使用的所有代码。希望这能让您更好地了解脚本中发生的事情。

class DreamModelDream extends JModel {

function getDetails()
{
    $session = JFactory::getSession();
    if($session->get('dreamticket'))
    {
        return $session->get('dreamticket');
    }
    $data = new stdClass();
    $data->tickets = -1;
    $data->fiftytickets = '';
    $data->qty = 0;
    $data->fiftyqty = 0;
    $data->firstname = '';
    $data->firstname2 = '';
    $data->firstname3 = '';
    $data->lastname = '';
    $data->lastname2 = '';
    $data->lastname3 = '';
    $data->address = '';
    $data->address2 = '';
    $data->address3 = '';
    $data->city = '';
    $data->city2 = '';
    $data->city3 = '';
    $data->postal = '';
    $data->postal2 = '';
    $data->postal3 = '';
    $data->phone = '';
    $data->phone2 = '';
    $data->phone3 = '';
    $data->altphone = '';
    $data->altphone2 = '';
    $data->altphone3 = '';
    $data->email = '';
    $data->email2 = '';
    $data->email3 = '';
    $data->giftname = '';
    $data->giftaddress = '';
    $data->giftcity = '';
    $data->giftpostal = '';
    $data->sec_firstname = '';
    $data->sec_firstname2 = '';
    $data->sec_firstname3 = '';
    $data->sec_lastname = '';
    $data->sec_lastname2 = '';
    $data->sec_lastname3 = '';
    $data->agegroup = 0;
    $data->expm = 0;
    $data->expy = 0;
    $data->nameoncard = '';
    $data->cctype = '';
    $data->ccnum = '';
    $data->Media_Radio = false;
    $data->Media_TV = false;
    $data->Media_Newspaper = false;
    $data->Media_Mail = false;
    $data->Media_Web = false;
    $data->Media_Kinsmen_Member = false;
    $data->Media_Other = false;
    $data->Radio_CJCY = false;
    $data->Radio_MY96 = false;
    $data->Radio_ROCK = false;
    $data->Radio_CHAT = false;
    $data->Radio_POWER = false;
    $data->Radio_Other = false;
    $data->total = false;
    $data->billingphone = '';
    $data->agree = 0;
    $data->ord_type = 0;
    $data->creditcard = '';
    $data->user_ip = $_SERVER['REMOTE_ADDR'];
    return $data;
}

function getConfirmDetails()
{
    $post = JRequest::get('post');
    $ticket = new stdClass();
    foreach($post as $key => $value)
    {
        $ticket->$key = $value;
    }
    $session = JFactory::getSession();
    $session->set('dreamticket', $ticket);
    if(!strlen($post['firstname'])){
        return "Your first name is missing<br>";
    }
    if(!strlen($post['lastname'])){
        return "Your last name is missing<br>";
    }
    if(!strlen($post['address'])){
        return  "Your address is missing<br>";
    }
    if(!strlen($post['city'])){
        return  "Your city is missing<br>";
    }
    if(!strlen($post['postal'])){
        return  "Your postal code is missing<br>";
    }
    if (!preg_match("/^T\d\w\d\w\d$/i", $post['postal'])) {
        //return  "Your postal code is invalid for this province<br>";
    }   
    if(!strlen($post['phone'])){
        return  "Your phone number is missing<br>";
    }
    if(!strlen($post['email'])){
        return  "Your email is missing<br>";
    }
    if($post['tickets'] == '-1'){
    ////////// TICKET 2
        if(!strlen($post['firstname2'])){
            return  "Your first2 name is missing<br>";
        }
        if(!strlen($post['lastname2'])){
            return  "Your last2 name is missing<br>";
        }
        if(!strlen($post['address2'])){
            return  "Your address2 is missing<br>";
        }
        if(!strlen($post['city2'])){
            return  "Your city2 is missing<br>";
        }
        if(!strlen($post['postal2'])){
            return  "Your postal2 code is missing<br>";
        }
        if (!preg_match("/^T\d\w\d\w\d$/i", $post['postal2'])) {
            //return  "Your postal2 code is invalid for this province<br>";
        }   
        if(!strlen($post['phone2'])){
            return  "Your phone number2 is missing<br>";
        }
    /////////////Ticket 3
        if(!strlen($post['firstname3'])){
            return  "Your first name3 is missing<br>";
        }
        if(!strlen($post['lastname3'])){
            return  "Your last name3 is missing<br>";
        }
        if(!strlen($post['address3'])){
            return  "Your address3 is missing<br>";
        }
        if(!strlen($post['city3'])){
            return  "Your city3 is missing<br>";
        }
        if(!strlen($post['postal3'])){
            return  "Your postal code3 is missing<br>";
        }
        if (!preg_match("/^T\d\w\d\w\d$/i", $post['postal3'])) {
            //return  "Your postal code3 is invalid for this province<br>";
        }   
        if(!strlen($post['phone3'])){
            return  "Your phone number3 is missing<br>";
        }
    }   
    //////// END TICKET CHECK   

    if(!strlen($post['nameoncard'])){
        return  "Your Name on Credit Card is missing<br>";
    }
    if($post['cctype'] == "Please select one"){
        return  "Your Credit Card Type is missing<br>";
    }
    if(!strlen($post['ccnum'])){
        return  "Your Credit Card Number is missing<br>";
    }
    if(!strlen($post['billingphone'])){
        return  "Your billing phone number is missing<br>";
    }
    if(!strlen($post['agree'])){
        return  "Your must agree to the Lottery rules in order to proceed<br>";
    }
    return $ticket; 
}

function process()
{
    $user = JFactory::getUser();
    jimport('joomla.database.table');
    $params = JComponentHelper::getParams('com_dream');
    $session = JFactory::getSession();
    $data = $session->get('dreamticket');
    if(!is_object($data))
    {
        return false;
    }

    $dif = strtotime("-1 hour");
    $timestamp = date("F j, Y, g:i a",$dif);
    $ord_id = date('ymdHis') . rand(1000,9999);
    $ticket_total = (int) (($data->tickets == '-1') ? '250' : (int) $data->tickets * 100); 
    $fiftyticket_total = (int) (($data->fiftytickets == '0') ? '' : (int) $data->fiftytickets * 10);
    $ordertotal = $ticket_total + $fiftyticket_total;

    if(strlen($data->expm) == 1)
    {
        $data->expm = '0'.$data->expm;
    }
    if(strlen($data->expy) != 2)
    {
        $data->expy = substr($data->expy, 2, 2);
    }
    $data->total = $ordertotal;
    JTable::addIncludePath(JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_dream'.DS.'tables');
    $table = JTable::getInstance('Tickets', 'Table');
    $table->auth = $auth;
    $table->billingphone = $data->billingphone;
    $table->Media_Radio = isset($data->Media_Radio) ? 1 : 0;
    $table->Media_TV = isset($data->Media_TV) ? 1 : 0;
    $table->Media_Newspaper = isset($data->Media_Newspaper) ? 1 : 0;
    $table->Media_Mail = isset($data->Media_Mail) ? 1 : 0;
    $table->Media_Web = isset($data->Media_Web) ? 1 : 0;
    $table->Media_Kinsmen_Member = isset($data->Media_Kinsmen_Member) ? 1 : 0;
    $table->Media_Other = isset($data->Media_Other) ? 1 : 0;
    $table->Radio_CJCY = isset($data->Radio_CJCY) ? 1 : 0;
    $table->Radio_MY96 = isset($data->Radio_MY96) ? 1 : 0;
    $table->Radio_ROCK = isset($data->Radio_ROCK) ? 1 : 0;
    $table->Radio_CHAT = isset($data->Radio_CHAT) ? 1 : 0;
    $table->Radio_POWER = isset($data->Radio_POWER) ? 1 : 0;
    $table->Radio_Other = isset($data->Radio_Other) ? 1 : 0;
    $table->agegroup = $data->agegroup;
    $table->orderdate = date('Y-m-d H:i:s');
    $table->ip = $_SERVER['REMOTE_ADDR'];
    $table->ord_type = ($user->get('id') > 0) ? 'CallCentre' : 'online';
    $table->ord_id = $ord_id;
    if($data->tickets == '0') {
        $table->ticket_type = 'None';
    } elseif($data->tickets == '-1') {
        $table->ticket_type = '3Pack';
    } elseif($data->tickets == '1') {
        $table->ticket_type = '1ticket';
    } elseif($data->tickets == '5') {
        $table->ticket_type = '8tickets';
    }
    if($data->fiftytickets == '0') {
        $table->fiftyticket_type = 'None';
    } elseif($data->fiftytickets == '1') {
        $table->fiftyticket_type = '1ticket';
    } elseif($data->fiftytickets == '2') {
        $table->fiftyticket_type = '3tickets';
    }
    $table->province = 'AB';
    $table->creditcard = $data->cctype;
    if(isset($data->giftpurchase)) {
        $table->giftname = $data->giftname;
        $table->giftadress = $data->giftadress;
        $table->giftcity = $data->giftcity;
        $table->giftpostal = $data->giftpostal;
    }

    $data->ord_id = $ord_id;
    $tickets = 1;
    $table->qty = $data->tickets;
    if($data->tickets === '-1')
    {
        $tickets = 3;
        $table->qty = 3;
    } elseif($data->tickets === '1')
    {
        $tickets = 1;
        $table->qty = 1;
    } elseif($data->tickets === '5')
    {
        $tickets = 8;
        $table->qty = 8;
    }
    $threepack = '';
    $i = '';
    for($i = 0; $i < $tickets; $i++)
    {
        $firstname = 'firstname'.$threepack;
        $lastname = 'lastname'.$threepack;
        $address = 'address'.$threepack;
        $city = 'city'.$threepack;
        $postal = 'postal'.$threepack;
        $phone = 'phone'.$threepack;
        $altphone = 'altphone'.$threepack;
        $sec_firstname = 'sec_firstname'.$threepack;
        $sec_lastname = 'sec_lastname'.$threepack;
        $email = 'email'.$threepack;

        $table->firstname = $data->$firstname;
        $table->lastname = $data->$lastname;
        $table->address = $data->$address;
        $table->city = $data->$city;
        $table->postal = $data->$postal;
        $table->phone = $data->$phone;
        $table->altphone = $data->$altphone;
        $table->sec_firstname = $data->$sec_firstname;
        $table->sec_lastname = $data->$sec_lastname;
        $table->email = $data->$email;
        $table->id = 0;

        if($data->tickets === '-1' || $data->tickets === '5')
        {
            if($threepack == 2)
            {
                $threepack = 3;
            } else {
                $threepack = 2;
            }
        }
    }
    $fiftytickets = 1;
    $table->fiftyqty = $data->fiftytickets;
    if($data->fiftytickets === '1')
    {
        $fiftytickets = 1;
        $table->fiftyqty = 1;

    } elseif($data->fiftytickets === '2')
    {
        $fiftytickets = 3;
        $table->fiftyqty = 3;

    }
    $table->order_total = $data->total;
    $table->store();

    //sending confirmation mail
    $mailcontent = '';

2 个答案:

答案 0 :(得分:0)

for($i = 0; $i < $data->tickets; $i++)

答案 1 :(得分:0)

我已经找到了for循环的问题。需要将50个代码移动到for循环之上,并且需要将$ table-&gt; store()函数移动到for循环中并放在$ table-&gt; id行之后。现在,当购买故障单或故障单时,会在数据库中插入三个具有相同名称或唯一名称的条目。