Jump to content

Why does name="name[]" value="2013-01-01" split the value up into an array?


FoxRocks

Recommended Posts

Hi there,

 

First, thank you for your interest in my problem, I appreciate your time.

 

I have a small form that is populated with records from a mysql database using php. I have three <input>'s which have the name attribute set to an array in case there is more than one record. The problem I'm having is for some reason the $adate (which is === "2013-01-01") gets broken down into 10 parts of an array. I don't get it, I've used this exact same setup on 5 or 6 other pages and it doesn't do this.

 

Just to be clear, when I echo out $date, it looks like this:

 

$date[$i] = 2

 

And when I run through it, it looks like this:

 

$date[0] = 2
$date[1] = 0
$date[2] = 1
$date[3] = 3
$date[4] = -
$date[5] = 0
$date[6] = 1
$date[7] = -
$date[8] = 0
$date[9] = 1

 

What I'm expecting, and wanting, is this:

 

$date[$i] = 2013-01-01

 

Here is my form code:

 

<form method="post" action="">
<table cellpadding="4" cellspacing="0" border="1" id="wod_tracking_form">

<tr align="center">
<td colspan="3"><?php echo $wod_name; ?> Tracking Form</td>
</tr>

<tr align="center">
<td class="bold">Date Worked</td>
<td class="bold">Notes</td>
<td class="bold">Entered By</td>
</tr>

<?php

for($i = $fromTIME; $i <= $toTIME; $i = strtotime('+1 day', $i))
   {
   $adate = date('Y-m-d',$i);
   $bdate = date('M jS Y',$i);
   echo "<tr>";
   echo "<td align=\"center\"><input type=\"hidden\" name=\"wod_date[]\" value=\"" . $adate . "\">" . $bdate . "</td>";
   echo "<td><textarea name=\"wod_note[]\" style=\"resize:none;\" maxlength=\"255\" cols=\"25\" rows=\"3\"><textarea></td>";
   //Change the value below once the admin area has been created. Use the session admin id here.
   echo "<td align=\"center\"><input type=\"hidden\" name=\"wod_admin\" value=\"10845\">10845</td>";
   echo "</tr>";
   }

?>

<tr align="center">
<td colspan="3"><input type="submit" name="wod_submit" style="width:100px;" value="SUBMIT"></td>
</tr>

</table>
</form>

 

Here is my code for inserting it into the database:

 

<?php
if(isset($_POST['wod_submit']))
   {
   $wod_date = $_POST['wod_date'];
   $note = $_POST['wod_note'];
   $admin = $_POST['wod_admin'];
   $i = 0;

   foreach($wod_date as $date)
       {
       $sql = ("INSERT INTO wod_tracking (Emp_ID, WOD_Date, WOD_Note, WOD_Admin) VALUES ('$emp_id', '$date[$i]', '$note[$i]', '$admin')");

       if(!$result_sql = $mysqli->query($sql))
           {
           die ("There was a problem inserting the records into the wod_tracking table. mySQLI Error: " . $mysqli->error . "");
           }

       $i++;
       }
   die;
   //unset($_SESSION['wod']);
   //redirect_to($url);
   }
?>

 

I don't know if this is because it's in a date format, or what, but it's causing me a lot of confusion, I would really appreciate some help on this one.

 

Cheers!

~FOX~

Edited by FoxRocks
Link to comment
Share on other sites

If it is dropdowns: Day, month & year, then catch it in an array and implode it:

 

$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

$date = array($day, $month, $year);
$date = implode('-', $date);

 

Then you can just use the $date variable in your MySQL query to insert it into the database. This is the best way I know how. Maybe someone else knows a better solution?

Edited by Beeeeney
Link to comment
Share on other sites

I reckon seeing a view source of the form page could be helpfull, just to see what's in those hidden fields.

 

also, you have missed the / out of the closing tag for the textarea in the code you posted here.

 

Edt: Without the loop this is what the form source looks like on my machine:

