0

データベースに配列を挿入するとエラーが発生します。 エラー: SQL 構文にエラーがあります。使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルの 23 行目の「visiting students should Consult thestudents should reference the related section」付近を確認してください。

これが以下の配列です

array
 'Choose by Subject Category or Module Code' => string '' (length=0)
 '
Back to Home page' => string '' (length=0)
 'International' => string 'visiting students should consult the' (length=36)
 'Undergraduate' => string 'students should refer to the relevant section of the UCC' (length=56)
 'Postgraduate' => string 'students should refer to the relevant section of the UCC' (length=56)
 'Credit Weighting' => string '5' (length=1)
 'Teaching Period(s)' => string 'Teaching Period 1.' (length=18)
 'No. of Students' => string 'Min 15, Max 30.' (length=15)
 'Pre-requisite(s)' => string 'None' (length=4)
 'Co-requisite(s)' => string 'None' (length=4)
 'Teaching Methods' => string '1 x 4hr(s) Lectures; Other (Distance Education Module - Up to 146hrs Self Directed Study).' (length=90)
 'Module Co-ordinator' => string 'Dr Peter Cleary, Department of Accounting, Finance and Information Systems.' (length=75)
 'Lecturer(s)' => string 'Staff, Department of Accounting, Finance and Information Systems.' (length=65)
 'Module Objective' => string 'To examine the management uses of accounting information and to enhance students ability to exert effective managerial control.' (length=127)
 'Module Content' => string 'Topics include; the accounting information needs of management, costs and pricing; estimating costs; the identification of key performance indicators; budgeting for control; capital investment appraisal and  implications for strategic planning and control.' (length=256)
 'Learning Outcomes' => string 'On successful completion of this module, students should be able to:' (length=68)
 'Assessment' => string 'Total Marks 100: Continuous Assessment 100 marks (Project/ Essay. Approximately 1500 words.).' (length=93)
 'Compulsory Elements' => string 'Continuous Assessment.' (length=22)
 'Penalties (for late submission of Course/Project Work etc.)' => string 'Where work is submitted up to and including 7 days late, 10% of the total marks available shall be deducted from the mark achieved.  Where work is submitted up to and including 14 days late, 20% of the total marks available shall be deducted from the mark achieved.  Work submitted 15 days late or more shall be assigned a mark of zero.' (length=336)
 'Pass Standard and any Special Requirements for Passing Module' => string '40%.' (length=4)
 'End of Year Written Examination Profile' => string 'No End of Year Written Examination.' (length=35)
 'Requirements for Supplemental Examination' => string 'Marks in passed element(s) of Continuous Assessment are carried forward, Failed element(s) of Continuous Assessment must be repeated (Resubmission of revised Continuous Assessment).' (length=181)

以下はクエリです。

//============== INSERT QUERY================//
$result = array();      
foreach($result as $snode){ 
$query = sprintf("INSERT INTO save_array 
       (ModuleCode,
        Homepage,
        International,
        ......) VALUES ('%s')",mysql_real_escape_string($snode)); 


foreach ($result as $key => $value) 
$query = $query . "$value"; 

 echo '<br /><br />'; 
mysql_query($query) or die($query."<br/><br/>".mysql_error());  
echo $snode. '<br />'; 
}
echo '<br /><br /><br />'; 

これを理解するための助けをいただければ幸いです。

//================== New Updated Query Using Mysqli =============================

$result = array();
foreach($result as $snode){ 
$snode = mysql_real_escape_string($snode);
$query = sprintf("INSERT INTO save_array 
       (ModuleCode,Homepage,International,.......)VALUES ('%s')",implode("','",$result)); 

echo $query. '<br />'; 

foreach ($result as $key => $value) 
    $query = $query . "$value"; 
$result = mysql_query($query) or die (mysql_error());
}

クエリをエコーし​​、正しい値を正しい列に挿入しているようですが、データベースに実行していないようです。

エラー: SQL 構文にエラーがあります。使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

4

4 に答える 4

1

$query をエコーすると表示されます。有効な SQL ステートメントではありません。

動的クエリをデバッグするためのルール番号 1、2、および 3: クエリ自体を見てください。

于 2012-06-27T09:37:34.980 に答える
1

複数の列に保存しようとしています:

ModuleCode,
Homepage,
International,
Undergraduate,
...

単一の値で('%s')

また、配列ではなく単一の値をmysql_real_escape_string取ることに注意してください( $ snode配列であると想定しています)。またはの使用も検討してください。PDOmysqli

あなたは(例えば、 $snode 構造を知らない)、出力をチェックすることができます:

foreach($snode as &$val) {
   $val = mysql_real_escape_string($val);
}
...VALUES ('%s'),implode("','",$snode)

更新しました:

問題が見つかりません。クエリが機能するはずです。システムにテーブルの構造を作成し(すべての列のVARCHAR(256)を想定)、クエリ出力が期待どおりに機能(挿入)しました..

$result = array();
foreach($result as $snode) { 

   foreach($snode as &$val) {
      $val = mysql_real_escape_string($val);
   }

   $query = sprintf("INSERT INTO save_array (
        ModuleCode,Homepage,International,Undergraduate,Postgraduate,CreditWeighting, 
        TeachingPeriod,NoofStudents,Prerequisite,Corequisite,TeachingMethods, 
        ModuleCoordinator,Lecturer,ModuleObjective,ModuleContent,LearningOutcomes, 
        Assessment,CompulsoryElements,Penalties,PassStandard, 
        EndofYearWrittenExamination,RequirementsforExamination) 
        VALUES ('%s')",implode("','",$snode)); 

   $result = mysql_query($query) or die (mysql_error());
}

上記のスニペットをそのまま実行します。何も変更しないでください。

于 2012-06-27T09:40:15.853 に答える
0

$node配列が表示された列と同じ順序であると仮定すると、vsprintf()代わりに使用して、結果のクエリを生成できます。

// assuming $node is the array with the data
// generate list of place holders
$placeholders = join(',', array_fill(0, count($node), "'%s'"));

// construct full query using array_map applied to the escaping function
$query = vsprintf("INSERT INTO save_array (ModuleCode,
        Homepage,
        International,
        Undergraduate,
        Postgraduate,
        CreditWeighting,
        TeachingPeriod,
        NoofStudents,
        Prerequisite,
        Corequisite,
        TeachingMethods,
        ModuleCoordinator,
        Lecturer,
        ModuleObjective,
        ModuleContent,
        LearningOutcomes,
        Assessment,
        CompulsoryElements,
        Penalties,
        PassStandard,
        EndofYearWrittenExamination,
        RequirementsforExamination) VALUES ($placeholders)", 
            array_map('mysql_real_escape_string', $node)
);

ところで、関数を使用しないでくださいmysql_!

于 2012-06-27T09:50:55.750 に答える
0

あなたの主な問題は引用符です:

('%s')

そして、あなた:

mysql_real_escape_string

SQLで競合が発生します。MySQL エスケープまたは '.

何が起こっているのかというと、実際にSQLインジェクションを引き起こしているSQL入力のダブルエスケープです...

また、MYSQL の実際のエスケープ文字列は、$snode を配列として挿入しません。値を抽出する配列と、SQL クエリに挿入する配列を foreach する必要があります。

于 2012-06-27T09:40:37.950 に答える