foreach insert error
am 08.10.2009 00:31:14 von Haig Davis--00c09f8fea9b8fbf8a04755fe7a8
Content-Type: text/plain; charset=ISO-8859-1
Hello All,
I have spent the entire day trying to get my calendar app to function
correctly --- I have no problem with the actual functioning of the
calendar. What is giving me trouble is for each calendar day the user has
the option to check a checkbox requesting the day off and additionally the
user must select a weighting for the request i.e. 1-31 with 1 being 1st
choice and 31 being their least desirable request. The request is then
insered into a mysql database.
I am relativly new to PHP so I may well be on the complete wrong track here.
so far I have tried imploding the array & posting an associative array.
Form Script:
if(isset($_POST['submit'])){
$userID = mysql_real_escape_string(trim($userID));
$year = mysql_real_escape_string(trim($_GET['year']));
$month = mysql_real_escape_string(trim($_GET['mon']));
$dayoff = $_POST['dayoff'];
$weighting = mysql_real_escape_string(trim($_POST['weight']));
var_dump($dayoff);
foreach ($dayoff as
$day){ //come back
to this when figured out that inserting day works to inser wieghting
$sql = "INSERT INTO dayoff (userID, month, year, day) VALUES
('$userID', '$year', '$month', '$day')";
$q = mysql_query($sql)
or die ('error making request');
}
}
?>
Calendar Script:
for($i = 1; $i <= $last['mday']; $i++){
if($i == $today['mday'] && $first['mon'] == $today['mon'] &&
$first['year'] == $today['year']){
$style = 'today';
}
else{
$style = 'day';
}
$filename="$1.['month'].['year']";
echo '
' ; // creates
comment box for points and check box for request day off
echo ' name="weight$i[]" size ="3" maxlength="3" />
';
echo ' ="checkbox" name="dayoff[]" value="\". $i . \"" />
';
echo '
}
I appreciate any advice that you may have. Thank you very much in advance,
my head hurts and I've googled for a few hours with no joy.
Thanks
Haig
--00c09f8fea9b8fbf8a04755fe7a8--
Re: foreach insert error
am 08.10.2009 02:34:47 von List ManagerHaig Davis wrote:
> Hello All,
>
> I have spent the entire day trying to get my calendar app to function
> correctly --- I have no problem with the actual functioning of the
> calendar. What is giving me trouble is for each calendar day the user has
> the option to check a checkbox requesting the day off and additionally the
> user must select a weighting for the request i.e. 1-31 with 1 being 1st
> choice and 31 being their least desirable request. The request is then
> insered into a mysql database.
>
> I am relativly new to PHP so I may well be on the complete wrong track here.
> so far I have tried imploding the array & posting an associative array.
>
> Form Script:
>
>
> > if(isset($_POST['submit'])){
>
> $userID = mysql_real_escape_string(trim($userID));
> $year = mysql_real_escape_string(trim($_GET['year']));
> $month = mysql_real_escape_string(trim($_GET['mon']));
> $dayoff = $_POST['dayoff'];
> $weighting = mysql_real_escape_string(trim($_POST['weight']));
> var_dump($dayoff);
>
> foreach ($dayoff as
> $day){ //come back
> to this when figured out that inserting day works to inser wieghting
>
> $sql = "INSERT INTO dayoff (userID, month, year, day) VALUES
> ('$userID', '$year', '$month', '$day')";
>
> $q = mysql_query($sql)
> or die ('error making request');
> }
>
>
> }
> ?>
>
>
>
> Calendar Script:
>
>
I see a number of typo's in the following code.
> for($i = 1; $i <= $last['mday']; $i++){
> if($i == $today['mday'] && $first['mon'] == $today['mon'] &&
> $first['year'] == $today['year']){
> $style = 'today';
> }
> else{
> $style = 'day';
> }
What are you trying to do here?
Should this...
> $filename="$1.['month'].['year']";
Be this...
$filename=$i."['month'].['year']";
Plus, where are you actually using the previous line at?
> echo '
' ; // creates
> comment box for points and check box for request day off
> echo ' > name="weight$i[]" size ="3" maxlength="3" />
';
Should you have "weight$i[]" in the previous line? This will break a few things
I think.
> echo ' > ="checkbox" name="dayoff[]" value="\". $i . \"" />
';
> echo '
>
> }
Over all, simply put, if you do not number the indexes of your array, you will
not be able to have a 1 to 1 association in your submitted array(s)
BTW: I would try and use the heredoc syntax in your example... Makes things way
cleaner!
echo <<
DAY;
Now, on your input side. Try it a few times to see what you get and I think you
will be able to figure out how to eliminate the days that were not selected /
checked.
>
> I appreciate any advice that you may have. Thank you very much in advance,
> my head hurts and I've googled for a few hours with no joy.
>
> Thanks
>
> Haig
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: foreach insert error
am 08.10.2009 06:32:24 von Paul M FosterOn Wed, Oct 07, 2009 at 03:31:14PM -0700, Haig Davis wrote:
> Hello All,
>
> I have spent the entire day trying to get my calendar app to function
> correctly --- I have no problem with the actual functioning of the
> calendar. What is giving me trouble is for each calendar day the user has
> the option to check a checkbox requesting the day off and additionally the
> user must select a weighting for the request i.e. 1-31 with 1 being 1st
> choice and 31 being their least desirable request. The request is then
> insered into a mysql database.
>
> I am relativly new to PHP so I may well be on the complete wrong track here.
> so far I have tried imploding the array & posting an associative array.
You don't actually saw what your real problem is, and I'm too lazy (and
tired) to try to figure it out from your code. Can you elaborate?
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php