سلام استاد ببخشید من هر کار میکنم نمیتونم توی جدول city اطلاعاتی وارد کنم :
۱- بخش html , ajax :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Save City</title>
<style>
@font-face{
font-family: Sahel;
src: url('font/Sahel.eot');
src: url('font/Sahel.eot?#iefix') format(Sahel-opentype),
url('font/Sahel.woff') format('wff'),
url('font/Sahel.ttf') format('truetype');
font-weight: bold;
font-style: normal;
}
body{
text-align: center;
}
input#city, button {
line-height: 30px;
margin-top: 200px;
padding: 0 30px;
border-radius: 5px;
border: 1px solid #81b7d0;
font-family: Sahel;
font-weight: bold;
}
input{
direction: rtl;
}
</style>
</head>
<body>
<form action="ajax-save-city.php" method="post" id="cityform">
<button type="submit">افزودن شهر</button>
<input type="text" name="city" id="city">
<select name="province-id" id="province-id">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="result-tag"></div>
</form>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
var form = $('#cityform');
var selectTag = $('#province-id').val();
var city = $('#city').val();
var resultTag = $('#result-tag');
form.submit(function(event){
event.preventDefault();
$.ajax({
url : 'ajax-save-city.php',
method : 'post',
data : {
city : city,
provinceid: selectTag
},
success : function(response){
resultTag.html(response);
}
});
});
});
</script>
</body>
</html>
۲- و اینم بخش ذخیره در پایگاه داده:
<?php
#Input data from input field
$city = $_POST['city'];
$provinceid = $_POST['provinceid'];
#Connect to mysql
$mysql = new mysqli("localhost","root","","iran");
#Check connection
if($mysql->connect_error){
echo "Failed to connect to MySQL : " . $mysql->connect_error , 'Error';
die();
}
$mysql->set_charset('utf8');
#insert into database
$sql = "insert into city (name,province_id) values (?,?)";
$stmt = $mysql->prepare($sql);
$stmt->bind_param('si',$city,$provinceid);
$stmt->execute();
$stmt->close();
echo "شهر $city با موفقیت به استان $provinceid اضافه شد!";
استاد در واقع خطایی نشون نمیده حتی پیام با موفقیت ثبت شد هم نمایش میده ولی چیزی توی جدول city اضافه نمیشه