2011-06-08 63 views
1

我是編程新手,請耐心等待...我使用MySQL/PHP創建應用程序,其中的一部分允許用戶從兩個下拉菜單中選擇位置和空間。有三個表格,房間,位置和room_location。我創建了一些表單,允許用戶添加房間到房間表和表單,以允許他們將位置添加到位置表 - 這些都可以正常工作,因爲輸入只是文本字段,用戶可以提交他們喜歡的任何文本。我的問題是第三個表格/表格(dropdown_form.php) - 這個表格有三個字段,id | room_name |地點名稱。PHP/FORM問題

我想用room_location表達到的是: 用戶將得到一個網頁,該網頁有一個下拉菜單,用於選擇location_name(源自位置表)和另一個下拉菜單,用於選擇room_name (它來自房間表)。從兩個下拉菜單中選擇後,用戶將提交這個房間和位置的組合,並且新條目將被添加到數據庫的room_location表中。

room_location表的示例

ID | location_name | room_name
1 |醫院1 | room1

2 |醫院1 | room2

3 |醫院1 | room3

4 | Hospital2 | room1

5 | Hospital2 | room2 etc

到目前爲止,我已經得到了用於顯示兩個下拉菜單的表單,但是當我選擇並提交時,我得到一個關於「PRIMARY」鍵的錯誤:「Error:Duplicate entry」「我包括在內爲了測試一些echo語句,看看發生了什麼,我現在越來越:

位置和房間名稱進入了

陣列([select_location] => [select_room] =>)

錯誤:重複條目''爲'PRIMARY'鍵' (重複條目似乎是NULL?所以不要認爲什麼事實際上是傳遞給process.php)

它似乎有錯誤(我認爲)與選擇應該是傳遞數據到process.php處理$ _POST語句。我已經包含了我的代碼的副本,任何幫助都將不勝感激。

dropdown_form.php

<html> 
    <head> 
    <title>Testing Dropdown form</title> 
    </head> 
    <body> 

<?php 
     //make connection to the database 
     mysql_connect ("localhost", "root", "password") or die ('Database connection ERROR: ' . mysql_error()); 
     mysql_select_db ("my_database"); 
?> 

<fieldset style="width:30%" align="center"><legend><b>Add New Location/Room Combination</b></legend> 
<table align="center" cellspacing="0" cellpadding="10" border="0"> 
<tr> 
    <td align="center" valign="middle"> 
     <b> Location Name: </b> 
    </td> 
    <td align="center" valign="middle"> 
     <b> Room Name: </b> 
    </td> 
</tr> 
<tr> 
    <td align="center" valign="middle"> 
     <!-- create form to submit data process.php --> 
     <form name="room_location_form" action="process.php" method="post"> 
     <select name="select_location">   

     <?php 
     //create and run a query that selects all the locations to create an options list 
     $loc_query = "SELECT location_name FROM location ORDER BY location_name"; 
     $loc_result = mysql_query($loc_query); 

     while ($row = mysql_fetch_array($loc_result, MYSQL_ASSOC)) 
     { 
     echo "<option value=$row[id]>$row[location_name]</option>"; 
     } 
     ?> 
     </select> 
    </td> 

    <td>   
     <?php 
     //create and run a query that selects all the rooms to create an options list 
     $room_query = "SELECT room_name FROM room_name ORDER BY room_name"; 
     $room_result = mysql_query($room_query); 
     ?> 

     <select name="select_room"> 

     <?php 
      while ($row = mysql_fetch_array($room_result, MYSQL_ASSOC)) 
      { 
      echo "<option value=$row[id]>$row[room_name]</option>"; 
      } 
     ?> 
     </select> 

    </td> 

    <!--Add a submit button --> 
    <td align="center" valign="middle"> 
     <input type="submit" value="Add Location/Room"> 
    </td> 

</tr> 

</form> 
</table> 
</fieldset> 

<br> 
<br> 
<hr width=50%> 
<br> 
<table border=1 align=center cellspacing=1> 
    <tr> 
     <th>Location Name</th> 
     <th>Room Name</th> 
    </tr> 

<?php 

//build query to display a list of all current locations and rooms 
$query = mysql_query("select * from room_location ORDER BY location_name, room_name"); 

    //return the array and loop through each row 
    while ($row = mysql_fetch_array($query)) { 
    $location_name = $row['location_name']; 
    $room_name = $row['room_name']; 

?> 

    <tr> 
     <th><?php echo $location_name;?></th> 
     <th><?php echo $room_name;?></th> 
    </tr> 

<?php } //this ends the loop 
?> 

