이번이 처음으로 PHP 양식을 작성했기 때문에 많은 자습서를 읽고 복사 해 왔지만 처음부터 끝까지 모든 것을 다루지 않는 것 같으므로 다른 사람들의 결정을 하나의 파일로 단편화하고 있습니다. 정말 작동합니다.
나는 내가 지금까지 배운 것을 이해하고 있다고 생각한다. 나는이 PHP의 문제를 해결하는 방법에 대해 충분히 알지 못하고 어디에서 잘못되는지 알아 내었다. HTML은 다음과 같습니다.
<section class="content">
<form method="POST" action="" enctype="multipart/form-data" name="form">
<p>Please remember, these results are saved into the database and will be shown to other users. So please do not include any identifying or personal information.</p>
<label>
<input type="text" id="object" placeholder="Name an object" required="required" />
<i class="fa fa-wrench"></i>
</label>
<label>
<input type="text" id="location" placeholder="Name a location" required="required" />
<i class="fa fa-map-marker"></i>
</label>
<label>
<input type="text" id="person" placeholder="Name a person" required="required" />
<i class="fa fa-user"></i>
</label>
<button type="submit">Submit</button>
</form>
</section>
<section class="result">
<div class="return"><?php echo $result; ?></div>
<h2>See how other people have responded.</h2>
<div class="previous"></div>
</section>
js :
<script type="text/javascript">
$(function(){
$("button").click(function(e){
e.preventDefault();
$.post("phptest.php", {$("form").serialize()}, function(res){
$(".return").html(res);
$(".content").hide();
$(".result").show();
});
});
});
</script>
다음은 PHP입니다.
<?php
$mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
if($mysqli->errno) {
printf("Connection To Database Failed:<br/> %s", $mysqli->error());
die();
};
$query = "INSERT INTO test_table (person, object, location) VALUES ('{$person}', '{$object}', '{$location}')";
$stmt = $mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('sss', $person, $object, $location);
$person = $_POST['person'];
$object = $_POST['object'];
$location = $_POST['location'];
$results = $mysqli->query($query);
$stmt->execute();
$stmt->close();
$mysqli->close();
?>
<?php
$mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
if($mysqli->errno) {
printf("Connection To Database Failed:<br/> %s", $mysqli->error());
die();
};
$query = "SELECT person, object, location FROM test_table WHERE person = ?";
$stmt = $mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('sss', $person, $object, $location);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
printf('<strong>%s</strong> is a person. <strong>%s</strong> is an object. <strong>%s</strong> is a location.', $row['person'], $row['object'], $row['location']);
$stmt->close();
$mysqli->close();
?>
이제 튜토리얼을 이해하고 튜토리얼에서 말한 내용을 취하여 내 사용 사례에 적용하려고 시도 했으므로 위의 내용이 잘못된 것은 내가 나쁜 조언을 따르는 경우가 아니라 내 잘못입니다.
골:
<div class="return">
<div class="previous">
(따라서 i<11
일부)먼저 각 입력 태그에서 이름 속성 을 사용해야합니다 . 예 :
<input type="text" id="location" name="location" placeholder="Name a location" required="required" />
js 코드는 다음과 같을 수 있습니다.
<script type="text/javascript">
$(function(){
$("button").click(function(e){
e.preventDefault();
$.post("phptest.php", $("form").serialize())
.done(function(res) {
$(".return").html(res);
$(".content").hide();
$(".result").show();
});
});
});
</script>
그리고 이것은 PHP 스크립트입니다. 주석 기호가있는 줄이 첫 번째 스크립트입니다.
<?php
$mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
if($mysqli->errno) {
printf("Connection To Database Failed:<br/> %s", $mysqli->error());
die();
};
//$query = "INSERT INTO test_table (person, object, location) VALUES ('{$person}', '{$object}', '{$location}')";
$query = "INSERT INTO test_table (person, object, location) VALUES (?, ?, ?)";
$stmt = $mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('sss', $person, $object, $location);
$person = $_POST['person'];
$object = $_POST['object'];
$location = $_POST['location'];
$results = $mysqli->query($query);
$stmt->execute();
$stmt->close();
/* No need a new mysqli object, so i block some lines from your script
$mysqli->close();
?>
<?php
$mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
if($mysqli->errno) {
printf("Connection To Database Failed:<br/> %s", $mysqli->error());
die();
};
*/
$query = "SELECT person, object, location FROM test_table WHERE person = ?";
$stmt = $mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('s', $person); // The query just need one parameter
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
printf('<strong>%s</strong> is a person. <strong>%s</strong> is an object. <strong>%s</strong> is a location.', $row['person'], $row['object'], $row['location']);
} // Don't forget to close a statement
$stmt->close();
$mysqli->close();
?>
그게 다야. 나는 전에 그것을 시도했고 모든 것이 작동합니다. 이것이 당신을 도울 수 있기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다