<tr>
<td align="[url=""]center[/url]">
<input type="[url=""]hidden[/url]" name="[url=""]wod_date[][/url]" value="[url=""]2013-01-10[/url]">
Jan 10th 2013
</td>
<td>
<textarea name="[url=""]wod_note[][/url]" style="[url=""]resize:none;[/url]" maxlength="[url=""]255[/url]" cols="[url=""]25[/url]" rows="[url=""]3[/url]">
</textarea>
</td>
<td align="[url=""]center[/url]">
<input type="[url=""]hidden[/url]" name="[url=""]wod_admin[/url]" value="[url=""]10845[/url]">10845
</td>
</tr>

2nd Edt - ignore the URL's all through that, thatis the damn code formating on this board again :(

Edited by Muddy_Funster
Link to comment
Share on other sites

Thank you all for the replies.

 

I should have included what the variable are set to, sorry.

 

<?php
$fromSTR = $_SESSION['tod_fromDate']; //Selected through the date picker
$toSTR = $_SESSION['tod_toDate']; //Selected through the date picker. This is possibly the same value as $_SESSION['tod_fromDate']
$fromTIME = strtotime($fromSTR);
$toTIME = strtotime($toSTR);
$emp_id = $_SESSION['emp_id']; //Set from the database
?>

 

I have printed all of these out and they come out as expected:

 

<?php
$fromDate = 2013-01-01;
$toDate = 2013-01-01;
$emp_id = 9078;
?>

Link to comment
Share on other sites

OK, here is my form code, all of it, including the $_SESSION's. I've added some echo statements.

 

<?php
$_SESSION['tod_fromDate'] = "2013-01-01";
$_SESSION['tod_toDate'] = "2013-01-01";
$fromSTR = $_SESSION['tod_fromDate'];
$toSTR = $_SESSION['tod_toDate'];
$fromTIME = strtotime($fromSTR);
$toTIME = strtotime($toSTR);
$_SESSION['emp_id'] = "9078";
$emp_id = $_SESSION['emp_id'];
$tod_name = "Taken Days Off";
$tod = 8;
?>
<form method="post" action="">
<table cellpadding="4" cellspacing="0" border="1" id="tod_tracking_form">
<tr align="center">
<td colspan="3"><?php echo $tod_name; ?> Tracking Form</td>
</tr>
<tr align="center">
<td class="bold">Date Taken</td>
<td class="bold">Notes</td>
<td class="bold">Entered By</td>
</tr>
<?php
for($i = $fromTIME; $i <= $toTIME; $i = strtotime('+1 day', $i))
{
echo "for i: " . $i . "<br>";//Error checking
$adate = date('Y-m-d',$i);
echo "for adate: " . $adate . "<br>";//Error checking
$bdate = date('M jS Y',$i);
echo "for bdate: " . $bdate . "<br>";//Error checking
echo "<tr>";
echo "<td align=\"center\"><input type=\"hidden\" name=\"tod_date[]\" value=\"" . $adate . "\">" . $bdate . "</td>";
echo "<td><textarea name=\"tod_note[]\" style=\"resize:none;\" maxlength=\"255\" cols=\"25\" rows=\"3\"></textarea></td>";
//Change the value below once the admin area has been created. Use the session admin id here.
echo "<td align=\"center\"><input type=\"hidden\" name=\"tod_admin\" value=\"10845\">10845</td>";
echo "</tr>";
}
?>
<tr align="center">
<td colspan="3"><input type="submit" name="tod_submit" style="width:100px;" value="SUBMIT"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['tod_submit']))
{
$tod_date = $_POST['tod_date'];
$note = $_POST['tod_note'];
$admin = $_POST['tod_admin'];
$i = 0;

foreach($tod_date as $date)
{
$sql = ("INSERT INTO tod_tracking (Emp_ID, TOD_Date, TOD_Note, TOD_Admin) VALUES ('$emp_id', '$date[$i]', '$note[$i]', '$admin')");
echo "foreach i: " . $i . "<br>";//Error checking
echo "foreach date[i]: " . $date[$i] . "<br>";//Error checking
// if(!$result_sql = $mysqli->query($sql))
// {
// die ("There was a problem inserting the records into the tod_tracking table. mySQLI Error: " . $mysqli->error . "");
// }
echo "sql: " . $sql . "<br>";//Error checking

$i++;
}
//unset($_SESSION['tod']);
//redirect_to($url);
}
?>

 

