2014-01-16 32 views
0

代碼爲20的2014年1月如何從數據庫編輯單個記錄?

<?php 
session_start(); 
// connect to the database 
include('connect.php'); 
$message = $_GET['message']; 
// check if the form has been submitted then process it 
if (isset($_POST['submit'])) 
{ 
// Get data from table 
//set the id manually for test purposes 
$id = "429"; 
$forename = mysql_real_escape_string(htmlspecialchars($_POST['forename'])); 
$surname = mysql_real_escape_string(htmlspecialchars($_POST['surname'])); 
$username = mysql_real_escape_string(htmlspecialchars($_POST['username'])); 
$email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); 
$password = mysql_real_escape_string(htmlspecialchars($_POST['password'])); 
// check for empty fields and display error message 
if ($forename == '' || $surname == '' || $username == '' || $password == '' || $email == '') 
{ 
$message = "Please enter data in all fields" ; 
header("Location: edit.php?message=$message"); 
} 
else 
{ 
// save the data to the table 
mysql_query("UPDATE registration SET forename='$forename', surname='$surname', username='$username', email='$email', password='$password' WHERE id='$id'") 
or die(mysql_error()); 
} 
// redirecr and display message 
$message = "Your changes have been saved"; 
header("Location: edit.php?message=$message"); 
exit; 
} 
$id=429;// this line could have been $id=$_SESSION['id']; 
$result = mysql_query("SELECT * FROM registration WHERE id=$id LIMIT 1") 
or die(mysql_error()); 
$row = mysql_fetch_array($result); 
// check that the 'id' matches up with a row in the databse 
if($row) 
{ 
// get data from the table 
$forename = $row['forename']; 
$surname = $row['surname']; 
$username = $row['username']; 
$email = $row['email']; 
$password = $row['password']; 
//dummy echo 
print $message; 
} 
?> 

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 

<link rel="stylesheet" href="styles/all.css" /> 
<link rel="stylesheet" href="styles/forms.css" /> 

<script type="text/javascript" src="javascript/jquery-1.7.1.min.js"></script> 

<link href='//fonts.googleapis.com/css?family=Cantora+One' rel='stylesheet' type='text/css'> 
<link href='//fonts.googleapis.com/css?family=Voltaire' rel='stylesheet' type='text/css'> 
<link href='//fonts.googleapis.com/css?family=Ubuntu:400,500' rel='stylesheet' type='text/css'> 

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> 

</head> 

<div class="container"> 

<form action="" method="post" enctype="multipart/form-data" name="edit" id="editrecord"> 
<fieldset> 
<legend><span class="headingreg">Edit Details</span></legend> 
<div class="formreg"> 

<input type="hidden" name="id" value="<?php echo $id; ?>"/> 

<br style="clear:left;"/> 
<label for="forename">Forename</label><div><input type="text" id="forename" name="forename" class="insetedit" value="<?php echo $forename; ?>"/><br/></div> 
<label for="forename">Surname</label><div><input type="text" name="surname" class="insetedit" value="<?php echo $surname; ?>"/><br/></div> 
<label for="forename">Username</label><div><input type="text" name="username" class="insetedit" value="<?php echo $username; ?>"/><br/></div> 
<label for="forename">Password</label><div><input type="text" name="password" class="insetedit" value="<?php echo $password; ?>"/><br/></div> 
<label for="forename">email</label><div><input type="text" name="email" class="insetedit" value="<?php echo $email; ?>"/><br/></div> 

<input type="submit" name="submit" class="submit2" value="submit"> 
</div> 
</fieldset> 
</form> 

<br style="clear:left;"/> 
<br style="clear:left;"/> 

</body> 
</html> 

縮進REMOVED

我下面要編輯的教程,並在數據庫中刪除存儲的記錄。

http://www.falkencreative.com/forum/records/view.php

在本教程中一個頁面顯示記錄在數據庫中,另一個用來編輯記錄:

http://www.falkencreative.com/forum/records/edit.php?id=33004

的問題是數據庫中的所有記錄都顯示出來。我需要做什麼更改才能顯示和編輯基於單個頁面上指定ID的記錄?例如

$ id =「429」;

最終我會使用會話,但爲了測試目的,我想手動設置ID。

我試着把代碼放在一個頁面中,但有很多錯誤,例如標題已發送。

