기본 index.php 파일의 맨 위에 파일을 포함 시켰는데 그 안에 변수 (포함 된 파일)를 상수 (정적)로 만드는 데 문제가 있습니다
.- 추가 정보 -: 그래서 양식을 만들려고합니다. 사용자가 성공적으로 제출할 때마다 양식은 새 파일 (이름과 피드백 단락 포함)을 만들어야합니다. 그래서 카운터를 만들어 파일명에 붙여서 제출할 때마다 .. 새 파일이 만들어집니다.
static $counter = 3;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// validation code
if( /* checking that the submission is successful */){
$file = "feedback". $GLOBALS['counter'] .".txt";
file_put_contents( $file, $feedback_paragraph );
$GLOBALS[ 'counter' ] = $GLOBALS [ 'counter' ] + 1;
}
}
문제는 카운터가 동일하게 유지된다는 것입니다. 다시 제출하면 4로 증가하고 다시 3으로 재설정됩니다. 왜 그런지 모르겠어요?
참고 : 이러한 프로젝트를 구현하는 가장 좋은 방법이 아니라는 것을 알고 있지만 여전히 php가 작동하는 방식과이 코드가 작동하지 않는 이유를 이해하고 싶습니다.
모두에게 감사합니다. 당신은 정말 놀라운 커뮤니티입니다!
@Moaz Eldefrawy 나는 당신을 위해 매우 간단한 스크립트를 작성했지만 최적이 아니라는 것을 알고 있지만 교육 목적으로 가능한 한 많은 세부 사항을 작성 하고이 코드를 로컬 호스트에 붙여 넣고 사용해보십시오.
이 코드는 이름과 피드백의 데이터를 텍스트 파일에 "feedback (counter value) .txt"로 기록하고 계수 목적으로 디렉토리에 counter.txt를 만듭니다.
디렉토리를 확인하고 기록 된 데이터에 대한 "feedback (counter value) .txt"를 열어보십시오.
도움이 되었기를 바랍니다 :)
<?php
//initialize
$name = "";
$feedback = "";
$counter_file = "counter.txt";
//Check if name field has input
if(isset($_POST['name']) && ($_POST['name']) != "") {
$name = $_POST['name'];
}
//Check if feedback has input
if(isset($_POST['feedback']) && ($_POST['feedback']) != "") {
$feedback = $_POST['feedback'];
}
//if name field and feedback has input call check_counter and record_counter functions
if((!empty($name)) && (!empty($feedback))) {
check_counter($counter_file);
record_data($name, $feedback, $counter_file);
}
//assuming no counter.txt is created in your directory attempt to create counter.txt
function check_counter($counter_file) {
if(file_exists($counter_file) == false) {
$file = fopen($counter_file, 'a'); //if no counter.txt file in your directory create one
$counter = 0; //initialize counter from 0
} else {
$file = fopen($counter_file, 'a+'); //if counter.txt file exist open that file
$counter = file_get_contents($counter_file); //read the counter as string
$counter++; //increment counter
}
file_put_contents($counter_file, $counter); //value of counter is recorded in counter.txt
fclose($file); //close the file
}
function record_data($name, $feedback, $counter_file) {
$file = fopen($counter_file, 'a+'); //open counter.txt file
$counter = file_get_contents($counter_file); //read the counter value of file
$txt = fopen('feedback'.$counter.'.txt', 'a'); //open file, if no file exist attempt to create it
$data = "name: ".$name.PHP_EOL."feedback: ".$feedback;
fwrite($txt, $data); //record the name,feed back value as 'feedback$counter.txt'
fclose($file); //close the file
}
?>
<html>
<head>
<title>record_data</title>
</head>
<body>
<form action="index.php" method="post">
<table>
<tr>
<td>User Name: </td><td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Feedback: </td><td><textarea name="feedback" /></textarea></td>
</tr>
<tr>
<td></td><td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다