서버에 저장 한 MySQL 데이터베이스에 데이터를 보내려고합니다. 제출 버튼을 누르면 PHP 스크립트가 DB에 성공적으로 연결되었음을 알려주지 만 데이터 삽입은 성공하지 못했습니다. 오류 로그에서 문제를 찾을 수 없거나 Google Chrome의 개발자 도구를 통해 코드를 검사 할 때 명백한 문제를 찾을 수 없습니다.
아래는 HTML 파일입니다.
<html>
<head>
<title>Home</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="http://example.com/JS/functions.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
//CSS
<style>
.ui-page {
background: #4990E2;
}
p {
color: #ffffff;
}
</style>
</head>
div data-role="page" id="page1">
<div data-role="header" data-theme="a">
<h1>Welcome</h1>
</div>
<!-- Main page content goes under this line -->
<div data-role="main" class="ui-content">
<br>
<br>
<br>
<form id="myForm" method="post" action="include.php">
<label for="rating">How are you feeling?</label>
<input type="range" name="rating" id="rating" value="50" min="0" max="100" data-popup-enabled="true">
<input id="submit" type="submit" data-inline="true" value="Submit">
</form>
<br>
<br>
<span id="result"></span>
<br>
<p>Some text</p>
</div>
<div data-role="footer" data-position="fixed" data-theme="b">
<div data-role="navbar">
<ul>
<li><a href="#page1" class="ui-btn-active ui-state-persist" data-transition="none">Home</a></li>
<li><a href="#page2" data-transition="none">Example</a></li>
<li><a href="#page3" data-transition="none">Example</a></li>
<li><a href="#page4" data-transition="none">Example</a></li>
</ul>
</div>
</div>
</div>
<!-- Page ends here -->
<!-- -->
아래는 PHP 파일 (db.php)입니다.
<?php
//put your connection code here
$servername = 'localhost';
$username = 'user_admin';
$password = '12345-678';
$dbname = 'my_DB';
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully, <br>";
?>
아래는 PHP 파일 (include.php);
<?php
include 'db.php';
$rating = $_POST['rating'];
$sql="INSERT INTO user Values('$rating')";
if(mysqli_query($conn,$sql)){
echo "Successfully Inserted";
} else {
echo "Did NOT insert";
}
?>
아래는 자바 스크립트 파일 (functions.js)입니다.
// AJAX-ify the slider
$(document).on("pagecreate","#page1",function()
{
$("#submit").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);
});
});
$("#myForm").submit( function() {
return false;
});
});
MySQL 테이블을 'user'라고하고 열을 'rating'이라고합니다. 열은 int (3) [슬라이더 최대 값 100과 일치해야합니까?]로 설정됩니다.
데이터가 DB에 삽입되지 않는 이유를 누구든지 정확히 지적 할 수 있습니까?
이것을 변경
$ sql = "INSERT INTO user Values ( '$ rating')";
이에
$sql="INSERT INTO user (`rating`) VALUES ('$rating')";
rating은 db의 해당 필드에 대한 열 이름이 될 것입니다. 테이블에 둘 이상의 열이있는 경우 명령문은 어떤 열에 배치해야하는지 알려야합니다.
또한 문장의 오류를 표시하려면 이것을 사용하십시오.
if(mysqli_query($conn,$sql) or die(msqli_error($conn))){
... your code here > it worked
}else{
... your code here > it failed
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다