我已经成为一个班级雇员,并且具有select函数,该函数应该将值返回到add_data.php并使用该数据,它应该位于select的选项标签中,该标记是国家/地区和城市的ajax。
employee.php
<?php
class employees{
function __construct(){
try {
$this->con= new PDO("mysql:host=myserver;dbname=dhruv_thakkar", 'uname', 'pass');
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
function insert($name,$email,$password,$birth_date,$gender,$postcode,$phno,$street_address){
try {
$name=$this->name=$name;
$email=$this->email=$email;
$password=$this->password=$password;
$birth_date=$this->birth_date=$birth_date;
$gender=$this->gender=$gender;
$postcode=$this->postcodde=$postcode;
$phno=$this->phno=$phno;
$street_address=$this->street_address=$street_address;
$d1 = date('Y-m-d',(strtotime($birth_date)));
$sql = "INSERT INTO employees(name, email,gender,password,street_address,postcode,phone_number,profile_pic,status,register_date,birth_date)
VALUES ( '$name', '$email','$gender ','$password','$street_address','$postcode','$phno','pic123','1',NOW(),'$d1')";
$this->con->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$this->con= null;
}
function select_country(){
$sql= "SELECT *from country";
$result =$this->con->query($sql);
return $result;
}
}
$obj= new employees();
?>
add_data.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'employee.php';
session_start();
$username=$_SESSION['username'];
$password=$_SESSION['password'];
if(isset($_SESSION['username'])){
if(isset($_POST['logout'])){
session_destroy();
header('location:index.php');
}
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];
$birth_date=$_POST['birth_date'];
echo $gender=$_POST['gender'];
$postcode=$_POST['postcode'];
$phno=$_POST['phno'];
$street_address=$_POST['street_address'];
$obj->insert($name,$email,$password,$birth_date,$gender,$postcode,$phno,$street_address);
}
}
else {
header('location:index.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<style>
.sidebar-sticky{
border-right: 3px solid #aaacaf;
height:1000px;
}
.col-centered{
align-items: center;
}
</style>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Admin Panel</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
<form action="" method="post" class="form-inline my-2 my-lg-0">
<input type="submit" value="logout" name="logout" class="btn btn-outline-danger my-2 my-sm-0 ">
</form>
</div>
</nav>
<!--Side Bar Now -->
<div class="container-fluid sidea">
<div class="row">
<nav class="col-md-2 d-none d-md-block bg-light sidebar">
<?php
include 'sidebar.php';
?>
</nav>
<div class="col-sm-10">
<div class="container">
<h2>Add User Here</h2>
<br>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" class="form-control" placeholder="Enter Name" name="name">
<br><br>
<input type="text" class="form-control" placeholder="Enter Email" name="email">
<br><br>
<input type="text" class="form-control" placeholder="Enter Password" name="password">
<br><br>
<label>Upload Your Profile Picture Here</label>
<input type="file" name="photo">
<br><br>
<label>Enter Your BirthDate</label>
<input type="date" class="form-control" name="birth_date">
<br><br>
<label>Select Gender</label> <br>
<label><input type="radio" value="M" name="gender">Male</label> <br>
<label><input type="radio" value="F" name="gender">Female</label>
<br><br>
<input type="text" class="form-control" placeholder="Enter Street Address" name="street_address">
<br><br>
<?php
?>
<select value="" id="country">
<?php
$result=$obj->select_country();
print_r($data);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['country_id']; ?>">
<?php
echo $row['country_name'];
?>
</option>
<?php
}
}
else{
echo '<option value="">Country not available</option>';
}
?>
</select>
<select id="state">
<option value="">Select country first</option>
</select>
<select id="city">
<option value="">Select state first</option>
</select>
<br><br>
<input type="text" class="form-control" placeholder="Postcode" name="postcode">
<br><br>
<input type="text" class="form-control" placeholder="Phone No" name="phno">
<br><br>
<input type="submit" class="btn btn-success" value=" Add Data" name="submit">
</form>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#country").change(function(){
var countryID = $(this).val();
if(countryID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'country_id='+countryID,
success:function(html){
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
}
});
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'state_id='+stateID,
success:function(html){
$('#city').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
我的国家/地区未显示。 它总是进入其他循环并显示国家/地区不可用。
答案 0 :(得分:1)
您正在混合mysqli
和PDO
数据库扩展名。选择一个并保持一致。
以以下方式更改select()
类和employees
块的<select>...</select>
方法,
class employees{
...
function select_country(){
$sql= "SELECT *from country";
$result =$this->con->query($sql);
return $result->fetchAll();
}
...
}
和
<select value="" id="country">
<option value="">Select a country<option>
<?php
$result = $obj->select_country();
$count = count($result);
// print_r($data);
if ($count > 0) {
// output data of each row
foreach($result as $row){
?>
<option value="<?php echo $row['country_id']; ?>">
<?php
echo $row['country_name'];
?>
</option>
<?php
}
}else{
echo '<option value="">Country not available</option>';
}
?>
</select>
边注::了解prepared statement,因为目前您的查询容易受到SQL注入攻击。另请参见how you can prevent SQL injection in PHP。