2015-10-05 122 views
0

這是我正在嘗試做的事情。我有一個下拉式(和/或Excel類型的外觀),我需要從數據庫中提取數據。當我選擇一個項目時,我需要通過會話將信息傳遞到下一頁。我有從數據庫拉下拉,但我需要在下拉列表中列出名稱和地址。從形式傳遞變量到下一頁,並將其放入sql語句php

如果這是不可能的,我可以做一個搜索項目的列表,但我需要使id成爲一個鏈接,將id傳遞到我需要構建報告的下一頁。

如果你可以給我一些這方面的信息,我將不勝感激。

以下是我對代碼的下拉

代碼:選擇所有

<?php 
// declare database connection variables. 
$host = "localhost"; 
$username = "root"; 
$password = ""; 
$db_name = "sample"; 
$tbl_name = "tbl_report"; 

// connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

$sql = "SELECT id, name, address FROM $tbl_name"; 
$result = mysql_query($sql) or die(mysql_error()); 

$dropdown = "<form action='report.php' method='post'>"; 

$dropdown .= "<select name='items' class='select'>"; 

while ($row = mysql_fetch_assoc($result)) { 
$dropdown .= '<option value="' . $row['id'] . '">' . 
$row['name'] . ' - ' . $row['address'] . '</option>';; 
} 
$dropdown .= "\r\n</select>"; 
$dropdown .= "<input type='submit' name='submit' value='Submit'>"; 
$dropdown .= "</form>"; //closing the form tag 
echo $dropdown; 
?> 

我需要做的是通過ID到下一個頁面,並有$ ID顯示在我的下一個sql語句的代碼。

這裏是我對下一頁sql語句的代碼。

select * from $tbl_name where id = (this is where I get hung up on.) 

我需要的是在$行[「身份證」]傳遞到下一個頁面,並放進了SQL代碼,然後使用get方法來顯示該頁面的表單上的信息的方式。

這裏是頁面的設置。 select-report.php(這是下拉的地方) - > report.php。

這裏是report.php的形式,我需要繼承id的值,並執行sql的select * from $ tbl_name,其中id = [$ post = items];然後,我需要使用$得到擺在下面的表格中的所有項目:

