2011-05-29 85 views
-2

首先我想說我是一個初學者在postgresql和PHP ..我的公司告訴我創建一個數據庫,他們可以在本地服務器上查看和編輯..所以我創建了數據庫在postgresql ..創建一個頁面正在查看該數據庫:如何使用php更新postgresql行?

<html> 
<head> 
<title>Ongoing projects</title> 
</head> 
<body bgcolor="666657"> 
<?php 
//database access information 
require_once("DB.php"); 
$host = "localhost"; 
$user = "admin"; 
$pass = ""; 
$db = "Project_list"; 
$port = "5432"; 

//open a connection to the database server 
$connection = pg_connect("host=$host dbname=$db user=$user password=$pass port=$port"); 
if (!$connection) 
{ 
die("Could not open connection to database server"); 
} 

?> 


<?php 


$query = 'select * from ongoing'; 
$result = pg_query($query); $i = 0; 


echo '<html><table bgcolor="666657" width="10" height="30" border="0" cellpadding="0" cellspacing="0"><td align="center"> <h1><font color = "#ffb200"> Ongoing projects</h1>'; 

echo '<html><body><table border= 2 BORDERCOLOR="000000" cellpadding="1" cellspacing="0"> <tr >'; 


while ($i < pg_num_fields($result)) { 
$fieldName =pg_field_name($result, $i); 
echo '<b>'.'<td width="2" bgcolor="666657" align="center">'.'<font color = "#ffb200">'. '</b>'.'<b>'. $fieldName . '</b>'. '</td>'; 
$i = $i + 1; } 
echo("<td><align= center><font color = #ffb200><b>Action</td>"); 

echo '</tr>' ; 
$i = 0; 

while ($row = pg_fetch_row($result)) { 
echo '<tr align="center" width="1">'; 
$count = count($row); 
$y = 0; 
while ($y < $count) { 
$c_row = current($row); 
echo '<td>' .'<font color = "#ffb200">'. $c_row . '</td>'; 

next($row); 

$y = $y + 1; 
} 
echo("<td><align= center><a href='editongoing.php?ProjectID=".$row[0]."'>Edit</a></td>"); 
echo '</tr>'; 
$i = $i + 1; 
} 


pg_free_result($result); 

echo '</table></body></html>'; 

?> 
<h3> 
<a href="projects.php"</a>Back to projects page</a> 

</h3> 
<SCRIPT LANGUAGE="JavaScript"> 
if (window.print) { 

document.write('<form> ' 
+ '<input type=button name=print value="Click" ' 
+ 'onClick="javascript:window.print()"> To Print!</form>'); 
} 
// End --> 
</script> 

當您單擊編輯按鈕,你會去到這個頁面,您可以編輯您想要的原料,這是(編輯)代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html> 
<head> 
<title>Edit Ongoing projects</title> 
</head> 
<body bgcolor="666657"> 

<?php 
// attempt a connection 
$connection = pg_connect("host=localhost dbname=Project_list user=admin password="); 
if (!$connection) { 
die("Error in connection: " . pg_last_error()); 
} 
if ($_REQUEST['ProjectID']!=''){ 

$QueryStr = "Select * from ongoing where project_no='".$_REQUEST['ProjectID']."'"; 
$result = pg_query($connection, $QueryStr); 
if (!$result) { 
die("Error in SQL query: " . pg_last_error()); 
} 
$row = pg_fetch_row($result); 

print_r($row); 

} 
if ($_POST['submit']) { 


// escape strings in input data 
$project_no = pg_escape_string($_POST['project_no']); 
$title = pg_escape_string($_POST['title']); 
$duration = pg_escape_string($_POST['duration']); 
$manager = pg_escape_string($_POST['manager']); 
$country = pg_escape_string($_POST['country']); 
$total_fee = pg_escape_string($_POST['totalfee']); 
$performed = pg_escape_string($_POST['performed']); 
$remaining = pg_escape_string($_POST['remaining']); 
$gross_profit = pg_escape_string($_POST['gross_profit']); 
$gp = pg_escape_string($_POST['gp']); 
$performance_year = pg_escape_string($_POST['performance_year']); 
$gp_year = pg_escape_string($_POST['gp_year']); 


// execute query 
$sql = "INSERT INTO ongoing (project_no, project_title, duration, manager, country, total_fee, 
performed, remaining, gross_profit, gp, performance_year, gp_year) 
VALUES('$project_no', '$title', '$duration', '$manager', '$country','$total_fee','$performed','$remaining', 
'$gross_profit','$gp', '$performance_year','$gp_year')"; 
$result = pg_query($connection, $sql); 
f (!$result) { 
die("Error in SQL query: " . pg_last_error()); 
} 

echo "Data successfully inserted!"; 


// free memory 
pg_free_result($result); 

// close connection 
pg_close($connection); 
} 
?> 

<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><b><font color = "#ffb200"> 
Project No.: <br> <input id="project_no" type="text" name="project_no" size="20" value=<?= $row[0] ?>> 
<p> 
Project Title: <br> <input id="title" type="text" name="title" value='<?= $row[1] ?>'> 
<p> 
Duration: <br> <input ID="duration" type="text" name="duration" value=<?= $row[2] ?>> 
<p> 
Project Manager: <br> <input ID="manager" type="text" name="manager" value=<?= $row[3] ?>> 
<p> 
Country: <br> <input ID="country" type="text" name="country" value=<?= $row[4] ?>> 
<p> 
Total Fee: <br> <input ID="totalfee" type="text" name="total_fee" value=<?= $row[5] ?>> 
<p> 
Already performed: <br> <input ID="performed" type="text" name="performed" value=<?= $row[6] ?>> 
<p> 
Remaining performance: <br> <input ID="remaining" type="text" name="remaining" value=<?= $row[7] ?>> 
<p> 
Gross Profit: <br> <input ID="gross_profit" type="text" name="gross_profit" value='<?= $row[8] ?>'> 
<p> 
GP%: <br> <input ID="gp" type="text" name="gp" value=<?= $row[9] ?>> 
<p> 
Performance actual year: <br> <input ID="performance_year" type="text" name="performance_year" value=<?= $row[10] ?>> 
<p> 
GP actual year: <br> <input ID="gp_year" type="text" name="gp_year" value=<?= $row[11] ?>> 
<p> 

<input type="submit" name="submit" value="Sumbit my table" size="30"> 

<P> 
<a href="ongoing.php"</a>View ongoing projects</a> 
<a href="editproject.php"</a>Back to editing menu</a> 
</form> 

</body> 
</html> 

我的問題是,當我編輯數據,然後單擊提交我的表,一個新的原料插入..但我希望它被更新沒有插入...幫助PLZ

回答

1

您需要選擇哪個記錄(ID號碼),然後您的查詢將看起來像

$sql = "UPDATE ongoing SET field1='value', field2='value' ... WHERE id = 'id of project you want to edit'";