2011-11-11 47 views
0

我有简单的形式:与MySQL数据库的数据记录PHP问题

<form action="add.php" method="post" > 
     <input name="name" maxlength="30"/><br/> 
     <textarea cols="80" name="description" rows="10"> 
     </textarea><br/> 

     <table> 
      <thead> 
        <tr> 
         <th></th> 
         <th width="200px">Shop list</th> 
        </tr> 
      </thead> 
      <tbody div class ="table"> 
      <?php 
      $result = mysql_query("SELECT shop_id, name FROM shop") or die(mysql_error()); 
      if(mysql_num_rows($result) > 0) { 
       while($row = mysql_fetch_assoc($result)) { 
       echo '<tr> 
         <td> 
         <input type="checkbox" name="identifer[]" value="'.$row['shop_id'].'" /> <br /></td> 
         <td>'.ucfirst($row['shop']).'</td> 
         </tr> ';  
                 } 
              } 
        ?> 

    </tbody></table> 
<input type="submit" value="Add"> 
</form> 

我要保存在数据库中的结果在两个不同的表:(book_id,名称,描述)和地方 。在的地方表我想保存你可以购买那本书的商店(places_id,book_id,shop_id)。

我有add.php文件(它不工作):

<?php 
    $name = $_POST['name']; 
    $description = $_POST['description']; 



     $identifer =($_POST['identifer']); 
     if (isset($identifer)){ 

      $id_arr = implode(',', $identifer); 
      $result = mysql_query("INSERT INTO places (places_id, book_id, shop_id) VALUES (NULL, NULL ($id_arr))") or die(mysql_error()); 

     } 




$q = "INSERT INTO baldas (book_id, name, description) VALUES (NULL, '$name', '$description')"; 

$result2 = mysql_query($q) or die(mysql_query()); 

    ?> 

我很困惑我怎么也得保存两个不同的表结果的同时,我也有问题,鉴定复选框被检查并将结果写入数据库,特别是我不知道如何处理book_id。 任何建议都会有所帮助。

回答

1

我不知道该如何处理book_id

你会第一值插入到表baldas,然后调用mysql_insert_id()

<?php 
    $q = "INSERT INTO baldas (book_id, name, description) VALUES (NULL, '$name', '$description')"; 
    $result = mysql_query($q) or die(mysql_query()); 

    $bookID = mysql_insert_id(); 

    // CODE TO INSERT INTO `places` TABLE USING $bookID // 
?> 

我有问题,鉴定复选框将被检查

你有他们的方式,你必须遍历数组$_POST['identifier']识别shop_id

<?php 
    foreach((array)$_POST['identifier'] as $shopID) { 
     $q2 = "INSERT INTO places (place_id, book_id, shop_id) VALUES (NULL, '$bookID', '$shopID')";  
     $result2 = mysql_query($q2) or die(mysql_error()); 
    } 
?> 

如果它不检查也不会在$_POST

+0

感谢您的快速响应。但我仍然可以做对。 :( 'foreach((array)$ _ POST ['identifier'] as $ shopID){ $ q2 =“INSERT INTO places(place_id,book_id,shop_id)VALUES(NULL,'$ bookID','$ shopID')“; \t $结果2 =请求mysql_query($ Q2)或死亡(的mysql_query());} ' 该代码给出了0的结果我错过了什么 – Lina

+0

您的模具陈述必须是'mysql_error'。?。也许这会给你带来你遇到的错误(因为从我们迄今为止的信息来看INSERT看起来不错) – DTest

0

我认为这可能是将ID设置为NULL,如果ID是自动递增(我想他们是)的问题。检查一下。

显示错误总是有帮助的。

0

包含所有的“地方”的表格,你可以买一个应该有:

  • place_id这是自动递增和“本地”到该表。 ID,唯一。
  • book_id是本书从书籍表的ID(在该双行会存在)
  • shop_id是从商店表店铺的ID(你没有提到)

会有一个排每个地方一本书可以买到。所以,你应该只需要一个查询,如果您添加的一个书就可以买到一个地方:

INSERT INTO places (book_id, shop_id) VALUES ($book_id, $shop_id) 

通知我并没有包括place_id,因为这应该是自动递增。

1

显示出来试试这个,希望可以帮助你:

<?php 
    $name = $_POST['name']; 
    $description = $_POST['description']; 
    $identifer[] =($_POST['identifer']); 
    $y = null; 
    if(count($identifer) > 1){ 
     foreach($identifer as $x){ 
      $y .= $x.","; 
     } 
     $val = rtrim($y,","); 
    } else { 
     $val = $identifer[0] 
    } 
    $result = mysql_query("INSERT INTO places (places_id, book_id, shop_id) VALUES (NULL, NULL, '$val')") or die(mysql_error()); 

    $q = "INSERT INTO baldas (book_id, name, description) VALUES (NULL, '$name', '$description')"; 

    $result2 = mysql_query($q) or die(mysql_query()); 
?> 

如果places_id上表baldas表的地方,book_id是AUTO_INCREMENT你可以尝试一下本作add.php

<?php 
    $name = $_POST['name']; 
    $description = $_POST['description']; 
    $identifer = $_POST['identifer']; 
    $shop_id = null; 
    if(isset($identifer)) $shop_id = implode(',', $identifer); 
    $q = "INSERT INTO baldas (name, description) VALUES ('$name', '$description')"; 
    $result = mysql_query($q) or die(mysql_query()); 
    $book_id = mysql_insert_id(); 
    $result2 = mysql_query("INSERT INTO places (book_id, shop_id) VALUES ('$book_id', '$shop_id')") or die(mysql_error()); 
?>