0

So I have a form that submits 7 textareas that are each text for a day of the week. (shows specials for bars by day) Right now I am using the following code to submit it to the database. Is there an easier way (using arrays, or a for loop) to do this function?

$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 Answer 1

1

You can string all the inserts into one commit:

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.');
Sign up to request clarification or add additional context in comments.

2 Comments

ok, thanks. I also have a table of weekdays that has week_number and week_day. Is there a way to recurse through that and have just one insert line?
Not sure without seeing your code. Also, you may want to use a cleaner php format: $sql = "INSERT INTO specials (bid, day_of_week, special) VALUES ('{$bid}','0','{$special_1}')"; mysql_query($sql);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.