![]() |
|
#1
|
|||
|
|||
Faulty LoopThe following code works great:
$Date = date("Y-m-d"); $section=$_POST['section']; for ($Loop = 1; $Loop <= 21; $Loop++) { $result=mysql_query("INSERT INTO tbltemperaturesl1 (Date, Room, Pot, Temperature) VALUES ('$Date','$room','$Loop','$Temp[$Loop]')")or die("Insert Error: ".mysql_error()); } mysql_close($link); print "Record added\n"; ?> But if I change the loop to read: for ($Loop = 22; $Loop <= 43; $Loop++) The date, room and pot are entered fine, but the temperature is entered as all zeroes. Any suggestions? |
|
#2
|
||||
|
||||
|
is there a $Temp array present?
you can add PHP Code:
a very handy 'tool' for finding errors. if it says "Notice: Undefined offset: 23 ....." you know that $Temp doesn't have a vaule for key 23 that could be why MySQL makes it a 0 |
|
#3
|
|||
|
|||
|
I have two php files -
insert_form.php: <html> <head> <title>Line 1 Temperatures</title> </head> <body> <table width="300" cellpadding="5" cellspacing="0" border="2"> <tr align="center" valign="top"> <td align="left" colspan="1" rowspan="1" bgcolor="64b1ff"> <h3>Line 1 Temperatures</h3> <form method="POST" action="insert_record.php"> <!--The hidden fields are provided to maintain state. They are used to pass the username and password from script to script.--> <input type="hidden" name="username" value="<? print $_POST['root']?>"> <input type="hidden" name="password" value="<?print $_POST['triadpass']?>"> <form action="insert_record.php" method="POST"> <? $Date = date("Y-m-d"); print("<table align=center border=1>"); print("<tr>"); print("<td><font style='FONT-WEIGHT:BOLD'>Date:</font></td>"); print("<td><font style='FONT-WEIGHT:BOLD'>Room:</font></td>"); print("<td><font style='FONT-WEIGHT:BOLD'>Pot:</font></td>"); print("<td><font style='FONT-WEIGHT:BOLD'>Temp:</font></td>"); print("</tr>"); $section=$_POST['section']; if ($section == A1_21) { $start = 1; $end = 21; $room = A; } elseif ($section == A22_43) { $start = 22; $end = 43; $room = A; } elseif ($section == A44_65) { $start = 44; $end = 65; $room = A; } elseif ($section == A66_87) { $start = 66; $end = 87; $room = A; } elseif ($section == B1_21) { $start = 1; $end = 21; $room = B; } elseif ($section == B22_43) { $start = 22; $end = 43; $room = B; } elseif ($section == B44_65) { $start = 44; $end = 65; $room = B; } else { $start = 66; $end = 87; $room = B; } for ($Loop = $start; $Loop <=$end; $Loop++) { print("<tr>"); print("<td>" .$Date. "</td>"); print("<td>$room</td>"); print("<td>" .$Loop. "</td>"); print("<td><input type=text name='Temp[]' size=3 maxlength=4></td>"); print("</tr>"); } print("</table><br><br>"); print "<input type=submit value=Submit><input type=reset>\n"; ?> <input type="hidden" name="ud_section" value="<? echo "$section"?>"><br> </form> </td> </tr> </table> </body> </html> insert_record.php: <html> <head> <title>Line 1 Temperatures</title> </head> <body> <? error_reporting(E_ALL); $Date = date("Y-m-d"); define(db_host, "localhost"); define(db_user, "root"); define(db_pass, "triadpass"); define(db, "processdata"); $link = mysql_connect(db_host,db_user,db_pass); if (! $link) die("Couldn't connect to MySQL"); mysql_select_db(db , $link) or die("Select Error: ".mysql_error()); $section=$_POST['section']; if ($ud_section == A1_21) { $start = 1; $end = 21; $room = "A"; } elseif ($ud_section == A22_43) { $start = 22; $end = 43; $room = "A"; } elseif ($ud_section == A44_65) { $start = 44; $end = 65; $room = "A"; } elseif ($ud_section == A66_87) { $start = 66; $end = 87; $room = "A"; } elseif ($ud_section == B1_21) { $start = 1; $end = 21; $room = "B"; } elseif ($ud_section == B22_43) { $start = 22; $end = 43; $room = "B"; } elseif ($ud_section == B44_65) { $start = 44; $end = 65; $room = "B"; } else { $start = 66; $end = 87; $room = "B"; } $Temp = array(22 => $Loop); for ($Loop = $start; $Loop <= $end; $Loop++) { $result=mysql_query("INSERT INTO tbltemperaturesl1 (Date, Room, Pot, Temperature) VALUES ('$Date','$room','$Loop','$Temp[$Loop]')")or die("Insert Error: ".mysql_error()); } mysql_close($link); print "Record added\n"; ?> <input type="text" name="ud_section2" value="<? echo "$ud_section"?>"><br> <form method="POST" action="insert_form.php"> <input type="submit" value="Insert Another Record"> </form> </body> </html> I think you're right, my array is set up correctly. it enters the temperaure I put in for A1 in A2, and doesn't put in any temperatures for A21 - 43, just zeroes. I know that arrays start at 0, should I set the array to 1 when entering A1-21 and set the array to 22 when entering 22-43? If so, how do I do this? |
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| No output from loop function | crystalattice | C++ Forum | 2 | 20-Dec-2004 20:39 |
| messy loop help please | sammacs | C Programming Language | 6 | 26-Nov-2004 15:18 |
| Nested for loop with function | Tori | C++ Forum | 11 | 08-Nov-2004 13:02 |
| coding a sentinel controlled loop | tommy69 | C++ Forum | 2 | 10-Mar-2004 14:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The