Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/480.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript PDO正在向数据库中插入ID而不是名称_Javascript_Php_Html - Fatal编程技术网

Javascript PDO正在向数据库中插入ID而不是名称

Javascript PDO正在向数据库中插入ID而不是名称,javascript,php,html,Javascript,Php,Html,所以我有点困惑,我做了两个下拉列表,它们正在工作。问题是,当我在表中插入数据时,下拉元素的ID而不是它们的名称。我很确定我需要用另一种方式来做,但我是一个业余爱好者,所以任何解释都是值得的。 这是我的密码: <?php session_start(); error_reporting(E_ALL); ini_set('display_errors', 1); include('includes/config.php'); if(strlen($_SESSION['alogin'])==0)

所以我有点困惑,我做了两个下拉列表,它们正在工作。问题是,当我在表中插入数据时,下拉元素的ID而不是它们的名称。我很确定我需要用另一种方式来做,但我是一个业余爱好者,所以任何解释都是值得的。 这是我的密码:

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
    {   
header('location:index.php');
}
else{
if(isset($_POST['submit']))
{

$id=null;
$name=$_POST['name'];
$email=$_POST['email'];
$user=$_POST['user'];
$password=md5($_POST['password']);
$mobileno=$_POST['mobileno'];
$bloc=$_POST['industry_name'];
$apartament=$_POST['sub_industry_name'];
$intretinere=$_POST['intretinere'];
$m2=$_POST['m2'];

$notitype='Create Account';
$reciver='Admin';
$sender=$email;

$sqlnoti="insert into notification (notiuser,notireciver,notitype) values (:notiuser,:notireciver,:notitype)";
$querynoti = $dbh->prepare($sqlnoti);
$querynoti-> bindParam(':notiuser', $sender, PDO::PARAM_STR);
$querynoti-> bindParam(':notireciver',$reciver, PDO::PARAM_STR);
$querynoti-> bindParam(':notitype', $notitype, PDO::PARAM_STR);
$querynoti->execute();    

$sql ="INSERT INTO users(id, name, email, user, password, mobile, bloc, apartament, intretinere, m2, status) VALUES(:id, :name, :email, :user, :password, :mobileno, :industry_name, :sub_industry_name, :intretinere, :m2, 1)";
$query= $dbh -> prepare($sql);
$query-> bindParam(':id', $id, PDO::PARAM_STR);
$query-> bindParam(':name', $name, PDO::PARAM_STR);
$query-> bindParam(':email', $email, PDO::PARAM_STR);
$query-> bindParam(':user', $user, PDO::PARAM_STR);
$query-> bindParam(':password', $password, PDO::PARAM_STR);
$query-> bindParam(':mobileno', $mobileno, PDO::PARAM_STR);
$query-> bindParam(':industry_name', $bloc, PDO::PARAM_STR);
$query-> bindParam(':sub_industry_name', $apartament, PDO::PARAM_STR);
$query-> bindParam(':intretinere', $intretinere, PDO::PARAM_STR);
$query-> bindParam(':m2', $m2, PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
echo "<script type='text/javascript'>alert('Registration Sucessfull!');</script>";
echo "<script type='text/javascript'> document.location = 'adduser.php'; </script>";
}
else 
{
$error="Something went wrong. Please try again";
}

}
?>

<script>
$(document).ready(function(){

  $('#category_item').selectpicker();

  $('#sub_category_item').selectpicker();

  load_data('category_data');

  function load_data(type, category_id = '')
  {
    $.ajax({
      url:"../admin/load_data.php",
      method:"POST",
      data:{type:type, category_id:category_id},
      dataType:"json",
      success:function(data)
      {
        var html = '';
        for(var count = 0; count < data.length; count++)
        {
          html += '<option value="'+data[count].id+'">'+data[count].name+'</option>';
        }
        if(type == 'category_data')
        {
          $('#category_item').html(html);
          $('#category_item').selectpicker('refresh');
        }
        else
        {
          $('#sub_category_item').html(html);
          $('#sub_category_item').selectpicker('refresh');
        }
      }
    });
  }

  $(document).on('change', '#category_item', function(){
    var category_id = $('#category_item').val();
    load_data('sub_category_data', category_id);
  });

});
</script>

<div class="form-group">
<label class="col-sm-2 control-label">Bloc<span style="color:red">*</span></label>
<div class="col-sm-4">
<select name="industry_name" id="category_item" class="form-control" required title="Selecteaza Bloc">
<option value="">Selecteaza Bloc</option>
</select>
</div>
<label class="col-sm-2 control-label">Apartament<span style="color:red">*</span></label>
<div class="col-sm-4">
<select name="sub_industry_name" id="sub_category_item" class="form-control" required data-live-search="true" title="Selecteaza Apartament">
<option value="">Selecteaza Apartament</option>
</select>
</div>
</div>

您说要在下拉列表中插入项目的ID,因为这是您在
load\u data()
中设置的

而不是

 html += '<option value="'+data[count].id+'">'+data[count].name+'</option>';
然后使用jQuery的
$。每个
函数在AJAX调用后读取数据:

$.each( data, function( key, value ) {
   html += '<option value="'+value+'">'+value+'</option>';
});
$。每个(数据、函数(键、值){
html+=''+值+'';
});

请参见

您真的不应该使用PHP来处理密码安全问题。在散列之前,请确保对它们进行了清理或使用了任何其他清理机制。这样做会更改密码并导致不必要的额外编码。感谢@JayBlanchard的建议,我刚刚查看了您建议的文档链接,我将明确尝试更改散列密码的方式。但首先,我想解决我的问题的目标。这是很多代码。你试图找出哪里出了问题?糟糕的是,我忘了添加代码的那一部分,我只是编辑了我的问题。我也尝试了你的建议,但是使用它不会在第二个下拉列表中显示元素的名称。我更新了我的答案,包括你的数据格式。这解决了问题吗?抱歉@user1517599,但不幸的是,我今天还没有机会尝试。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('includes/config.php');

if(isset($_POST["type"]))
{
 if($_POST["type"] == "category_data")
 {
  $query = "
  SELECT * FROM tbl_industry 
  ORDER BY industry_name ASC
  ";
  $statement = $dbh->prepare($query);
  $statement->execute();
  $data = $statement->fetchAll();
  foreach($data as $row){
  $output[$row["industry_id"]] = $row["industry_name"];
}
  echo json_encode($output);
 }
 else
 {
  $query = "
  SELECT * FROM tbl_sub_industry 
  WHERE industry_id = '".$_POST["category_id"]."' 
  ORDER BY sub_industry_name ASC
  ";
  $statement = $dbh->prepare($query);
  $statement->execute();
  $data = $statement->fetchAll();
  foreach($data as $row){
  $output[$row["sub_industry_id"]] = $row["sub_industry_name"];
}
  echo json_encode($output);
 }
}

?> 
 html += '<option value="'+data[count].id+'">'+data[count].name+'</option>';
 html += '<option value="'+data[count].name+'">'+data[count].name+'</option>';
foreach($data as $row){
   $output[$row["industry_id"]] = $row["industry_name"];
}
$.each( data, function( key, value ) {
   html += '<option value="'+value+'">'+value+'</option>';
});