這是edit.php頁面,我嘗試手動設置ID。

<?php 
/* 
EDIT.PHP 
Allows user to edit specific entry in database 
*/ 

// creates the edit record form 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
function renderForm($id, $forename, $surname, $username, $password, $email, $error) 
{ 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>Edit Record</title> 
</head> 
<body> 
<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 

<form action="" method="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
<div> 
<p><strong>ID:</strong> <?php echo $id; ?></p> 
<strong>Forename: *</strong> <input type="text" name="forename" value="<?php echo $forename; ?>"/><br/> 
<strong>Surname: *</strong> <input type="text" name="surname" value="<?php echo $surname; ?>"/><br/> 
<strong>Username: *</strong> <input type="text" name="username" value="<?php echo $username; ?>"/><br/> 
<strong>email: *</strong> <input type="text" name="password" value="<?php echo $password; ?>"/><br/> 
<strong>password: *</strong> <input type="text" name="email" value="<?php echo $email; ?>"/><br/> 
<p>* Required</p> 
<input type="submit" name="submit" value="Submit"> 
</div> 
</form> 
</body> 
</html> 
<?php 
} 


// connect to the database 
include('connect-db.php'); 

// check if the form has been submitted. If it has, process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// confirm that the 'id' value is a valid integer before getting the form data 
if (is_numeric($_POST['id'])) 
{ 
// get form data, making sure it is valid 
$id = "429"; 
$forename = mysql_real_escape_string(htmlspecialchars($_POST['forename'])); 
$surname = mysql_real_escape_string(htmlspecialchars($_POST['surname'])); 
$username = mysql_real_escape_string(htmlspecialchars($_POST['username'])); 
$email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); 
$password = mysql_real_escape_string(htmlspecialchars($_POST['password'])); 

// check that forename/surname fields are both filled in 
if ($forename == '' || $surname == '' || $username == '' || $password == '' || $email == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 

//error, display form 
    renderForm($id, $forename, $surname, $username, $password, $email, $error); 
} 
else 
{ 
// save the data to the database 
mysql_query("UPDATE login SET forename='$forename', surname='$surname', username='$username', email='$email', password='$password' WHERE id='$id'") 
or die(mysql_error()); 


// once saved, redirect back to the view page 
header("Location: view.php"); 
} 
} 
else 
{ 
// if the 'id' isn't valid, display an error 
echo 'Error!'; 
} 
} 
else 
// if the form hasn't been submitted, get the data from the db and display the form 
{ 

// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
{ 
// query db 
$id = $_GET['id']; 
$result = mysql_query("SELECT * FROM login WHERE id=$id") 
or die(mysql_error()); 
$row = mysql_fetch_array($result); 

// check that the 'id' matches up with a row in the databse 
if($row) 
{ 

// get data from db 
$forename = $row['forename']; 
$surname = $row['surname']; 
$username = $row['username']; 
$email = $row['email']; 
$password = $row['password']; 

// show form 
renderForm($id, $forename, $surname, $username, $password, $email, ''); 
    } 
else 
// if no match, display result 
{ 
echo "No results!"; 
} 
} 
else 
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error 
{ 
echo 'Error!'; 
} 
} 
?> 

而且view.php頁面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title>View Records</title> 
</head> 
<body> 

<?php 
/* 
VIEW.PHP 
Displays all data from 'players' table 
*/ 

// connect to the database 
include('connect-db.php'); 

// get results from database 
$result = mysql_query("SELECT * FROM login") 
or die(mysql_error()); 

// display data in table 
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>"; 

echo "<table border='1' cellpadding='10'>"; 
echo "<tr> <th>ID</th> <th>Forename</th> <th>Surname</th> <th>Username</th> <th>eMail</th> <th>Password</th></tr>"; 

// loop through results of database query, displaying them in the table 
while($row = mysql_fetch_array($result)) { 

// echo out the contents of each row into a table 
echo "<tr>"; 
echo '<td>' . $row['id'] . '</td>'; 
echo '<td>' . $row['forename'] . '</td>'; 
echo '<td>' . $row['surname'] . '</td>'; 
echo '<td>' . $row['username'] . '</td>'; 
echo '<td>' . $row['password'] . '</td>'; 
echo '<td>' . $row['email'] . '</td>'; 
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>'; 
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; 
echo "</tr>"; 
} 

