본문 바로가기
php/개인공부

mysqli // php + insert

by SEOKIHOUSE 2023. 10. 31.

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
 </head>
 <body>
	<h1>글 작성페이지</h1>
	<div>
		<input type="text" name="title" placeholder="제목을 입력하세요"><br>
		<input type="text" name="content" placeholder="내용을 입력하세요" style="width:300px; height:300px">

		<button type="button" id="btn">글 작성</button>
		<script>
			$('#btn').click(function() {
				$.ajax({
					url : "writeActive.php",
					method : "post",
					data : {
						title : $('input[name=title]').val(),
						content : $('input[name=content]').val()
						
					},
					dataType : "",
					success:function(response) {
						if(response == "success") {
							alert("글 작성 완료!");
							location.href= "board.php";
						}
					}

				});
			});
		</script>



	</div>
 </body>
</html>
<?php
	$servername = "localhost";
	$username = "root";
	$password = "1234";
	$dbname = "practice";

	$conn = new mysqli($servername, $username, $password, $dbname);

	if($conn->connect_error) {
		die("연결 실패: " . $conn->connect_error);
	}

	$title = $_POST['title'];
	$content = $_POST['content'];

	$sql = "INSERT INTO board VALUES (null, ?,?,'d',now(),0)";
	
	$stmt = $conn->prepare($sql);
	$stmt->bind_param("ss", $title, $content);
	
	if($stmt->execute()) {
		echo "success";   
	}


?>