Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
php/sql-在多个字段中插入相同的值_Php_Mysql - Fatal编程技术网

php/sql-在多个字段中插入相同的值

php/sql-在多个字段中插入相同的值,php,mysql,Php,Mysql,我有这段代码(这正在工作,并将这些变量传递到另一个文件) 我想添加与所选天数相同的值(“1”)。如何更改值('1','1','1')?这里有一个生成相同字符串序列的解决方案。使用 PS:通过直接在查询中插入不安全的$month和$days值,您的查询容易受到SQL注入的攻击。您应该使用白名单方法来确保这些输入与数据库中的实际表名和列名匹配,而不仅仅是信任用户输入 PPS:您应该知道您使用的是ext/mysql函数,但这些函数已被弃用。如果这是一个新的应用程序,在投入更多时间使用不推荐的API之前

我有这段代码(这正在工作,并将这些变量传递到另一个文件)


我想添加与所选天数相同的值(“1”)。如何更改值('1','1','1')?

这里有一个生成相同字符串序列的解决方案。使用

PS:通过直接在查询中插入不安全的$month和$days值,您的查询容易受到SQL注入的攻击。您应该使用白名单方法来确保这些输入与数据库中的实际表名和列名匹配,而不仅仅是信任用户输入


PPS:您应该知道您使用的是ext/mysql函数,但这些函数已被弃用。如果这是一个新的应用程序,在投入更多时间使用不推荐的API之前,您应该开始使用mysqli或PDO。

您的代码完全没有正确的SQL转义。这是很危险的,因为您使用的不是来自$\u POST的数据作为值(可以使用
mysql\u real\u escape\u string()
)进行转义),而是作为表和列的名称,没有预先制作的转义函数。事先准备好的声明在这里也会失败。走另一条路@斯文:你完全正确。我会做出这些改变。我知道,我还在学习,我一直很懒。明天我会开始改变。Ps我尝试了你的代码,但当我检查数据库时,没有记录。因此它以静默方式失败,但除非我们检查mysql_query()函数的错误返回状态,否则我们永远不会知道原因。我已将其添加到上述代码中。缺少$month和$days的逃逸或消毒。此代码可用于SQL注入。@Sven,是的,我已通过注释解决了该问题,但我没有显示白名单的实现。我用一个例子编辑了上面的内容。请取消你的否决票。@BillKarwin我想我发现了错误。如果我保留$tuples=array_fill(1,$count,“('1')”),则查询不起作用,但如果我使用数字(如3)更改$count,则查询会起作用。有没有可能$count只是字符串3而不是数字3,这就是为什么数组_fill()不起作用?缺少对$month和$days的转义或清理。这段代码是为SQL注入而打开的。
$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));
$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));
$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());
}
$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));