从jQuery的传递变量创建数组PDO数组、变量、jQuery、PDO

2023-09-10 14:50:40 作者:是我闯入你的生活

这是PHP code我现在已经从我的数据库计数:

This is the PHP code I have now to get counts from my database:

$hostname = '****';
$username = '****';
$password = '****';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=firstdb", $username, $password);

echo 'Connected to database<br />';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

/*** some example variables ***/
$firstpara = 'age';
$secondpara = 'marital_status';
$thirdpara = 'gender';
$data = array($firstpara=>55, $secondpara=>'single', $thirdpara=>'male');

/*** prepare the SQL statement ***/
$stmt = $dbh->prepare("SELECT COUNT(guid) FROM full_db2 WHERE {$firstpara} = :{$firstpara} AND {$secondpara} = :{$secondpara} AND {$thirdpara} = :{$thirdpara}");

$stmt->execute($data);

$count =$stmt->fetch();
    echo json_encode($count);

/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

这成功地让我含在有我的变量设置上面的属性数据库用户的数量JSON($ firstpara,$ secondpara,$ thirdpara)。

This successfully gives me JSON containing the count of users in the database that have the attributes set in my variables above ($firstpara, $secondpara, $thirdpara).

当然,这是所有硬codeD和我想要做的就是让用户选择,包含了不同的属性选项(例如,婚姻状况,性别),并基于该给他们可能的选择框在第二个值选项中进行选择(如单)。我是动态生成这些选择框没有问题,可以存储的选择作为变量。我可以将它们传递到AJAX发送到PHP文件,但我不知道如何分析它们放在一起。像这样发送的变量。     VAR PARA1 =男性; //从选择值这个抓获     //选择的更多的属性。

Of course, this is all hard-coded and what I want to do is have the user pick from select boxes containing the different attribute options (e.g., marital status, gender) and based on that give them the possible value options in a second select (e.g., single). I'm generating these select boxes dynamically no problem and can store the selections as variables. I can pass them into AJAX to send to the PHP file, but I have no idea how to parse them all together. Something like this to send the variables. var para1 = "male"; //this captured from select value //any more attributes selected

$.ajax({
        url: 'all_get_2.php',
        type: 'GET',
        dataType: 'JSON',
        data: {firstpara: para1, secondpara: para2 ,thirdpara: para3},
        success: function(data) {
       //do something
      }
      });

而在我的PHP,我可以添加行这样的设置变量:

And in my PHP I can add lines like this to set variables:

  $firstpara = isset($_GET['firstpara'])? "{$_GET['firstpara']}" : '';
  $firstpara = mysql_real_escape_string($firstpara);

这里的问题是,我不知道有多少属性(例如,婚姻状况)的用户可以选择。它们可能使用两种或10或某些其它数目。所以,我的问题是这样的:

The problem here is that I don't know how many attributes (e.g., marital status) the user might select. They might use two or ten or some other number. So, my questions are this:

我怎样才能将数据发送到PHP(安全......所以没有注射可能)只是说,我可以动态地添加合适数量的 $ firstpara 变量一样考虑到一切,AJAX顺利通过包括添加正确数量的where子句在SQL语句?

How can I send the data to PHP (securely...so there are no injections possible) just that I can dynamically add the right number of $firstpara like variables to account for everything that AJAX passed including adding the right number of where clauses into the SQL statement?

我猜,需要有某种形式的/每个回路或相似的,但我只是不能与PHP非常熟悉,知道怎么做了。

I'm guessing there needs to be some sort of for/each loop or similar, but I'm just not familiar enough with PHP to know how to do it.

任何方向将AP preciated。

Any direction would be appreciated.

推荐答案

我不知道如果我在正确的方式得到了你的问题,但你可以尝试(或评论,如果我听错了)。

I am not sure if I got your problem in correct way but you can try (or comment if I got it wrong).

替换thise片段:

$stmt = $dbh->prepare("SELECT COUNT(guid) FROM full_db2 WHERE {$firstpara} = :{$firstpara} AND {$secondpara} = :{$secondpara} AND {$thirdpara} = :{$thirdpara}");

$stmt->execute($data);

这一点:

$validKeys = array('gender','maritalstatus', 'age');
    $sql = 'SELECT COUNT(guid) FROM full_db2';
    $any_condition = false;
    foreach($_GET as $key=>$val) {
       if (!empty($val) && in_array($key,$validKeys)) {
         if ($any_condition) {
           $sql .= ' AND '.$key.' = :'.$key;
         } else {
           $sql .= ' WHERE '.$key.' = :'.$key;
           $any_condition = true;
         }
       }
    }

    $stmt = $dbh->prepare($sql);

    foreach($_GET as $key=>$val) {

   if (!empty($val)  && in_array($key,$validKeys)) {
     $stmt ->bindValue(':'.$key, $val, PDO::PARAM_STR);
   }
}

$stmt->execute();

你的Ajax请求就在这里读到:使用提交表单的jQuery

在您的Ajax请求改变行格式: 数据:{firstpara:PARA1,secondpara:PARA2,thirdpara:para3}

in your ajax request change line format from: data: {firstpara: para1, secondpara: para2 ,thirdpara: para3}

数据:{年龄:PARA1,maritalstatus:PARA2,性别:para3}