2013-09-26 116 views
1

我想從下拉式和文本框通過網頁插入值。值不插入到數據庫表

第一會有一個下拉框,其具有數從1到25 ..

當我從下拉數

,相應的文本框和下拉框被創建。

但是,當我輸入數據,然後單擊提交按鈕,值不會去數據庫,而只是最後一行值插入數據庫。

例如:

說,我已選擇4從編號下拉,現在4行與文本框和下拉框。

當我在文本框中輸入數據並從下拉列表中選擇值並單擊提交。最後一行值被插入4次。

我希望它能正確插入值...我該如何解決這個問題。

這裏是我使用的代碼..

代碼:的

<html> 
<head> 
<link rel="stylesheet" href="css/common.css" type="text/css"> 
<link rel="stylesheet" type="text/css" href="css/page.css"> 
<link rel="stylesheet" type="text/css" href="css/button.css"> 
<link href="css/loginmodule.css" rel="stylesheet" type="text/css" /> 

<script type="text/javascript" src="js/jquery.js"></script> 
<script type='text/javascript' src='js/jquery.autocomplete.js'></script> 
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" /> 

<script type="text/javascript"> 
$().ready(function() { 
    $("#Fname").autocomplete("get_course_list.php", { 
     width: 260, 
     matchContains: true, 
     //mustMatch: true, 
     //minChars: 0, 
     //multiple: true, 
     //highlight: false, 
     //multipleSeparator: ",", 
     selectFirst: false 
       }); 
       }); 
      </script> 

      <script> 
      function showUser(str) { 
       if(str=="") { 
        document.getElementById("txtHint").innerHTML=""; 
        return; 
       } 

       if(window.XMLHttpRequest) { 
        xmlhttp=new XMLHttpRequest(); 
       } else { 
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
       } 

       xmlhttp.onreadystatechange = function() { 
        if(xmlhttp.readyState==4 && xmlhttp.status==200) { 
         document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
        } 
       } 
       xmlhttp.open("GET","gettheater.php?q="+str,true); 
       xmlhttp.send(); 
       } 
     </script> 


     <script type="text/javascript"> 


    function create(param) { 
    'use strict'; 
    var i, target = document.getElementById('screens'); 
    target.innerHTML = ''; 
    target.innerHTML = '<input name="RowCount" value="' + param + '" hidden />'; 
    for(i = 0; i < param; i += 1) { 

     target.innerHTML +='</br>'; 
     target.innerHTML +='New Movie '+i+' '; 
     target.innerHTML += '<input type="text" name="Fname">'; 
     target.innerHTML +=' '+'Language '+' '; 
     target.innerHTML += "<?php 
     try { 
      $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger'); 
     } catch (PDOException $e) { 
      echo 'Connection failed: ' . $e->getMessage(); 
     } 

     $sql = 'SELECT language FROM languages;'; 

     $sth = $dbh->prepare($sql); 
     $sth->execute(); 

     echo "<select name='language' id='course'>"; 
     echo "<option>----Select Language----</option>"; 
     while($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
      echo "<option value='" . $row['language'] ."'>" . $row['language']. "  </option>"; 
     } 
     echo "</select>"; 
?>"; 

     target.innerHTML +='</br>'; 
     target.innerHTML +='</br>'; 
    } 
} 


</script> 

<style> 

#screens{ 
color:black; 
} 

</style> 



</head> 
<body> 


<h1>Welcome <?php echo $_SESSION['SESS_FIRST_NAME'];?></h1> 
<a href="member-profile.php" class="log">My Profile</a> | <a href="logout.php" class="log">Logout</a> 
<p>This is a password protected area only accessible to Admins. </p> 

<center> 



<div class="pan"><br><br> 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" autocomplete="off"> 

<div class="head">Update Theater Information</div> 
<table> 
<tr> 
<td><label for="screens">New Movies Released</label></td> 
<td> 

<select id="select" onchange='javascript:create(this.value);' name="range"> 
    <option>--Select Number Of New Movies Released--</option> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
    <option value="5">5</option> 
    <option value="6">6</option> 
    <option value="7">7</option> 
    <option value="8">8</option> 
    <option value="9">9</option> 
    <option value="10">10</option> 
    <option value="11">11</option> 
    <option value="12">12</option> 
    <option value="13">13</option> 
    <option value="14">14</option> 
    <option value="15">15</option> 
    <option value="16">16</option> 
    <option value="17">17</option> 
    <option value="18">18</option> 
    <option value="19">19</option> 
    <option value="20">20</option> 
</select> 

</td> 
</tr> 


<tr> 
<td></td> 
<td> 
<div id="screens"> 

</div> 
</td> 
<td></td> 
</tr> 

<tr> 
<td></td> 
<td><input type="submit" class="button" name='submit' value="Submit" /></td> 

</tr> 
</table> 
</form><br><br> 
</div> 

<?php 
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error()); 
mysql_select_db("theaterdb") or die(mysql_error()); 
for ($i=0; $i < $_POST["range"] ; $i++) 
{ 
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error()); 
} 
?> 

</center> 
</body> 
</html> 
+1

強烈建議使用'mysqli_ *',因爲mysql_ *'是折舊的。 –

回答

2

你應該改變你的名字,以陣列狀

target.innerHTML += '<input type="text" name="Fname[]">'; 

而且還插入查詢應該是

$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('".$_POST['Fname'][$i]."','".$_POST['language']."') ") or die(mysql_error()); 
+0

但是,當改變到您的修改....相當有效。但它僅向文件數據庫添加文本值,但不添加下拉值,只有最後一個下拉列表選定值正在增加特定次數。 – MintY

+0

使用'print_r'或'var_dump'打印您的'$ _POST'結果以顯示您從$ _POST'獲得的內容 –

+0

@Pandora,然後您必須更改回顯「