process.php

<?php 
//make connection 
$conn = mysql_connect ("localhost", "root", "password"); 
if (!$conn) 
    { 
    die ('Database connection ERROR: ' . mysql_error()); 
    } 
$db = mysql_select_db ("my_database"); 

//Add new Location and Room Name combination - sent from form 
$sql="INSERT INTO room_location (location_name, room_name) VALUES (('$_POST[select_location]'), ('$_POST[select_room]'))"; 
    echo "Location and Room name entered was ", $_POST['select_location'], " & ", $_POST['select_room']; 
?> 
<br> <!-- Line break just to see what is being output when testing --> 
<?php 
print_r($_POST); //added for testing purposes to see what is being POSTed 
?> 
<br> <!-- Line break just to see what is being output when testing --> 
<?php 
if (!mysql_query($sql,$conn)) 
    { 
    die ('Error: ' . mysql_error()); 
    } 
    echo "Location and Room name entered was ", $_POST['select_location'], " & ", $_POST['select_room']; 

mysql_close($conn) 
?> 

***方式進行更新的建議* * 感謝您的建議,到目前爲止,我做了一些親謝謝大家。使用下面的代碼,我現在得到select_location輸出作爲location_id和select_room輸出作爲room_name_id而不是location_name和room_name - 任何想法我做錯了什麼傢伙?

dropdown_form.php

<?php 
    //make connection to the database 
      mysql_connect ("localhost", "root", "password") or die ('Database connection ERROR: ' . mysql_error()); 
      mysql_select_db ("my_database"); 
    ?> 

    <fieldset style="width:30%" align="center"><legend><b>Add New Location/Room Combination</b></legend> 
    <table align="center" cellspacing="0" cellpadding="10" border="0"> 
    <tr> 
     <td align="center" valign="middle"> 
      <b> Location Name: </b> 
     </td> 
     <td align="center" valign="middle"> 
      <b> Room Name: </b> 
     </td> 
    </tr> 
    <tr> 
     <td align="center" valign="middle"> 
      <!-- create form to submit data process.php --> 
      <form name="room_location_form" action="process.php" method="post"> 
      <select name="select_location">   

      <?php 
      //create and run a query that selects all the locations to create an options list 
      $loc_query = "SELECT location_id,location_name FROM location ORDER BY location_name"; 
      $loc_result = mysql_query($loc_query); 

      while ($row = mysql_fetch_array($loc_result, MYSQL_ASSOC)) 
      { 
      echo "<option value=\"".$row['location_id']."\">".$row['location_name']."</option>\n"; 
      } 
      ?> 
      </select> 
     </td> 

     <td>   
      <?php 
      //create and run a query that selects all the rooms to create an options list 
      $room_query = "SELECT room_name_id,room_name FROM room_name ORDER BY room_name"; 
      $room_result = mysql_query($room_query); 
      ?> 

      <select name="select_room"> 

      <?php 
       while ($row = mysql_fetch_array($room_result, MYSQL_ASSOC)) 
       { 
       echo "<option value=\"".$row['room_name_id']."\">".$row['room_name']."</option>\n"; 
         } 
      ?> 
      </select> 

     </td> 

     <!--Add a submit button --> 
     <td align="center" valign="middle"> 
      <input type="submit" value="Add Location/Room"> 
     </td> 

    </tr> 

    </form> 
    </table> 
    </fieldset> 

    <br> 
    <br> 
    <hr width=50%> 
    <br> 
    <table border=1 align=center cellspacing=1> 
     <tr> 
      <th>Location Name</th> 
      <th>Room Name</th> 
     </tr> 

    <?php 

    //build query to display a list of all current locations and rooms 
    $query = mysql_query("select * from room_location ORDER BY location_name, room_name"); 

     //return the array and loop through each row 
     while ($row = mysql_fetch_array($query)) { 
     $location_name = $row['location_name']; 
     $room_name = $row['room_name']; 

    ?> 

     <tr> 
      <th><?php echo $location_name;?></th> 
      <th><?php echo $room_name;?></th> 
     </tr> 

    <?php } //this ends the loop 
    ?> 

process.php

