如何为特定成员插入唯一数据

时间:2015-09-08 18:29:05

标签: php mysql

这是我在MYSQL中的表,我想为membership_id的contacts_member_id插入唯一记录,表示member_id = 1可以有多条记录,但它应该是唯一的,我在PHP页面插入,但编码不起作用正常。

    create table contact_viewed(
    contact_viewed_id int(11) not null auto_increment,
    contacted_member_id int (11) not null,
    member_id int(11) not null,
    primary key(contact_viewed_id)
    );

我的php文件如下所示

 <?php
    include_once 'dbconfig.php';
    if (! $user->is_loggedin ()) {
    $user->redirect ( 'login.php' );
    }
    $m_id = $_SESSION ['user_session'];
    $stmt = $DB_con->prepare ( "SELECT * FROM member WHERE member_id=:member_id" );
    $stmt->execute ( array (
    ":member_id" => $m_id 
    ) );
    $userRow = $stmt->fetch ( PDO::FETCH_ASSOC );
    ?>


    <?php
    $stmt7 = $DB_con->prepare ( "SELECT * FROM contact_viewed WHERE member_id=:member_id" );
    $stmt7->bindParam ( ":member_id", $m_id );
    $stmt7->execute ();
    $row2 = $stmt7->fetchAll ();
    if ($stmt7->rowCount () > 0) {
    foreach ( $row2 as $data ) {
     if ($data ['member_id'] == $m_id) {enter code here
     if($data ['contacted_member_id'] == $_GET['id']){
         echo "do nothing1";
     }
     else{
         $stmt6 = $DB_con->prepare ( "INSERT INTO contact_viewed(contacted_member_id,member_id) VALUES(:contacted_member_id,:member_id)" );
         $stmt6->bindParam ( ":contacted_member_id", $_GET ['id'] );
         $stmt6->bindParam ( ":member_id", $m_id );
         $stmt6->execute ();
         echo "hello";
     }
     } 
    } 
    }
    else{
    $stmt6 = $DB_con->prepare ( "INSERT INTO contact_viewed(contacted_member_id,member_id) VALUES(:contacted_member_id,:member_id)" );
    $stmt6->bindParam ( ":contacted_member_id", $_GET ['id'] );
    $stmt6->bindParam ( ":member_id", $m_id );
    $stmt6->execute ();
    $available_contact = $userRow ['available_contact'];
    $available_contact = $available_contact - 1;
    $stmt5 = $DB_con->prepare ( "UPDATE member SET available_contact=:available_contact WHERE member_id=:member_id" );
    $stmt5->bindParam ( ":available_contact", $available_contact );
    $stmt5->bindParam ( ":member_id", $m_id );
    $stmt5->execute ();
    echo "ok ok";
    }
    ?>
    <?php
    require 'script.php';
    ?>

    <!-- close all connection at the end of the page -->

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>

    <?php
    require 'css.php';
    ?>

    <?php
    require 'config.php';
    ?>

   <link rel="stylesheet" href="style.css" />
   <link rel="stylesheet" href="css/theme-inner.css" />
   </head>
   <body>
    <?php
    require 'nav.php';
    ?>

    <div align="center">
    <h1>Contact Detail</h1>
    </div>

    <section class="full-width-inner">
    <div class="container">
    <div class="row" style="margin: 0 auto;">
     <div class="col-lg-12 col-md-12" style="margin: 0 auto;">
     <h2 class="article-title">Profile</h2>
     <div class="row">
         <?php require 'leftNav.php';?>

    <?php

    if (isset ( $_GET ['id'] )) {
    $search = trim ( $_GET ['id'] );
    $result = mysqli_query ( $conn, "select * from member where member_id like '%" . $search . "%' Limit 1" );
    $rows = mysqli_num_rows ( $result );
    if ($rows) {
    while ( $post = mysqli_fetch_assoc ( $result ) ) {
     ?>




     <div class="col-sm-8 col-md-9 col-lg-9">
         <div class="panel panel-info light-pink">
          <div class="panel-heading action-box">
          <div class="panel-caption">
              <h3 class="panel-title"><?php print($post['member_name']); ?></h3>
          </div>
          <!-- panel-caption -->
          <div class="panel-tools">
              <a href="view_profiles_others.php?id=<?php echo $search ?>"
              class="btn btn-white-outline btn-xsmall">View Member Profile</a>
          </div>
          <!-- panel-tools -->

          <div class="panel-tools">
              <a href="view_contact.php?id=<?php echo $search ?>"
              class="btn btn-white-outline btn-xsmall">View Contact Deatil</a>
          </div>
          <!-- panel-tools -->
          </div>
          <!-- panel panel-info light-pink -->
          <div class="panel-body">

          <div class="row">
              <div class="col-md-4 col-lg-3">
              <a href="view_contact.php?id=<?php echo $search;?>"><img
               alt="Image Not Available"
               src="<?php echo $post['upload_image_path'];?>"
               class="thumbnail" /></a>
              </div>
              <!-- col-md-4 col-lg-3 -->
              <div class="col-md-8 ">
              <div class="profile-sumup">

               <div class="row">
               <table class="table table-condensed table-user-information">
                <tbody>
                <tr>
                    <td class="col-xs-4 col-sm-4">Name</td>
                    <td><span>:</span> <?php echo $post['member_name'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Date Of Birth</td>
                    <td><span>:</span> <?php echo $post['day'];?>/<?php echo $post['month'];?>/<?php echo $post['year'];?></td>
                </tr>
                    <?php
     $year = $userRow ['year'];
     $y = date ( "Y" );
     $age = $y - $year;
     ?>
                    <tr>
                    <td class="col-xs-4 col-sm-4">Age</td>
                    <td><span>:</span> <?php echo $age;?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Email</td>
                    <td><span>:</span> <?php echo $post['email'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Country</td>
                    <td><span>:</span> <?php echo $post['country'];?></td>
                </tr>

                <tr>
                    <td class="col-xs-4 col-sm-4">Residing State</td>
                    <td><span>:</span> <?php echo $post['residing_state'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">City</td>
                    <td><span>:</span> <?php echo $post['city'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Contact Address</td>
                    <td><span>:</span> <?php echo $post['contact_address'];?></td>
                </tr>

         <tr>
          <td class="col-xs-4 col-sm-4">Primary Mobile Number</td>
           <td><span>:</span> <?php echo $post['primary_mobile_number'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Alternate Mobile Number</td>
                    <td><span>:</span> <?php echo $post['alternate_mobile_number'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Citizenship</td>
                    <td><span>:</span> <?php echo $post['citizenship'];?></td>
                </tr>
                <tr>
                    <td class="col-xs-4 col-sm-4">Residing Status</td>
                    <td><span>:</span> <?php echo $post['residing_status'];?>  </td>
                </tr>
                </tbody>
               </table>
               <!-- table -->
               </div>
               <!-- /row -->
              </div>
              <!-- profile-sumup -->



              </div>
              <!-- col-md-8 -->

          </div>
          <!-- row -->

          </div>
          <!-- panel-body -->
         </div>
         <!-- col-sm-8 col-md-9 col-lg-9 -->

     <?php
    }
    } else {

    ?>
     <div class="col-sm-8 col-md-9 col-lg-9">
          <div class="panel panel-info light-pink">
          <div class="panel-heading action-box">
              <div class="panel-caption">
     <h3 class="panel-title"><?php echo "No Record Found In The  Database!"?></h3>
              </div>
              <!-- panel-caption -->
          </div>
          <!-- panel-heading action-box -->
          </div>
          <!-- panel panel-info light-pink -->



         </div>
         <!-- col-sm-8 col-md-9 col-lg-9 -->
              <?php
     }
     }

     ?>

         </div>
         <!-- row -->
     </div>
     <!-- col-lg-12 col-md-12 -->
     </div>
     <!-- row -->
     </div>
      <!-- container -->

     </section>
     <!-- full-width-inner -->

     </body>
     </html>

0 个答案:

没有答案