2013-08-30 50 views
0

我是PHP的新手,所以請原諒我的簡單問題(我甚至不知道如何問它或我需要使用什麼關鍵字)。試圖自動填寫用戶數據的表格

基本上,我試圖更新我的數據庫中的用戶數據。我有列明所有客戶一個PHP(phpselect.php):

<html> 
<body> 

<?php 
# 
# Connect to the database 
# 
$conn=odbc_connect('database1','',''); 
if (!$conn) 
{exit("Connection Failed: " . $conn);} 

# 
# SQL statements 
# 
$sql="SELECT * FROM customer"; 
$rs=odbc_exec($conn,$sql); 
if (!$rs) 
{exit("Error in SQL");} 

# 
# Make a table 
# 
echo "<table><tr>"; 
echo "<th>Action</th>"; 
echo "<th>Customer ID</th>"; 
echo "<th>Last Name</th>"; 
echo "<th>First Name</th>"; 
echo "<th>Middle Initial</th>"; 
echo "<th>Home Phone</th>"; 
echo "<th>Cell Phone</th>"; 
echo "<th>Date of Birth</th>"; 
echo "<th>Address</th>"; 
echo "<th>City</th>"; 
echo "<th>State</th>"; 
echo "<th>Zip Code</th>"; 
echo "<th>Employer</th>"; 
echo "<th>Referrer</th>"; 
echo "<th>Agent</th></tr>"; 

# 
# Fetch records from SQL result-set 
# 
while (odbc_fetch_row($rs)) 
{ 

# 
# Set field variables... 
# 
$cust_ID=odbc_result($rs,"cust_ID"); 
$cust_last=odbc_result($rs,"cust_last"); 
$cust_first=odbc_result($rs,"cust_first"); 
$cust_mi=odbc_result($rs,"cust_mi"); 
$home_phone=odbc_result($rs,"home_phone"); 
$cell_phone=odbc_result($rs,"cell_phone"); 
$DOB=odbc_result($rs,"DOB"); 
$street=odbc_result($rs,"street"); 
$city=odbc_result($rs,"city"); 
$state=odbc_result($rs,"state"); 
$zip_code=odbc_result($rs,"zip_code"); 
$employer=odbc_result($rs,"employer"); 
$referrer=odbc_result($rs,"referrer"); 
$agent_ID=odbc_result($rs,"agent_ID"); 

# 
# ...and display them by variable name 
# 

echo "<tr>"; 
echo "<td>edit</td>"; 
echo "<td>$cust_ID</td>"; 
echo "<td>$cust_last</td>"; 
echo "<td>$cust_first</td>"; 
echo "<td>$cust_mi</td>"; 
echo "<td>$cust_last</td>"; 
echo "<td>$home_phone</td>"; 
echo "<td>$cell_phone</td>"; 
echo "<td>$DOB</td>"; 
echo "<td>$street</td>"; 
echo "<td>$city</td>"; 
echo "<td>$state</td>"; 
echo "<td>$zip_code</td>"; 
echo "<td>$employer</td>"; 
echo "<td>$referrer</td>"; 
echo "<td>$agent_ID</td></tr>"; 
} 

# 
# Close the connection to the database 
# 
odbc_close($conn); 

# 
# End the table 
# 
echo "</table>"; 
?> 
</body> 
</html> 

...,我想在每行的鏈接(「更新」),將發送用戶的信息添加(從數據庫),以在表單中的字段,以便字段進行編輯(phpinsert.php):

<html> 
<body> 

Enter Customer Information 
<br> 
(* indicates required fields) 

<p> 
<form action="phpinsert.php" method="post"> 
Last Name*: <input type="text" name="cust_last"> 
<br> 
First Name*: <input type="text" name="cust_first"> 
<br> 
Middle Initial: <input type="text" name="cust_mi"> 
<br> 
Home Phone*: <input type="text" name="home_phone"> 
<br> 
Cell Phone: <input type="text" name="cell_phone"> 
<br> 
Date of Birth (mm/dd/yyyy)*: <input type="text" name="DOB"> 
<br> 
Street Address: <input type="text" name="street"> 
<br> 
City: <input type="text" name="city"> 
<br> 
State: <input type="text" name="state"> 
<br> 
Zip Code: <input type="text" name="zip_code"> 
<br> 
Employer: <input type="text" name="employer"> 
<br> 
Referrer: <input type="text" name="referrer"> 
<br> 
<tr> 
<td class="formLabel">Agent*:</td> 
<td> 
<select name="agent_ID" class="formEntry"> 
<option value="1">Guy Smiley</option></select> 
</td> 
</tr> 
<br><br> 
<input type="submit"> 
</form> 
</body> 
</html> 

我不知道如何做到這一點還是怎麼問它雖然正確?

感謝您的任何幫助。

+0

您可以通過在[phpAcademy](http://phpacademy.org)上觀看Alex Garret的免費教程獲得一些好處 – gibberish

+0

對於這種類型的操作,您會遇到很多術語是CRUD,我會讓你查找它並搜索包含PHP和CRUD的簡單教程。祝你好運! – Cups

回答

2

你可以把你的價值觀的更新表單的頁面的URL:

<?php 
$info = "cust_id=".$cust_id."&cust_last=".$cust_last; //etc. 
$url = "http://".$_SERVER['HTTP_HOST']."/phpinsert.php?".$info; 
?> 
<!-- html --> 
<a href="<?php echo $url; ?>">Update</a> 

然後在更新形式:

Customer ID: <input type="text" name="cust_id" value="<?php echo $_GET['cust_id']; ?>"> 
<br> 
Customer Last: <input type="text" name="cust_last" value="<?php echo $_GET['cust_last']; ?>"> 
+0

我試圖實現這一點,但每當我嘗試我的'更新'字段(在我的表),頁面不再顯示(只是一個空白頁)。 –

+0

當一個php頁面是空白的,通常表明你有一個語法錯誤。嘗試檢查您的語法並添加ini_set('display_errors',1);更新你的代碼,我會發布另一個答案來幫助你。 – DrewP84

0

這裏的總體思路(如果你想將表格與表格保持在同一頁面上):

爲每行包含所有元素創建一個列表,即:

PHP

$alldata= "'".$cust_ID."','".$cust_last."','".$cust_first."','". ... ; 

推,列出爲一個JavaScript函數,即:

PHP

echo "<td>$cust_ID</td>"; 
.... 
echo "<td>$agent_ID</td>"; 
echo "<td><a onClick=populateForm(".$alldata."); href=#>Update</a></tr>"; 

你需要一個ID添加到每個文本框,即:

HTML

Last Name*: <input type="text" id="cust_last" name="cust_last"> 
First Name*: <input type="text" id="cust_first" name="cust_first"> 

現在使用JavaScript來這些值推入形式,即:

的Javascript

function populateForm(custid,cust_last,cust_first, ...) { 
    document.getElementById('cust_last').value = cust_last; 
    ... 
} 

請評論,如果您需要更多的幫助,我會進一步闡述。