// close table> 
echo "</table>"; 
?> 
<p><a href="new.php">Add a new record</a></p> 

</body> 
</html> 

REMOVED功能和錯誤變量

<?php 
include('connect.php'); 
// check if the form has been submitted. If it has, process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// get form data 
$id = "429"; 
$forename = mysql_real_escape_string(htmlspecialchars($_POST['forename'])); 
$surname = mysql_real_escape_string(htmlspecialchars($_POST['surname'])); 
$username = mysql_real_escape_string(htmlspecialchars($_POST['username'])); 
$email = mysql_real_escape_string(htmlspecialchars($_POST['email'])); 
$password = mysql_real_escape_string(htmlspecialchars($_POST['password'])); 
// check empty fields 
if ($forename == '' || $surname == '' || $username == '' || $password == '' || $email == '') 
{ 
// generate error message 
echo 'ERROR: Please fill in all required fields!'; 
} 
else 
{ 
// save the data to the database 
mysql_query("UPDATE registration SET forename='$forename', surname='$surname', username='$username', email='$email', password='$password' WHERE id='$id'") 
or die(mysql_error()); 
// Redirect 
echo "Your changes have been saved"; 
header("Location: edit.php"); 
} 
} 
$id=429;// this line could have been $id=$_SESSION['id']; 
$result = mysql_query("SELECT * FROM registration WHERE id=$id LIMIT 1") 
or die(mysql_error()); 
$row = mysql_fetch_array($result); 
// check that the 'id' matches up with a row in the databse 
if($row) 
{ 
// get data from db 
$forename = $row['forename']; 
$surname = $row['surname']; 
$username = $row['username']; 
$email = $row['email']; 
$password = $row['password']; 
//dummy echo 
echo 'formatting is messed up'; 
} 
?> 
+0

* * *教程,使用折舊函數 – 2014-01-16 02:30:05

+1

'「SELECT * FROM登錄WHERE ID = $ ID」' – Sablefoste

+0

這已經在上面的edit.php代碼中。 – Nullbreaker

回答

0

你只需要與提供該ID的代碼替換$_GET['id']

如果您正在使用的會話,例如,與$_SESSION['id']

更換$_GET['id']在你的代碼文件edit.php:

// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
{ 
// query db 
$id = $_GET['id']; 
$result = mysql_query("SELECT * FROM login WHERE id=$id") 
or die(mysql_error()); 
$row = mysql_fetch_array($result); 

更改爲:

$id=429;// this line could have been $id=$_SESSION['id']; 
$result = mysql_query("SELECT * FROM login WHERE id=$id LIMIT 1") 
    or die(mysql_error()); 
$row = mysql_fetch_array($result); 

當然,爲了達到執行該代碼的目的,您需要刪除條件語句,因爲您不再從第e $_GET

我還爲查詢添加了一個LIMIT 1,以便您只返回一條記錄;你可能會反正,但如果id沒有唯一的索引(如主鍵),它可能會返回多個記錄。

另外,在本例中,您幾乎可以用mysqli_替換所有已棄用的mysql_參考號。它不會像mysqli那樣通過準備好的語句來保護你,但它仍然應該起作用。

最後,renderForm函數是一個形式很差的函數。每個頁面只能有一個<html>聲明,如果您多次調用該函數,則會有多個聲明。

+0

renderForm有什麼替代方法?另外我想顯示更新消息。我試圖使用錯誤變量。 header(「Location:edit.php」); $ error =「您已成功註冊」; – Nullbreaker

+0

另一種方法是剪切並粘貼'renderForm'內的代碼,並替換函數調用。特別是在這種情況下,你只需要調用一次。緊接着這個粘貼(仍然在你的條件'if'中,添加'echo「你已經成功註冊了。」;'。 – Sablefoste

+0

謝謝Sable。我將使用會話來獲取數據是必要的功能嗎?我也刪除了錯誤變量,因爲它是沒有必要的,一切工作正常,如果任何一個字段是空白的,就會顯示錯誤消息,並且一旦用戶提交數據就顯示一條消息。唯一的問題是格式化是直到我點擊提交或在從數據庫中提取數據後,輸入一個虛擬echo語句。我已經標記了它 // dummy echo 並將其添加到開幕文章中的代碼,以便您可以希望看到什麼是造成這種情況? – Nullbreaker

相關問題