<form id="form1" name="form1" method="post" action=""> 
<table width="100%" border="0" cellspacing="4" cellpadding="4"> 
<tr> 
<td width="20%">Water Purveyor 
<input name="water-purveyor" type="text" id="water-purveyor" size="35" /> 
</td> 
<td colspan="2">Facility Contact 
<input type="text" name="facility-contact" id="facility-contact" /></td> 
<td colspan="2">Facility Address 
<input type="text" name="facility-address" id="facility-address" /></td> 
</tr> 
<tr> 
<td>Manufacturer 
<input type="text" name="manufacturer" id="manufacturer" /></td> 
<td width="20%">State 
<select name="state" id="state"> 
</select></td> 
<td width="20%">Model # 
<input name="model-num" type="text" id="model-num" size="10" /></td> 
<td width="20%">Serial # 
<input name="serial-num" type="text" id="serial-num" size="10" /></td> 
<td width="20%">Meter # 
<input name="meter-num" type="text" id="meter-num" size="10" /></td> 
</tr> 
<tr> 
<td>Owner 
<input type="text" name="owner" id="owner" /></td> 
<td>Phone # 
<input name="phone-num" type="text" id="phone-num" size="12" /></td> 
<td>Caged 
<input type="checkbox" name="caged" id="caged" /></td> 
<td>Locked 
<input type="checkbox" name="locked" id="locked" /></td> 
<td>Code key 
<input name="code-key" type="text" id="code-key" size="6" /></td> 
</tr> 
<tr> 
<td colspan="2">Owner Address 
<input name="owner-address" type="text" id="owner-address" size="50" /></td> 
<td>Owner City 
<input type="text" name="owner-city" id="owner-city" /></td> 
<td>Owner State 
<select name="owner-state" id="owner-state"> 
</select></td> 
<td>Owner Zip 
<input name="owner-zip" type="text" id="owner-zip" size="10" /></td> 
</tr> 
<tr> 
<td>Contact 
<input type="text" name="contact" id="contact" /></td> 
<td>Contact Phone 
<input name="contact-phone" type="text" id="contact-phone" size="10" /></td> 
<td>&nbsp;</td> 
<td>Other</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="2">Facility Name 
<input name="facility-name" type="text" id="facility-name" size="40" /></td> 
<td colspan="3">Assembly Address 
<input name="assembly-address" type="text" id="assembly-address" size="75" /></td> 
</tr> 
<tr> 
<td colspan="5">Onsite Location 
<input name="onsite-location" type="text" id="onsite-location" size="75" /></td> 
</tr> 
<tr> 
<td colspan="5">email Address 
<input name="email" type="text" id="email" size="125" /></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>SHUTOFF VALVES</td> 
<td colspan="2"><table width="100%" border="0" cellspacing="4" cellpadding="4"> 
<tr> 
<td>N/A</td> 
<td>Good</td> 
<td>Poor</td> 
<td>Fail</td> 
</tr> 
</table></td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Valve 1</td> 
<td colspan="2"><table width="100%" border="0" cellspacing="4" cellpadding="4"> 
<tr> 
<td><input type="checkbox" name="v1-na" id="v1-na" /></td> 
<td><input type="checkbox" name="v1-good" id="v1-good" /></td> 
<td><input type="checkbox" name="v1-poor" id="v1-poor" /></td> 
<td><input type="checkbox" name="v1-fail" id="v1-fail" /></td> 
</tr> 
</table></td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Valve 2</td> 
<td colspan="2"><table width="100%" border="0" cellspacing="4" cellpadding="4"> 
<tr> 
<td><input type="checkbox" name="v2-na" id="v2-na" /></td> 
<td><input type="checkbox" name="v2-good" id="v2-good" /></td> 
<td><input type="checkbox" name="v2-poor" id="v2-poor" /></td> 
<td><input type="checkbox" name="v2-fail" id="v2-fail" /></td> 
</tr> 
</table></td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Assembly Replacement? 
<input type="checkbox" name="assembly-replacement" id="assembly-replacement" /></td> 
<td>if yes Serial # 
<input type="text" name="assembly-replacement-serial" id="assembly-replacement-serial" /></td> 
<td>New Installation 
<input type="checkbox" name="new-install" id="new-install" /></td> 
<td>Permit # 
<input type="text" name="permit-num" id="permit-num" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Containment 
<input type="checkbox" name="containment" id="containment" /></td> 
<td>Isolation 
<input type="checkbox" name="isolation" id="isolation" /></td> 
<td colspan="2">Assembly Ground Clearance 
<input type="text" name="assembly-ground-clearance" id="assembly-ground-clearance" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Domestic 
<input type="checkbox" name="domestic" id="domestic" /></td> 
<td>Irrigation 
<input type="checkbox" name="irrigation" id="irrigation" /></td> 
<td>Fire Line 
<input type="checkbox" name="fire-line" id="fire-line" /></td> 
<td>Alarmed Fire Line 
<input type="checkbox" name="alarmed-fire-line" id="alarmed-fire-line" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="2"><strong><em><u>Double Check Balve Assembly Repair</u></em></strong></td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Repairs</td> 
<td>Check Valve 1</td> 
<td>&nbsp;</td> 
<td>Check Valve 2</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td rowspan="3">&nbsp;</td> 
<td>Cleaned 
<input type="checkbox" name="cv1-repair-cleaned" id="cv1-repair-cleaned" /></td> 
<td>&nbsp;</td> 
<td>Cleaned 
<input type="checkbox" name="cv2-repair-cleaned" id="cv2-repair-cleaned" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td><strong>Replaced</strong></td> 
<td>&nbsp;</td> 
<td><strong>Replaced</strong></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>Rubber Kit 
<input type="checkbox" name="cv1-rubber-kit" id="cv1-rubber-kit" /></td> 
<td>&nbsp;</td> 
<td>Rubber Kit 
<input type="checkbox" name="cv2-rubber-kit" id="cv2-rubber-kit" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>Other 
<input type="checkbox" name="cv1-other" id="cv1-other" /></td> 
<td>&nbsp;</td> 
<td>Other 
<input type="checkbox" name="cv2-other" id="cv2-other" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td colspan="2">Comments  
<textarea name="cv1-other-comments" id="cv1-other-comments" cols="45" rows="5"></textarea></td> 
<td colspan="2">Comments 
<textarea name="cv2-other-comments" id="cv2-other-comments" cols="45" rows="5"></textarea></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="2">Initial Test By  
<input name="initial-test-by" type="text" id="initial-test-by" size="50" /></td> 
<td colspan="2">Certified Tester # 
<input type="text" name="certified-tester-num" id="certified-tester-num" /></td> 
<td>Test Date/Time 
<input type="text" name="test-date-time" id="test-date-time" /></td> 
</tr> 
<tr> 
<td>Pass  
<input type="checkbox" name="initial-test-pas" id="initial-test-pas" /></td> 
<td>Fail 
<input type="checkbox" name="initial-test-fail" id="initial-test-fail" /></td> 
<td colspan="2">Calib Expire Date 
<input type="text" name="calib-expire-date" id="calib-expire-date" /></td> 
<td>Guage Serial # 
<input type="text" name="gague-serial-num" id="gague-serial-num" /></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="2">Repair Test By 
<input name="repair-test-by" type="text" id="repair-test-by" size="50" /></td> 
<td colspan="2">Repair Date/Time 
<input type="text" name="repair-date-time" id="repair-date-time" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="2">Final Test by 
<input name="final-test-by" type="text" id="final-test-by" size="50" /></td> 
<td colspan="2">Certified Tester # 
<input type="text" name="ft-certified-tester-num" id="ft-certified-tester-num" /></td> 
<td>Test Date/Time 
<input type="text" name="ft-date-time" id="ft-date-time" /></td> 
</tr> 
<tr> 
<td>Pass 
<input type="checkbox" name="ft-pass" id="ft-pass" /></td> 
<td>Fail 
<input type="checkbox" name="ft-fail" id="ft-fail" /></td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="5">Comments 
<textarea name="comments" id="comments" cols="80" rows="5"></textarea></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td colspan="2">Video upload <input type="file" name="uploadvideo" /></td> 
<td colspan="2">Image 1 <input type="file" name="uploadimage" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td colspan="2">Image 2 <input type="file" name="uploadimage2" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td colspan="2">Image 3 <input type="file" name="uploadimage3" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td colspan="2">Image 4 <input type="file" name="uploadimage4" /></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td colspan="2">Image 5 <input type="file" name="uploadimage5" /></td> 
<td>&nbsp;</td> 
</tr> 
</table> 
</form> 
+0

