2

私はこのコードを持っています(これは機能しており、これらの変数を別のファイルに渡します)

            var month = "<?php echo openedMonthbid();?>";
    var user = "<?php echo $_SESSION['member_id'];?>";
    var day = new Array();


    $(':checkbox:checked').each(function(i){
    day.push('`' + $(this).val() + '`');  });
    var count = day.length;

                            $.ajax({
                            type: "POST",
                            url: "sendBidding.php",
                            data : "user="+user+"&days="+day+"&month="+month+"&number=",
                            dataType: "json",

sendBidding.php

$month = $_POST['month'];
$user = $_POST['user'];
$days = $_POST['days'];
$count = $_POST['count'];//if a check 3 values I get '3'


      mysql_query("INSERT INTO $month ($days) VALUES ('1','1','1')");


    $result = true;

    echo json_encode(array("success"=>$result,
                               "datas" => $data,
                                "mon"=>$month));

選択した日数と同じ数の値 ('1') を追加したいと思います。VALUES ('1','1','1') を変更するにはどうすればよいですか?

4

2 に答える 2

2

これは、同一の文字列のシーケンスを生成するためのソリューションです。を使用しarray_fill()ます。

$month = $_POST['month'];
$days = $_POST['days'];

// Be sure to whitelist $month and $days before using them in an SQL query!  
// For example, you could store an associative array keyed by month,
// containing lists of the day column names.
$month_table_whitelist = array(
  "month_jan" => array("day1", "day2", "day3", /* etc */),
  "month_feb" => array("day1", "day2", "day3", /* etc */),
  /* etc */
);
if (!array_key_exists($month, $month_table_whitelist)) {
  die("Please specify a valid month.");
}
if (!array_search($days, $month_table_whitelist[$month])) {
  die("Please specify a valid day of month.");
}

$count = $_POST['count'];//if a check 3 values I get '3'

$tuples = array_fill(1, $count, "('1')");

$status = mysql_query("INSERT INTO $month ($days) VALUES ".implode(",", $tuples));
if ($status === false) {
  die(mysql_error());
}

PS: 安全でない値 $month と $days をクエリに直接挿入することにより、クエリは SQL インジェクションに対して脆弱です。ホワイトリスト方式を使用して、これらの入力がデータベース内の実際のテーブルと列の名前と一致するようにする必要があります。ユーザー入力を信頼するだけではいけません。

PPS: ext/mysql 関数を使用していることを知っておく必要がありますが、これらは非推奨です。これが新しいアプリケーションである場合は、非推奨の API の使用に時間を費やす前に、mysqli または PDO の使用を開始する必要があります。

于 2013-02-03T19:57:28.760 に答える
-1
$count = $_POST['count'];//if a check 3 values I get '3'

$daystatic="('1')";

mysql_query("INSERT INTO $month ($days) VALUES ". $daystatic . str_repeat ( ",$daystatic" , $count-1));
于 2013-02-03T20:10:41.403 に答える