  | 
              
	              
	                
  
    
	 
        insert รูปลงตารางที่2 โดยอ้างอิง id ตารางที่1 แบบ  CreateElement ได้ไม่จำกัดจำนวนช่อง ไม่ได้ครับ รบกวนหน่อยครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                จากบทความนี้ครับ 
 
 
 
ผมจะใช้ CreateElement ได้ไม่จำกัดจำนวนช่อง เก็บรูปลงตาราง contents โดยอ้างอึง book_id จากตาราง book 
ตอนนี้ถ้าผมไม่ใส่ Loop if สามารถ insert ข้อมูลลงตาราง contents โดยอ้างอิงเลข book_id จากตางราง book มาเก็บลงใน img_id ของตาราง contents ได้แล้วครับ 
 
แต่ผมต้องการจะใช้ Loop ตามบทความ พอใส่ไปแล้ว ข้อมูล insert ลงตารางแรก แต่ไม่ insert ลงตาราง2 ครับ รบกวนช่วยดู code หน่อยครับ 
 
Sql 
Code (PHP) 
CREATE TABLE book
(
     book_id int(10) PRIMARY KEY,
     book_name varchar(100),
     book_author varchar(100),
     book_isbn varchar(100),
     book_call varchar(100),
     book_link varchar(100),
     book_cover varchar(100),
     book_detail varchar(100),
     book_lang varchar(100),
	book_type varchar(100)
);
CREATE TABLE contents
(
     contents_id int(10) PRIMARY KEY,
     img_id int(10),
     contents_img varchar(100),
     CONSTRAINT book_fk_contents FOREIGN KEY ( img_id ) REFERENCES book ( book_id )
);
 
 
 
 
 
Bookform.php 
Code (PHP) 
<meta http-equiv=Content-Type content="text/html; charset=tis-620">
    <title>ระบบจัดการเว็บไซต์</title>
  <script language="javascript">
	function fncCreateElement(){
		
	   var mySpan = document.getElementById('mySpan');
		
		var myElement1 = document.createElement('input');
		myElement1.setAttribute('type',"file");
		myElement1.setAttribute('img_id',"contents_img");
		//myElement1.setAttribute('id',"filUpload[]");
		mySpan.appendChild(myElement1);	   
		//*** Remove Element ***//
		/*
		var deleteEle = document.getElementById('txt1');
		mySpan.removeChild(deleteEle);
		*/
	   var myElement2 = document.createElement('<br>');
	   mySpan.appendChild(myElement2);
	}
