使用PHP更新/插入三个MySQL表

时间:2012-05-13 05:54:05

标签: php mysql insert

我有3张桌子:

  1. SMS_IN 带有“原始”号码(数据通过短信发送,因此所有号码都是以短信格式发送的:我正在'爆炸'将它们分成一个数字):

    sms_text
    sender_number
    sent_dt
  2. 会员手机号码
  3. members

    Memberid
    Username
    comp_id
    phone
  4. mobile_numbers_test 所有数字的表格:

    Memberid
    comp_id
    numbers
    contact_number
    active
  5. 我试图将SMS_IN(新号码)转换为mobile_numbers_test(旧数字),并进行2次检查:

    1. SMS_IN.sender_number = members.phone;和
    2. 如果SMS_IN.sender_number = mobile_numbers_test.contact_number AND SMS_IN.sms_text(以爆炸形式)= mobile_numbers_test.numbers,请跳过(或覆盖) - 否则INSERT
    3. 这是我到目前为止所做的:

      include ('database_connection.php');
      
      $result = mysql_query("SELECT phone FROM members")
      or die(mysql_error());  
      $no=0;
      echo'<table>
              <tr>
              <td>Numbers</th>
              <td>From</th>
              <td>Date</th>
              </tr>';
      while($row = mysql_fetch_array($result))
          {$no++;
          $phone= $row['phone'];  
          $comp_id =  $row['comp_id'];
          $result1 = mysql_query("SELECT * FROM SMS_IN where sender_number ='$phone'")
          or die(mysql_error()); 
          while($row = mysql_fetch_array($result1))
              {$no++;
              $sms= $row['sms_text'];
              $sms_no=$row['sender_number'];
              $sms_time=$row['sent_dt'];
              $sms_exp = explode("\r\n",$sms);
              foreach($sms_exp as $no )
              { //** not working from here **//
      
              $result2 = mysql_query("SELECT * FROM members,mobile_numbers_test,SMS_IN 
              where members.phone = SMS_IN.sender_number and 
              mobile_numbers_test.numbers != '$no'
              ")
              or die(mysql_error());
      
              while($row = mysql_fetch_array($result2))
              {
              $circle=$row['state'];
              $price='Call Us';
              $contact_person=$row['Username'];
              $contact_number=$row['phone'];
              $type='Fancy';
              $per_post='Pre-Paid';
              $provider=$sms_exp[0];
              $date=strtotime($sms_time);
              $ip=$sms_no;
              $old_no=$row['numbers'];;
      
              if($old_no!=$no){ 
                  echo '<tr><td>'.$no.'</td>';
                  echo '<td>'.$contact_person.'</td></tr>';
              } else { echo 'Old No';
              }
              }
              }
              }
      
              }       
          echo'</table>'; 
      

      在此示例中我echo答案,以便我知道会发生什么......我如何UPDATEINSERT

1 个答案:

答案 0 :(得分:1)

  1. UNIQUEmobile_numbers_test创建一个(contact_number, numbers)索引(如果尚未存在):

    ALTER TABLE mobile_numbers_test ADD UNIQUE INDEX (contact_number, numbers);
    
  2. 加入members加上SMS_IN加入条件:

    SELECT * FROM members JOIN SMS_IN ON SMS_IN.sender_number = members.phone
    
  3. 循环结果集,使用mobile_numbers_test扩展数字并插入INSERT IGNORE。把它们放在一起(使用PDO,因为它会让你的生活更轻松,你真的应该停止使用古老的mysql_*函数编写新的代码:它们不再被维护,社区已经开始了{{3} }):

    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $user, $pass);
    
    $select = $dbh->query("
      SELECT * FROM members JOIN SMS_IN ON SMS_IN.sender_number = members.phone
    ") or die($dbh->errorInfo());
    
    $insert = $dbh->prepare("
      INSERT IGNORE INTO mobile_numbers_test
               ( Memberid,  comp_id,  numbers,  contact_number)
        VALUES (:Memberid, :comp_id, :numbers, :contact_number)
    ");
    
    while($sms = $select->fetch()) {
      foreach(explode("\r\n",$sms['sms_text']) as $no) $insert->execute(array(
        'Memberid'       => $sms['Memberid'],
        'comp_id'        => $sms['comp_id'],
        'numbers'        => $no,
        'contact_number' => $sms['sender_number']
      ));
    }