将多个textareas添加到数据库php / MySQL

时间:2011-12-20 21:01:25

标签: php mysql

所以我有一个表格提交7个textareas,每个文本都是一周中的一天。 (按日显示栏的特价)现在我使用以下代码将其提交到数据库。是否有更简单的方法(使用数组或for循环)来执行此功能?

$special_0 = mysql_real_escape_string(stripslashes($_POST['special_0']));
$special_1 = mysql_real_escape_string(stripslashes($_POST['special_1']));
$special_2 = mysql_real_escape_string(stripslashes($_POST['special_2']));
$special_3 = mysql_real_escape_string(stripslashes($_POST['special_3']));
$special_4 = mysql_real_escape_string(stripslashes($_POST['special_4']));
$special_5 = mysql_real_escape_string(stripslashes($_POST['special_5']));
$special_6 = mysql_real_escape_string(stripslashes($_POST['special_6']));


mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','0','.$special_0.')') or die(mysql_error());
mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','1','.$special_1.')') or die(mysql_error());
mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','2','.$special_2.')') or die(mysql_error());
mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','3','.$special_3.')') or die(mysql_error());
mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','4','.$special_4.')') or die(mysql_error());
mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','5','.$special_5.')') or die(mysql_error());
mysql_query('INSERT INTO specials (bid, day_of_week,special) VALUES('.$bid.','6','.$special_6.')') or die(mysql_error());

1 个答案:

答案 0 :(得分:1)

您可以将所有插入字符串转换为一个提交:

mysql_query('INSERT INTO specials (bid, day_of_week,special)
VALUES('.$bid.','0','.$special_0.'),
('.$bid.','1','.$special_1.'),
('.$bid.','2','.$special_2.'),
('.$bid.','3','.$special_3.'),
('.$bid.','3','.$special_4.'),
('.$bid.','3','.$special_5.'),
('.$bid.','3','.$special_6.');