刪除'方法='post'',如果你希望它是'GET'(或更改'POST'到'GET')。值應該可以通過'$ _GET ['items']'來訪問。 – chris85

+0

在查詢前添加'print_r($ _ POST)'並檢查發佈的內容。 –

回答

0

您可以使用jQuery的時候,當你重定向到獲取所選值這樣

$("#dropdownid").change(function({ var vals = $(this).val(); }) 

然後下一個頁面,然後將其添加爲GET參數

0

(report.php)

if(isset($_REQUEST['submit'])){ 
    $option = $_POST['items']; 
    mysql_query("SELECT * FROM `table_name` WHERE FIELD_NAME = '$options'"); 
} 
+0

SQL注入。另外,如果使用'submit'的請求,爲什麼不設置'option'值呢? – chris85

0

可以使用p合格值下一頁惠普會議。

$_SESSION ['name'] = $value; 

你可以從下一頁使用php會話獲得價值。

$value = $_SESSION ['name']; 
+0

OP還需要啓動會話。 – chris85

0
if(isset($_REQUEST['submit'])){ 
    $option = $_REQUEST['items']; 
    mysql_query("SELECT * FROM `table_name` WHERE FIELD_NAME = '$options'"); 
} 
+0

這段代碼是SQL注入式的。 – chris85

相關問題