Now, here is the page source once the code has run:

 

<form method="post" action="">
<table cellpadding="4" cellspacing="0" border="1" id="tod_tracking_form">
<tr align="center">
<td colspan="3">Taken Days Off Tracking Form</td>
</tr>
<tr align="center">
<td class="bold">Date Taken</td>
<td class="bold">Notes</td>
<td class="bold">Entered By</td>
</tr>
for i: 1356998400<br>for adate: 2013-01-01<br>for bdate: Jan 1st 2013<br><tr><td align="center"><input type="hidden" name="tod_date[]" value="2013-01-01">Jan 1st 2013</td><td><textarea name="tod_note[]" style="resize:none;" maxlength="255" cols="25" rows="3"></textarea></td><td align="center"><input type="hidden" name="tod_admin" value="10845">10845</td></tr>
<tr align="center">
<td colspan="3"><input type="submit" name="tod_submit" style="width:100px;" value="SUBMIT"></td>
</tr>
</table>
</form>
foreach i: 0<br>foreach date[i]: 2<br>sql: INSERT INTO tod_tracking (Emp_ID, TOD_Date, TOD_Note, TOD_Admin) VALUES ('9078', '2', 'Here are the notes', '10845')<br>

 

And finally, here is a screen shot of the form with the echo statements:

 

post-130182-0-10780900-1357820271_thumb.png

 

This makes NO sense to me, I can't believe I'm either missing something obvious, or that I can't get it to work...ugh.

Link to comment
Share on other sites

As I suspected... Run a var_dump () on $tod_date after this line:

$tod_date = $_POST['tod_date'];

 

Then have a look at your loop, and translate what it does to plain English. Make sure you really understand what it actually does, not just what you wanted it to do. ;)

 

Here it is:

array (size=1)
 0 => string '2013-01-01' (length=10)

Link to comment
Share on other sites

"basic debugging 101" , ok, I guess my error checking wasn't cutting it? I echo'd out everything you did, I just didn't label which line it was on.

 

It's not that I don't appreciate getting helped in the right direction, or being taught how to troubleshoot instead of just being handed the answer...in fact I prefer that over just getting the answer...however the var_dump() and the "basic debugging 101" ideas didn't produce anything new that my already existing error checking didn't already have.

 

Anyways, I've found the problem. It was a stupid one as I'm sure we all expected.

 

Thanks for the help, I'm sorry if I sound unappreciative with my comment, don't take it personally.

 

~FOX~

 

EDIT: I forgot to say what the problem was. I changed $date[$i] to just $date in the insert statement and it worked fine.

Edited by FoxRocks
Link to comment
Share on other sites

You found the problem, but do you know what the problem was?

 

that which I echoed out was different to what you did, I'm not in the habbit of re-writing things for the fun of it or to pass the time. The point I was making was that you should be checking the content of the variable at each change.

 

Why did $date work when $date[$i] did not. What was $date[$i] if it wasn't what you expected it to be?

These are the key questions you need to be able to answer.

Link to comment
Share on other sites

Right, I didn't really explain the solution very well.

 

The FOR loop was already looping through each value of the $date array. When I accidentally added the [$i] key to it, it broke that value down further thinking it was mutlidimensional array, when it wasn't.

 

~FOX~

Link to comment
Share on other sites

To whoever last updated my fiddle, while I'm not a "faggot" as you claim, I'm also not a coward. You might think that updating the fiddle to say such a thing is funny and clever, but in reality it's just pathetic. If you have something to say, just say it and don't be such a juvenile.

 

~FOX~

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.