<?php 
    //make connection 
    $conn = mysql_connect ("localhost", "root", "password"); 
    if (!$conn) 
     { 
     die ('Database connection ERROR: ' . mysql_error()); 
     } 
    $db = mysql_select_db ("my_database"); 

     //Add new Location and Room Name combination - sent from form 
     $sql="INSERT INTO room_location (location_name, room_name) VALUES (('".$_POST['select_location']."'), ('".$_POST['select_room']."'))"; 

    ?> 
    <br> <!-- Line break just to see what is being output when testing --> 
    <?php 
    print_r($_POST); //added for testing purposes to see what is being POSTed 
    ?> 
    <br> <!-- Line break just to see what is being output when testing --> 
    <?php 
    if (!mysql_query($sql,$conn)) 
     { 
     die ('Error: ' . mysql_error()); 
     } 
    echo "Location and Room name entered was ", [$_POST['select_location']], " & ", $_POST['select_room']; 

    mysql_close($conn) 
    ?> 

回答

0

只是做筆記,因爲我假設你刪除這個帖子上快速目的的消毒,但要確保你在你的消毒$_POST針對SQL注入的變量。

隨着中說,嘗試用這個替換您的查詢:

$sql="INSERT INTO room_location (location_name, room_name) VALUES (('".$_POST['select_location']."'), ('".$_POST['select_room']."'))"; 

編輯:

看來我是有點晚了這一點,如攪拌機已經注意到它。

0

嘗試在數據庫中自動增量設置爲你的id字段

+0

嗨,不錯的location_room_id是自動遞增,是主鍵。 – Damo 2011-06-08 21:06:48

1

除了其他人所說,

在您SELECTS dropdown_form.php,讓你無法獲取的ID爲ID的下拉菜單

值的形式。

SELECT location_name FROM location ORDER BY location 
make it 
SELECT id,location_name FROM location ORDER BY location desc 

那麼你就可以訪問它想:

echo "<option value=\"".$row['id']."\">".$row['location_name']."</option>\n"; 

通知我也是它的格式正確

做的第二選擇相同..

將新行插入數據庫時​​,請確保該ID是主鍵並自動遞增。 查詢:

$sql='INSERT INTO room_location (`id`,`location_name`, `room_name`) VALUES ("","'.mysql_real_escape_string($_POST['select_location']).'", "'.mysql_real_escape_string($_POST['select_room']).'")'; 
+0

謝謝大家迄今爲止的幫助。 你是絕對正確的,主要問題是我沒有選擇任何id值,我已經添加了,但不幸的是,查詢不起作用,它在該行上拋出了一個sytax錯誤 - 使用你的SELECT語句和@拉莫尼拉莫尼斯查詢我現在能夠得到更多一點,我得到一個返回select_room和select_location的值,但它是它的id號而不是我所需要的名稱(插入語句失敗,因爲它需要的名稱不是id) - 希望這是有道理的。我將再次發佈完整更新的代碼作爲單獨的評論。 – Damo 2011-06-08 21:20:13

0

最後這個破解自己...不是最完美的解決方案,但它的工作原理:爲指針d感謝。

*更新到process.php * **

<?php 
    //This section retrieves the room name selected on dropdown_form.php page 
    $room_id = $_POST['select_room']; //temp variable to hold room_name_id 
    $query = mysql_query("SELECT room_name FROM room_name WHERE room_name_id = $room_id"); 
     while ($row = mysql_fetch_array($query)) 
     { 
     $room_name = $row['room_name']; 
     } 

    //This section retrieves the location name selected on add_room_location_form.php page 
    $location_id = $_POST['select_location']; //temp variable to hold location_name_id 
    $query = mysql_query("SELECT location_name FROM location WHERE location_id = $location_id"); 
     while ($row = mysql_fetch_array($query)) 
     { 
     $location_name = $row['location_name']; 
      } 

    //Insert room & location data held in $location_name & $room_name into room_location table 
    $sql="INSERT INTO room_location (location_name, room_name) VALUES (('$location_name'), ('$room_name'))"; 
    echo "Location and Room name entered was ", $location_name, " & ", $room_name; 

    //I tried using this INSERT below but it inserts the array indexes instead of elements to location_name, room_name. 
    //$sql="INSERT INTO room_location (location_name, room_name) VALUES (('".$_POST['select_location']."'), ('".$_POST['select_room']."'))";  
?>