【问题标题】:Escaping apostrophe in a dynamically created array in PHP在 PHP 中动态创建的数组中转义撇号
【发布时间】:2017-05-18 19:02:08
【问题描述】:

我正在开展一个项目,以在 PostgreSQL 数据库中混合多个不同的数据集。我仍然认为自己是 PHP 开发和脚本的初学者。我在转义数组中的撇号时遇到了一些真正的麻烦。我从这些论坛尝试了一些不同的解决方案:An escaped apostrophe in associative array valueReplace apostrophe in a dynamically created insert statementhttp://www.codingforums.com/php/296075-array_walk_recursive-w-function-takes-2-parameters-mysqli_real_escape_string.html,最后是Escaping quotation marks in PHP。我目前正在尝试使用 PDO 版本重新创建我的脚本,因此我不必清理我的文本。至少这是我所理解的,这是我所做的所有研究中更好的方法。我目前正在寻找的是一种在找到更雄辩的解决方案时转义字符的方法。这是我用于导入过程的主要代码:

<?php


include('connect_local.php'); //Includes DB Connection Script
ini_set('max_execution_time', 3000); //3000 seconds = 50 minutes

$emp_get = "SELECT * FROM table1 WHERE person_type LIKE 'Employee'";
$emp_data = pg_query($conn, $emp_get);
while ($emp_row=pg_fetch_array($emp_data)) {


$oraint_get = "SELECT * FROM table2 WHERE source_enrollment_status_name LIKE  'Attended' AND employee_number LIKE '$emp_row[0]' ";
$oraint_data = pg_query($conn, $oraint_get);
$oraint_lms = "Oracle Learning Management Platform";
$oranull = "";


//foreach ($oraint_row as $oraint)
while ($oraint_row = pg_fetch_array($oraint_data)){




    $data_deposit = "INSERT INTO EDU_DATA (person_number, person_name, preferred_name, person_type, start_date, original_date_of_hire
                                 ,hire_date, email_address, region, location, gender, job_name, cbs_level, supervisor_employee_number
                                 ,supervisor_name, supervisor_person_type, business_unit, organization_2, organization_3, effective_date
                                 ,completion_date, training_item_code, days_on_to_do_list, days_overdue, initial_due_in, initial_due_in_unit 
                                 ,retraining_due_in, retraining_due_in_unit, retraining_period, retraining_period_unit 
                                 ,curriculum_code, curriculum_title, learning_course_name, learning_activity, class_duration, college, delivery_method_name
                                 ,class_location_name, class_location_country_name, learning_category, source_enrollment_status_name, lms_platform
                                 ,supervisor_1 ,supervisor_2, supervisor_3, supervisor_4, supervisor_5, supervisor_6, supervisor_7, supervisor_8) 
                          VALUES ('$emp_row[0]','$emp_row[1]','$emp_row[2]','$emp_row[4]','$emp_row[5]','$emp_row[6]','$emp_row[8]'
                                  ,'$emp_row[9]','$emp_row[16]','$emp_row[17]','$emp_row[19]','$emp_row[21]','$emp_row[22]','$emp_row[28]'
                                  ,'$emp_row[29]','$emp_row[30]','$emp_row[33]','$emp_row[44]','$emp_row[45]','$oraint_row[2]','$oraint_row[3]'
                                  ,'$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull','$oranull'
                                  ,'$oranull','$oraint_row[4]','$oraint_row[5]','$oraint_row[6]','$oraint_row[7]','$oraint_row[8]','$oraint_row[9]'
                                  ,'$oraint_row[10]','$oraint_row[11]','$oraint_row[12]','$oraint_lms','$emp_row[46]','$emp_row[47]','$emp_row[48]'
                                  ,'$emp_row[49]','$emp_row[50]','$emp_row[51]','$emp_row[52]','$emp_row[53]')";
    pg_query($conn, $data_deposit);

在我尝试清理文本时,我尝试将数组输出转换为字符串,然后使用添加斜杠但没有任何成功:

$clnname = $emp_row[1];
addslashes($clnname);

我还尝试使用我在此处找到的示例创建一个函数来递归处理这个问题:Escape single quotes in every string in php。代码sn-p如下:

 function escapeApos(array $emp_row)
{

    $return_array = [];
    array_walk_recursive($emp_row, function($x) use (&$return_array)
    {

        $return_array[] = str_replace("'","\\'",$x);

    }   

    return $return_array;
}

我还尝试了其他一些方法,但均未成功。任何帮助或协助将不胜感激。同样使用上述函数,我不确定是否需要在数组中声明我想要清理的实际列。再次欢迎任何帮助!提前谢谢!

【问题讨论】:

  • 你试过mysqli_real_escape_string()吗?
  • 那张桌子看起来应该有五张左右。
  • 我不了解 PDO,但您必须规范化该数据库并绑定/清理您的插入查询,它会自动为您完成。
  • 我愿意,但我的数据操作是在 PostgreSQL 数据库上进行的。我确实查找了我认为是 PostgreSQL 版本并收到此错误消息:“pg_query(): Query failed: ERROR:” 这是我尝试的代码 sn-p:$data = $emp_row[1]; $cln_name = pg_escape_string($data);
  • 使用准备好的语句而不是替换变量。请参阅 pg_prepare()pg_execute(),或使用 PDO。

标签: php arrays escaping


【解决方案1】:

好的,非常感谢大家帮助我!我开始使用 PDO 重新创建脚本,而不是我采用的第一种方法。这是脚本的示例,我还有一些工作要做。但是,既然我正在使用 PDO,那么清理文本的问题就不再是问题了。以后我就用这个方法了!

<?php

include('connect_local_pdo.php'); //Includes DB Connection Script
ini_set('max_execution_time', 3000); //3000 seconds = 50 minutes

try {
    $stmt = $conn->query('SELECT * FROM table1');
    $rows = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    while ($rows = $stmt->fetch()) {

        $emp_id = $rows['person_number'];

        $stmt2 = $conn->query("SELECT * FROM table2 WHERE employee_number LIKE '$emp_id'");
        $oracleint = $stmt2->setFetchMode(PDO::FETCH_ASSOC);

        while ($oracleint = $stmt2->fetch()) {



            $GO = $conn->prepare("INSERT INTO table3 (person_number, person_name, learning_course_name) VALUES (:emp_number, :emp_name, :learning_course_name)");

            $GO->bindParam(':emp_number', $rows['person_number']);
            $GO->bindParam(':emp_name', $rows['person_name']);
            $GO->bindParam(':learning_course_name', $oracleint['learning_course_name']);

            $GO->execute();

        }


        }

    } catch (PDOException $b) {
        echo 'Data Extraction Failed: ' . $b->getMessage();

    }

再次感谢您对新手的帮助!我非常喜欢 StackExchange!你们摇滚!

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2012-05-27
    相关资源
    最近更新 更多