GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jan-2005, 11:58
rams rams is offline
New Member
 
Join Date: Jan 2005
Posts: 10
rams is on a distinguished road

Faulty Loop


The 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  
Old 14-Jan-2005, 08:16
Allowee's Avatar
Allowee Allowee is offline
Regular Member
 
Join Date: May 2003
Location: The Netherlands
Posts: 339
Allowee has a spectacular aura about
is there a $Temp array present?

you can add
PHP Code:

error_reporting(E_ALL); 


at the top of your scripts and see all things that go wrong.
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
__________________
Pastebin
PHP Documentation Site
Allowee's Blog http://allowee.net
  #3  
Old 14-Jan-2005, 08:25
rams rams is offline
New Member
 
Join Date: Jan 2005
Posts: 10
rams is on a distinguished road
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 GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.