2017-06-30 59 views
0

制作一些东西,以便您可以使用表单编辑数据库条目,但不更新,第一个ID 0 doens't根本不起作用,但不要担心,当您单击编辑时,主要是在表格中,而不是显示名称,排名,文本 它显示 文本,name.rank ,它也没有更新时,它按下提交。PHP编辑数据库条目不更新

下面是什么,现在看起来像一个预览:http://rumblegaming.co.uk/admin/records.php?id=1 这里是我的代码:

<?php 
/* 
Allows the user to both create new records and edit existing records 
*/ 

// connect to the database 
include("connect.php"); 

// creates the new/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($name = '', $rank ='', $text ='', $error = '', $id = '') 
{ ?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<title> 
<?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?> 
</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
</head> 
<body> 
<h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1> 
<?php if ($error != '') { 
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error 
. "</div>"; 
} ?> 

<form action="" method="post"> 
<div> 
<?php if ($id != '') { ?> 
<input type="hidden" name="id" value="<?php echo $id; ?>" /> 
<p>ID: <?php echo $id; ?></p> 
<?php } ?> 

<strong>Name:</strong> <input type="text" name="name" 
value="<?php echo $name; ?>"/><br/> 
<strong>Rank:</strong> <input type="text" name="rank" 
value="<?php echo $rank; ?>"/><br/> 
<strong>Text:</strong> <input type="text" name="text" 
value="<?php echo $text; ?>"/><br/> 
<input type="submit" name="submit" value="Submit" /> 
</div> 
</form> 
</body> 
</html> 

<?php } 



/* 

EDIT RECORD 

*/ 
// if the 'id' variable is set in the URL, we know that we need to edit a record 
if (isset($_GET['id'])) 
{ 
// if the form's submit button is clicked, we need to process the form 
if (isset($_POST['submit'])) 
{ 
// make sure the 'id' in the URL is valid 
if (is_numeric($_POST['id'])) 
{ 
// get variables from the URL/form 
$id = $_POST['id']; 
$name = htmlentities($_POST['name'], ENT_QUOTES); 
$rank = htmlentities($_POST['rank'], ENT_QUOTES); 
$text = htmlentities($_POST['text'], ENT_QUOTES); 

// check that firstname and lastname are both not empty 
if ($name == '' || $rank == '') 
{ 
// if they are empty, show an error message and display the form 
$error = 'ERROR: Please fill in all required fields!'; 
renderForm($name, $rank, $text, $error, $id); 
} 
else 
{ 
// if everything is fine, update the record in the database 
if ($stmt = $mysqli->prepare("UPDATE Team SET name = ?, rank = ?, text = ? WHERE id=?")) 
{ 
$stmt->bind_param("ssi", $id, $name, $rank, $text); 
$stmt->execute(); 
$stmt->close(); 
} 
// show an error message if the query has an error 
else 
{ 
echo "ERROR: could not prepare SQL statement."; 
} 

// redirect the user once the form is updated 
header("Location: home"); 
} 
} 
// if the 'id' variable is not valid, show an error message 
else 
{ 
echo "Error!"; 
} 
} 
// if the form hasn't been submitted yet, get the info from the database and show the form 
else 
{ 
// make sure the 'id' value is valid 
if (is_numeric($_GET['id']) && $_GET['id'] > 0) 
{ 
// get 'id' from URL 
$id = $_GET['id']; 

// get the recod from the database 
if($stmt = $mysqli->prepare("SELECT * FROM Team WHERE id=?")) 
{ 
$stmt->bind_param("i", $id); 
$stmt->execute(); 

$stmt->bind_result($id, $rank, $text, $name); 
$stmt->fetch(); 

// show the form 
renderForm($name, $rank, $text, NULL, $id); 

$stmt->close(); 
} 
// show an error if the query has an error 
else 
{ 
echo "Error: could not prepare SQL statement"; 
} 
} 
// if the 'id' value is not valid, redirect the user back to the view.php page 
else 
{ 
header("Location: home"); 
} 
} 
} 



/* 

NEW RECORD 

*/ 
// if the 'id' variable is not set in the URL, we must be creating a new record 
else 
{ 
// if the form's submit button is clicked, we need to process the form 
if (isset($_POST['submit'])) 
{ 
// get the form data 
$firstname = htmlentities($_POST['firstname'], ENT_QUOTES); 
$lastname = htmlentities($_POST['lastname'], ENT_QUOTES); 

// check that firstname and lastname are both not empty 
if ($firstname == '' || $lastname == '') 
{ 
// if they are empty, show an error message and display the form 
$error = 'ERROR: Please fill in all required fields!'; 
renderForm($firstname, $lastname, $error); 
} 
else 
{ 
// insert the new record into the database 
if ($stmt = $mysqli->prepare("INSERT players (firstname, lastname) VALUES (?, ?)")) 
{ 
$stmt->bind_param("ss", $firstname, $lastname); 
$stmt->execute(); 
$stmt->close(); 
} 
// show an error if the query has an error 
else 
{ 
echo "ERROR: Could not prepare SQL statement."; 
} 

// redirec the user 
header("Location: view.php"); 
} 

} 
// if the form hasn't been submitted yet, show the form 
else 
{ 
renderForm(); 
} 
} 

// close the mysqli connection 
$mysqli->close(); 
?> 

回答

1

貌似错误的位置:

if ($stmt = $mysqli->prepare("UPDATE Team SET name = ?, rank = ?, text = ? WHERE id=?")) 
{ 
$stmt->bind_param("ssi", $id, $name, $rank, $text); // check data types here 

如果数据类型是这样的:

$name = string 
$rank = string 
$text = string 
$id = integer 

然后它应该是这样的:

if ($stmt = $mysqli->prepare("UPDATE Team SET name = ?, rank = ?, text = ? WHERE id=?")) 
{ 
$stmt->bind_param("sssi", $name, $rank, $text, $id); 
+0

谢谢,这固定它,但他们仍然没有按正确的顺序显示。 –

+0

任何线索为什么他们没有以正确的顺序显示? –