</script>
<style type="text/css">
body,td,th {
	font-size: 14px;
	color: #36F;
}
</style>
<td colspan="3" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="50"> </td>
        <td><img src="images/icon/logo1.jpg" width="250" height="80" /></td>
        <td width="50"> </td>
      </tr>
      <tr>
        <td> </td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><fieldset>
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="10"> </td>
                  <td width="100" align="center" valign="top"><img src="images/icon/l_book.jpg" width="30" height="30" /></td>
                  <td width="100" align="center" valign="top"><a href="change_passwd.php"><img src="images/icon/l_admin.jpg" width="30" height="30" /></a></td>
                  <td width="100" align="center" valign="top"> </td>
                  <td width="100" align="center" valign="top"> </td>
                  <td align="center" valign="middle"> </td>
                  <td align="center" valign="middle"> </td>
                  <td width="100" align="center" valign="middle"><a href="index.php"><img src="images/delete-icon.png" width="30" height="30" /></a></td>
                  <td width="10"> </td>
                </tr>
                <tr>
                  <td> </td>
                  <td align="center" valign="bottom"><a href="bookform.php">เพิ่มหนังสือ</a></td>
                  <td align="center" valign="bottom"><a href="change_passwd.php">ผู้ดูแลระบบ</a></td>
                  <td align="center" valign="bottom"> </td>
                  <td align="center" valign="bottom"> </td>
                  <td align="center" valign="middle"> </td>
                  <td align="center" valign="middle"> </td>
                  <td align="center" valign="middle"><a href="index.php">ออกจากระบบ</a></td>
                  <td> </td>
                </tr>
              </table>
            </fieldset></td>
          </tr>
          <tr>
            <td> </td>
          </tr>
          <tr>
            <td><fieldset>
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="100"> </td>
                  <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="10"> </td>
                      <td><form action="insertsave.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td colspan="4" align="center"><img src="images/icon/l_book.jpg" alt="" width="30" height="30" />เพิ่มหนังสือ</td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">ชื่อหนังสือ  :</td>
                            <td width="733" height="25"><label for="book_name"></label>
                              <input name="book_name" type="text" id="book_name" size="30" /></td>
                            <td width="100"> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">ชื่อผู้แต่ง :</td>
                            <td height="25"><label for="book_author"></label>
                              <input name="book_author" type="text" id="book_author" size="30" /></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">รหัส ISBN :</td>
                            <td height="25"><label for="book_isbn"></label>
                              <input name="book_isbn" type="text" id="book_isbn" size="30" /></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">รหัส Call Number  :</td>
                            <td height="25"><label for="book_call"></label>
                              <input name="book_call" type="text" id="book_call" size="30" /></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td align="right"> </td>
                            <td height="25" align="right">ลิงค์  :</td>
                            <td height="25"><input name="book_link" type="text" id="book_link" size="53" /></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">หน้าปก  :</td>
                            <td height="25"><label for="book_cover"></label>
                              <input name="book_cover" type="file" id="book_cover" size="30" /></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right"><input name="btnButton" id="btnButton" type="button" value="+" onclick="JavaScript:fncCreateElement();" />
                              สารบัญ :</td>
                            <td height="25"><label for="contents_img"></label>
                              <span id="mySpan">
                              <label for="textfield"></label>
                              <label for="contents_img"></label>
                              <input type="file" name="contents_img" id="contents_img" />
                              </span></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">รายละเอียด  :</td>
                            <td width="733" height="25"><label for="book_detail"></label>
                              <textarea name="book_detail" cols="50" rows="10" id="book_detail"></textarea></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td width="121" height="25" align="right">ภาษา :</td>
                            <td height="25"><label for="book_lang"></label>
                              <select name="book_lang" id="book_lang">
                                <option value="THA">ภาษาไทย</option>
                                <option value="ENG">ภาษาอังกฤษ</option>
                                </select></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td align="right"> </td>
                            <td height="25" align="right">ชนิดหนังสือ :</td>
                            <td height="25"><label for="book_type"></label>
                              <select name="book_type" id="book_type">
                                <option value="นิยาย">นิยาย</option>
                                <option value="วารสาร">วารสาร</option>
                              </select>
                              <label for="textfield"></label>
                              <label for="contents_img"></label></td>
                            <td> </td>
                          </tr>
                          <tr>
                            <td width="100" align="right"> </td>
                            <td height="25" align="right"><label for="textfield3"></label>
                              <input type="reset" name="Reset" id="button" value="Reset" /></td>
                            <td height="25"><input type="submit" name="button2" id="button2" value="Submit" /></td>
                            <td> </td>
                          </tr>
                        </table>
                      </form></td>
                      <td width="10"> </td>
                    </tr>
                  </table></td>
                  <td width="100"> </td>
                </tr>
            </table>
            </fieldset></td>
          </tr>
          <tr>
            <td> </td>
          </tr>
        </table></td>
        <td> </td>
      </tr>
    </table>      <br /></td>
</tr>
</table>
 
 
Insertsave.php 
Code (PHP) 
<meta http-equiv=Content-Type content="text/html; charset=tis-620">
<html>
<head>
<title></title>
</head>
<body>
<?
	if(move_uploaded_file($_FILES["book_cover"]["tmp_name"],"myfile/".$_FILES["book_cover"]["name"]))
	{
		//*** Insert Record ***//
          include('.../include/config.inc.php');
		$strSQL = "INSERT INTO book";
		$strSQL .="(book_name,book_author,book_isbn,book_call,book_link,book_cover,book_detail,book_lang,book_type) VALUES 
		('".$_POST["book_name"]."',
		'".$_POST["book_author"]."',
		'".$_POST["book_isbn"]."',
		'".$_POST["book_call"]."',
		'".$_POST["book_link"]."',
		'".$_FILES["book_cover"]["name"]."',
		'".$_POST["book_detail"]."',
		'".$_POST["book_lang"]."',
		'".$_POST["book_type"]."'
		)";
		$objQuery = mysql_query($strSQL);
		for($i=0;$i<count($_FILES["contents_img"]["name"]);$i++)
	{
		if($_FILES["contents_img"]["name"][$i] != "")
		{
			if(move_uploaded_file($_FILES["contents_img"]["tmp_name"][$i],"myfile/".$_FILES["contents_img"]["name"][$i]))
			{
				$img_id = mysql_insert_id();
		$sql1="INSERT INTO contents 
		(img_id,contents_img)VALUES($img_id,'".$_FILES["contents_img"]["name"][$i]."')";
		$query=mysql_query($sql1);
		}
		}
	}
	}
?>
<script>
alert('Insert Completed, Thanks');
window.location='index2.php';
</script>
</body>
</html>
 
 
  Tag : PHP               
                        | 
           
          
            | 
			
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2012-09-29 10:07:57 | 
                      By :
                          relatedlove | 
                      View :
                          1671 | 
                      Reply :
                          4 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ได้แล้วครับ ลืมใส่ []  
 
ขอบคุณครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-10-01 07:55:51 | 
                        By :
                            relatedlove | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   Good job                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2012-10-01 17:03:10 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |