2016-08-12 2 views
0

이 자습서 (http://www.killersites.com/community/index.php?/topic/3064-basic-php-system-view-edit-add-delete-records-with-mysqli/)를 사용하고 있으며 데이터베이스에 새 레코드를 만드는 데 필요한 모든 단계를 수행했지만 업데이트/편집을 통해 데이터베이스를 성공적으로 업데이트 할 수 없습니다. 나는 코드가 html5를위한 것이 아니라는 것을 알고 있지만 나중에 고칠 것이다. 또한 & 삭제 작업을 검색하십시오.새 레코드 업데이트/추가가 작동하지 않습니다.

내가 뭘 잘못하고 있니? 왜 작동하지 않는거야? 어떤 도움이라도 대단히 감사합니다.

또한 내 테이블 여기
Table: supplyDetails 
Columns: 
id int(11) AI PK 
localAuthority varchar(50) 
supplyRef varchar(50) 
supplyName varchar(50) 
estimatedDailyWater varchar(10) 
numberOfConsumers varchar(45) 
dateOfAssessment date 
mitigatedRating varchar(2) 
finalRating varchar(2) 

내 records.php

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

// connect to the database 
include("connect-db.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($localauth = '', $supref = '', $supname = '', $waterusage = '', $numofconsum = '', $dateofassess = '', $mitrating = '', $frating = '', $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 } ?> 

<label>Local Authority: *</label> 
<input type="text" name="localAuthority" value="<?php echo $localauth; ?>"/> 
<br/> 

<label>Supply Reference: *</label> 
<input type="text" name="supplyRef" value="<?php echo $supref; ?>"/> 
<br/> 

<label>Supply Name: *</label> 
<input type="text" name="supplyName" value="<?php echo $supname; ?>"/> 
<br/> 

<label>Estimated Daily Water Usage: *</label> 
<input type="text" name="estimatedDailyWater" value="<?php echo $waterusage; ?>"/> 
<br/> 

<label>Number of Consumers: *</label> 
<input type="text" name="numberOfConsumers" value="<?php echo $numofconsum; ?>"/> 
<br/> 

<label>Date of Assessment: *</label> 
<input type="date" name="dateOfAssessment" value="<?php echo $dateofassess; ?>"/> 
<br/> 

<label>Mitigated Rating: *</label> 
<input type="text" name="mitigatedRating" value="<?php echo $mitrating; ?>"/> 
<br/> 

<label>Final Rating: *</label> 
<input type="text" name="finalRating" value="<?php echo $frating; ?>"/> 

<p>* required</p> 
<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']; 
$localAuthority = htmlentities($_POST['localAuthority'], ENT_QUOTES); 
$supplyRef = htmlentities($_POST['supplyRef'], ENT_QUOTES); 
$supplyName = htmlentities($_POST['supplyName'], ENT_QUOTES); 
$estimatedDailyWater = htmlentities($_POST['estimatedDailyWater'], ENT_QUOTES); 
$numberOfConsumers = htmlentities($_POST['numberOfConsumers'], ENT_QUOTES); 
$dateOfAssessment = htmlentities($_POST['dateOfAssessment'], ENT_QUOTES); 
$mitigatedRating = htmlentities($_POST['mitigatedRating'], ENT_QUOTES); 
$finalRating = htmlentities($_POST['finalRating'], ENT_QUOTES); 

// check that firstname and lastname are both not empty 
if ($localAuthority == '' || $supplyRef == '' || $supplyName == '' || $estimatedDailyWater == '' || $numberOfConsumers == '' || $dateOfAssessment == '' || $mitigatedRating == '' || $finalRating == '') { 
// if they are empty, show an error message and display the form 
$error = 'ERROR: Please fill in all required fields!'; 
renderForm($localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $dateOfAssessment, $mitigatedRating, $finalRating, $error, $id); 
} else { 
// if everything is fine, update the record in the database 
if ($stmt = $mysqli->prepare("UPDATE supplyDetails SET localAuthority = ?, supplyRef = ?, supplyName = ?, estimatedDailyWater = ?, numberOfConsumers = ?, dateOfAssessment = ?, mitigatedRating = ?, finalRating = ? WHERE id=?")) { 
$stmt->bind_param("sssssdssi", $localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $dateOfAssessment, $mitigatedRating, $finalRating, $id); 
$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: view.php"); 
} 
} 
// 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 supplyDetails WHERE id=?")) { 
$stmt->bind_param("i", $id); 
$stmt->execute(); 

$stmt->bind_result($id, $localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $dateOfAssessment, $mitigatedRating, $finalRating); 
$stmt->fetch(); 

// show the form 
renderForm($localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $dateOfAssessment, $mitigatedRating, $finalRating, 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: view.php"); 
} 
} 
} 


/* 

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 
$localAuthority = htmlentities($_POST['localAuthority'], ENT_QUOTES); 
$supplyRef = htmlentities($_POST['supplyRef'], ENT_QUOTES); 
$supplyName = htmlentities($_POST['supplyName'], ENT_QUOTES); 
$estimatedDailyWater = htmlentities($_POST['estimatedDailyWater'], ENT_QUOTES); 
$numberOfConsumers = htmlentities($_POST['numberOfConsumers'], ENT_QUOTES); 
$dateOfAssessment = htmlentities($_POST['dateOfAssessment'], ENT_QUOTES); 
$mitigatedRating = htmlentities($_POST['mitigatedRating'], ENT_QUOTES); 
$finalRating = htmlentities($_POST['finalRating'], ENT_QUOTES); 

// check that firstname and lastname are both not empty 
if ($localAuthority == '' || $supplyRef == '' || $supplyName == '' || $estimatedDailyWater == '' || $numberOfConsumers == '' || $dateOfAssessment == '' || $mitigatedRating == '' || $finalRating == '') { 
// if they are empty, show an error message and display the form 
$error = 'ERROR: Please fill in all required fields!'; 
renderForm($localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $dateOfAssessment, $mitigatedRating, $finalRating, $error); 
} else { 
// insert the new record into the database 
if ($stmt = $mysqli->prepare("INSERT supplyDetails (localAuthority, supplyRef, supplyName, estimatedDailyWater, numberOfConsumers, dateOfAssessment, mitigatedRating, finalRating) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) { 
$stmt->bind_param("sssssdss", $localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $dateOfAssessment, $mitigatedRating, $finalRating); 
$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(); 
?> 

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> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
</head> 
<body> 

<h1>View Records</h1> 

<p><b>View All</b> | <a href="view-paginated.php">View Paginated</a></p> 

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

// get the records from the database 
if ($result = $mysqli->query("SELECT * FROM supplyDetails ORDER BY id")) 
{ 
// display records if there are records to display 
if ($result->num_rows > 0) 
{ 
// display records in a table 
echo "<table border='1' cellpadding='10'>"; 

// set table headers 
echo "<tr>"; 
echo "<th>ID</th>"; 
echo "<th>Local Authority</th>"; 
echo "<th>Supply Reference</th>"; 
echo "<th>Supply Name</th>"; 
echo "<th>Estimated Daily Water Usage</th>"; 
echo "<th>Number of Consumers</th>"; 
echo "<th>Date of Assessment</th>"; 
echo "<th>Mitigated Rating</th>"; 
echo "<th>Final Rating</th>"; 
echo "<th></th><th></th></tr>"; 

while ($row = $result->fetch_object()) 
{ 
// set up a row for each record 
echo "<tr>"; 
echo "<td>" . $row->id . "</td>"; 
echo "<td>" . $row->localAuthority . "</td>"; 
echo "<td>" . $row->supplyRef . "</td>"; 
echo "<td>" . $row->supplyName . "</td>"; 
echo "<td>" . $row->estimatedDailyWater . "</td>"; 
echo "<td>" . $row->numberOfConsumers . "</td>"; 
echo "<td>" . $row->dateOfAssessment . "</td>"; 
echo "<td>" . $row->mitigatedRating . "</td>"; 
echo "<td>" . $row->finalRating . "</td>"; 
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>"; 
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>"; 
echo "</tr>"; 
} 

echo "</table>"; 
} 
// if there are no records in the database, display an alert message 
else 
{ 
echo "No results to display!"; 
} 
} 
// show an error if there is an issue with the database query 
else 
{ 
echo "Error: " . $mysqli->error; 
} 

// close database connection 
$mysqli->close(); 

?> 

<a href="records.php">Add New Record</a> 
</body> 
</html> 

이다 이와 같이 구성되는 연결-db.php를

<?php 

// server info 
$server = 'localhost:3306'; 
$user = 'root'; 
$pass = '*****'; 
$db = 'test'; 

// connect to the database 
$mysqli = new mysqli($server, $user, $pass, $db); 

// show errors (remove this line if on a live site) 
mysqli_report(MYSQLI_REPORT_ERROR); 

?> 

해결책 나중에 참조하십시오.

OK, 나는 대답을 마련 할 수 있었다. 내가 날짜에 대한 오류를 수신 한 기록을 편집으로 주위 하구 후 내 연결-db.php를 파일

mysqli_report(MYSQLI_REPORT_ALL) ; 
try { 
$mysqli = new mysqli($server, $user, $pass, $db); 

// show errors (remove this line if on a live site) 

} catch (Exception $e) { 
    echo $e->getMessage(); 
} 

로, 위의 제안 덕분에 적절한 오류 처리기를 구현, 그래서 날짜를 변경 내 mysql 테이블에 입력하고 날짜 -> varchar (30). ,

$dateOfAssessment = htmlentities($_POST['dateOfAssessment'], ENT_QUOTES); 
$displaydate = date("D d M Y", strtotime($dateOfAssessment)); 

다음

내가 그 변경 사항을 반영하기 위해 조금 내 코드를 변경 (30 일자하지만 MEH에 대한 많은 수 있음) 그리고 또한 $stmt

if ($stmt = $mysqli->prepare("UPDATE supplyDetails SET localAuthority = ?, supplyRef = ?, supplyName = ?, estimatedDailyWater = ?, numberOfConsumers = ?, dateOfAssessment = ?, mitigatedRating = ?, finalRating = ? WHERE id=?")) { 
$stmt->bind_param("ssssssssi", $localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $displaydate, $mitigatedRating, $finalRating, $id); 
$stmt->execute(); 
$stmt->close(); 
} 

그리고 변경 출력은 다음과 같습니다.

Sat 06 Aug 2016 

답장 시간이있는 모든 분들께 감사드립니다.

+0

오류 메시지 또는 로그에 오류가 있습니까? – pmahomme

+0

오류가 없습니다 – robins02

+0

http://php.net/manual/en/mysqli.error.php - http://php.net/manual/en/function.error-reporting.php 여기에 적용하십시오. 귀하의 코드, 아무것도 그것의 오는지보십시오. –

답변

0

좋아, 나는 대답을 내놓을 수 있었다. 내가 날짜에 대한 오류를 수신 한 기록을 편집으로 주위 하구 후 내 연결-db.php를 파일

mysqli_report(MYSQLI_REPORT_ALL) ; 
try { 
$mysqli = new mysqli($server, $user, $pass, $db); 

// show errors (remove this line if on a live site) 

} catch (Exception $e) { 
    echo $e->getMessage(); 
} 

로, 위의 제안 덕분에 적절한 오류 처리기를 구현, 그래서 날짜를 변경 내 mysql 테이블에 입력하고 날짜 -> varchar (30).,

$dateOfAssessment = htmlentities($_POST['dateOfAssessment'], ENT_QUOTES); 
$displaydate = date("D d M Y", strtotime($dateOfAssessment)); 

다음

내가 그 변경 사항을 반영하기 위해 조금 내 코드를 변경 (30 일자하지만 MEH에 대한 많은 수 있음) 그리고 또한 $stmt

if ($stmt = $mysqli->prepare("UPDATE supplyDetails SET localAuthority = ?, supplyRef = ?, supplyName = ?, estimatedDailyWater = ?, numberOfConsumers = ?, dateOfAssessment = ?, mitigatedRating = ?, finalRating = ? WHERE id=?")) { 
$stmt->bind_param("ssssssssi", $localAuthority, $supplyRef, $supplyName, $estimatedDailyWater, $numberOfConsumers, $displaydate, $mitigatedRating, $finalRating, $id); 
$stmt->execute(); 
$stmt->close(); 
} 

그리고 변경 출력은 다음과 같습니다.

Sat 06 Aug 2016 

답장 시간이있는 모든 분들께 감사드립니다.

관련 문제