Page 1 of 2
While loop problem
Posted: Thu Sep 10, 2009 7:14 am
by Brad7928
Is it possible to do something along the lines of this? This doesn't work, i suspect because i try to change the variable inside a $_POST[]...
I have a table above this and on each row are inputs named with the names you see in the php but on each row the number after the names goes up by one. in total there are 5 rows, so for example to field pick is named pick1 on the first row, pick2 on the second... so on to pick5 on the fifth row.
Code: Select all
<?
$hostname ="localhost";
$user = "root";
$pass = "root_pass"; //these are set
$database = "database_name"; //these are set
// Connects to your Database
$connect = mysql_connect($hostname, $user, $pass);
mysql_select_db($database, $connect);
echo ("connected");
if (isset($_POST['submit'])) {
$i=1;
while($i<=5) {
if(isset($_POST['pick$i'])) {
mysql_query("INSERT INTO jobs (ts, pick, addr, dest, tonn, driv, invc ,subc, span, completed) VALUES (ts, '$_POST[pick$i]', '$_POST[addr$i]', '$_POST[dest$i]', '$_POST[tonn$i]', '$_POST[driv$i]', '$_POST[invn$i]', '$_POST[subc$i]', '$_POST[span$i]', no)",$connect);
//echo ("<br>Inserted<br>");
}
$i++;
}
}
?>
Re: While loop problem
Posted: Thu Sep 10, 2009 8:16 am
by Mark Baker
change
to
Code: Select all
$fld = 'pick'.$i;
if(isset($_POST[$fld])) {
and forall equivalently referenced fields as well
Re: While loop problem
Posted: Thu Sep 10, 2009 6:34 pm
by Brad7928
I tried this, it's echo's 5 Inserteds when the all the fields have data in them, but the data isn't actually being entered into the database...
Any idea's?
Code: Select all
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="center" width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><strong>Pick Up From</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>Destination</strong></td>
<td align="center"><strong>Tonnes</strong></td>
<td align="center"><strong>Driver</strong></td>
<td align="center"><strong>Ryans Inv No.</strong></td>
<td align="center"><strong>Sub Cont.</strong></td>
<td align="center"><strong>Spa No.</strong></td>
</tr>
<tr>
<td><input type="text" name="pick1" /></td>
<td><input type="text" name="addr1" /></td>
<td><input type="text" name="dest1" /></td>
<td><input type="text" name="tonn1" /></td>
<td><input type="text" name="driv1" /></td>
<td><input type="text" name="invn1" /></td>
<td><input type="text" name="subc1" /></td>
<td><input type="text" name="span1" /></td>
</tr>
<tr>
<td><input type="text" name="pick2" /></td>
<td><input type="text" name="addr2" /></td>
<td><input type="text" name="dest2" /></td>
<td><input type="text" name="tonn2" /></td>
<td><input type="text" name="driv2" /></td>
<td><input type="text" name="invn2" /></td>
<td><input type="text" name="subc2" /></td>
<td><input type="text" name="span2" /></td>
</tr>
<tr>
<td><input type="text" name="pick3" /></td>
<td><input type="text" name="addr3" /></td>
<td><input type="text" name="dest3" /></td>
<td><input type="text" name="tonn3" /></td>
<td><input type="text" name="driv3" /></td>
<td><input type="text" name="invn3" /></td>
<td><input type="text" name="subc3" /></td>
<td><input type="text" name="span3" /></td>
</tr>
<tr>
<td><input type="text" name="pick4" /></td>
<td><input type="text" name="addr4" /></td>
<td><input type="text" name="dest4" /></td>
<td><input type="text" name="tonn4" /></td>
<td><input type="text" name="driv4" /></td>
<td><input type="text" name="invn4" /></td>
<td><input type="text" name="subc4" /></td>
<td><input type="text" name="span4" /></td>
</tr>
<tr>
<td><input type="text" name="pick5" /></td>
<td><input type="text" name="addr5" /></td>
<td><input type="text" name="dest5" /></td>
<td><input type="text" name="tonn5" /></td>
<td><input type="text" name="driv5" /></td>
<td><input type="text" name="invn5" /></td>
<td><input type="text" name="subc5" /></td>
<td><input type="text" name="span5" /></td>
</tr>
<tr>
<td colspan="9" align="center"><input type="hidden" name="date" value="<? echo date("j-M"); ?>" /><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
<?
$hostname ="localhost";
$user = "root";
$pass = "****";
$database = "****";
// Connects to your Database
$connect = mysql_connect($hostname, $user, $pass);
mysql_select_db($database, $connect);
//echo ("<br>Connected<br>");
if (isset($_POST['submit'])) {
$i=1;
$fld = 'pick'.$i;
$fld1 = 'addr'.$i;
$fld2 = 'dest'.$i;
$fld3 = 'tonn'.$i;
$fld4 = 'driv'.$i;
$fld5 = 'invn'.$i;
$fld6 = 'subc'.$i;
$fld7 = 'span'.$i;
while($i<=5) {
if(isset($_POST[$fld])) {
mysql_query("INSERT INTO jobs (ts, pick, addr, dest, tonn, driv, invn ,subc, span, completed) VALUES (ts, '$_POST[$fld]', '$_POST[$fld1]', '$_POST[$fld2]', '$_POST[$fld3]', '$_POST[$fld4]', '$_POST[$fld5]', '$_POST[$fld6]', '$_POST[$fld7]', no)",$connect);
echo ("<br>Inserted<br>");
}
$i++;
}
}
?>
Re: While loop problem
Posted: Thu Sep 10, 2009 7:18 pm
by SimonMayer
It seems you are declaring your $fld, $fld1 etc variables before the while loop, hence $i won't be set.
Try:
Code: Select all
if (isset($_POST['submit'])) {
while($i<=5) {
$i=1;
$fld = 'pick'.$i;
$fld1 = 'addr'.$i;
$fld2 = 'dest'.$i;
$fld3 = 'tonn'.$i;
$fld4 = 'driv'.$i;
$fld5 = 'invn'.$i;
$fld6 = 'subc'.$i;
$fld7 = 'span'.$i;
if(isset($_POST[$fld])) {
mysql_query("INSERT INTO jobs (ts, pick, addr, dest, tonn, driv, invn ,subc, span, completed) VALUES (ts, '$_POST[$fld]', '$_POST[$fld1]', '$_POST[$fld2]', '$_POST[$fld3]', '$_POST[$fld4]', '$_POST[$fld5]', '$_POST[$fld6]', '$_POST[$fld7]', no)",$connect);
echo ("<br>Inserted<br>");
}
$i++;
}
}
?>
Also, it seems like wasted effort to declare the $fld, $fld1 variables.
Personally I would do that bit almost like you were doing originally. You just had a small problem with your syntax. See below:
Code: Select all
mysql_query("INSERT INTO jobs (ts, pick, addr, dest, tonn, driv, invc ,subc, span, completed) VALUES (ts, '$_POST["pick".$i]', '$_POST["addr".$i]', '$_POST["dest".$i]', '$_POST["tonn".$i]', '$_POST["driv".$i]', '$_POST["invn".$i]', '$_POST["subc".$i]', '$_POST["span".$i]', no)",$connect);
Re: While loop problem
Posted: Thu Sep 10, 2009 7:37 pm
by Brad7928
I've made the changes recommended by the last poster, although when i refreshed the page it came up blank. I've narrowed it down to this code... (When it's commented out it displays fine)
Code: Select all
mysql_query("INSERT INTO jobs (ts, pick, addr, dest, tonn, driv, invc ,subc, span, completed) VALUES (ts, '$_POST["pick".$i]', '$_POST["addr".$i]', '$_POST["dest".$i]', '$_POST["tonn".$i]', '$_POST["driv".$i]', '$_POST["invn".$i]', '$_POST["subc".$i]', '$_POST["span".$i]', no)",$connect);
i'm guessing it's the '$_POST["pick".$i]' variables, is there another way i could write the same thing? Sorry i've never inserted names with variables in them before...
Re: While loop problem
Posted: Thu Sep 10, 2009 7:49 pm
by SimonMayer
I think it's my fault. I hadn't given you very good syntax there. Let me try again...
Code: Select all
mysql_query("INSERT INTO `jobs` (`ts`, `pick`, `addr`, `dest`, `tonn`, `driv`, `invc`, `subc`, `span`, `completed`) VALUES ('ts', '".$_POST["pick".$i]."', '".$_POST["addr".$i]."', '".$_POST["dest".$i]."', '".$_POST["tonn".$i]."', '".$_POST["driv".$i]."', '".$_POST["invn".$i]."', '".$_POST["subc".$i]."', '".$_POST["span".$i]."', 'no')", $connect);
Now, I have added `these quotes` as they are very occasionally required with some table/field names in MySQL, so it's good practice to include them, even though they probably weren't the cause of your problem.
I have also added ". ." around each $_POST[]. This is because it's the proper PHP syntax. You should always close the quotes and follow with a dot if you are calling a variable. To then resume the text, you must use a dot and reopen the quotes.
I hope this 1) explains the situation. 2) works for you
Re: While loop problem
Posted: Thu Sep 10, 2009 8:20 pm
by Brad7928
Thanks for both your help and quick replies... This resulted with me getting pages and pages of "Inserted" when i ran it, i have a link here
http://202.45.110.174/ryans/addjob2.php
this is the code i am now using
Code: Select all
if (isset($_POST['submit'])) {
while($i<=5) {
$i=1;
$fld = 'pick'.$i;
$fld1 = 'addr'.$i;
$fld2 = 'dest'.$i;
$fld3 = 'tonn'.$i;
$fld4 = 'driv'.$i;
$fld5 = 'invn'.$i;
$fld6 = 'subc'.$i;
$fld7 = 'span'.$i;
if(isset($_POST[$fld])) {
mysql_query("INSERT INTO `jobs` (`ts`, `pick`, `addr`, `dest`, `tonn`, `driv`, `invc`, `subc`, `span`, `completed`) VALUES ('ts', '".$_POST["pick".$i]."', '".$_POST["addr".$i]."', '".$_POST["dest".$i]."', '".$_POST["tonn".$i]."', '".$_POST["driv".$i]."', '".$_POST["invn".$i]."', '".$_POST["subc".$i]."', '".$_POST["span".$i]."', 'no')", $connect);
echo ("<br>Inserted<br>");
}
$i++;
}
}
?>
Re: While loop problem
Posted: Thu Sep 10, 2009 8:33 pm
by John Cartwright
Lets think about this logically with pseudo,
while (variable $i is less than 5)
{
set $i to 1
add 1 to $i
}
Notice anything wrong here
Re: While loop problem
Posted: Thu Sep 10, 2009 8:50 pm
by Brad7928
hahahaha i'm a peanut... i should define i before the while loop... that way when it loops again it won't be setting i to 1 again...
so i've moved $i=1 just under the $_POST['submit'] section. It does the loop fine, prints 5 inserted's just doesn't actually insert the data into mySQL...
Re: While loop problem
Posted: Thu Sep 10, 2009 8:55 pm
by John Cartwright
A good strategy for debugging SQL statements is to echo out the SQL prior to querying it to verify it's what you expect, as well to include mysql error reporting.
Code: Select all
$sql = "INSERT INTO `jobs` (`ts`, `pick`, `addr`, `dest`, `tonn`, `driv`, `invc`, `subc`, `span`, `completed`) VALUES ('ts', '".$_POST["pick".$i]."', '".$_POST["addr".$i]."', '".$_POST["dest".$i]."', '".$_POST["tonn".$i]."', '".$_POST["driv".$i]."', '".$_POST["invn".$i]."', '".$_POST["subc".$i]."', '".$_POST["span".$i]."', 'no')";
echo $sql .'<br>';
mysql_query($sql) or die(mysql_error());
..and you really should be passing your input through mysql_real_escape_string() before adding the values into the query.
Re: While loop problem
Posted: Thu Sep 10, 2009 9:19 pm
by Brad7928
aha!! a column was named wrong... invc instead of invn that will do it, i've updated the code and it works perfectly, thanks guys!

Re: While loop problem
Posted: Fri Sep 11, 2009 6:39 am
by Brad7928
wow, thanks for that link on SQL injection! It shouldn't matter with this site as it's not going to be on the internet, just on a local LAN...
i only have one problem with the code now... if i leave a row of the table empty, say only insert 4 rows, it inserts the 5th blank row into mySQL... what would be the best way to check if the field isn't empty?
i've seen this used before, just don't know how to put it into my code... or if it will even work in a loop...
Code: Select all
if (!$_POST['$fld']) {
die('You did not complete all of the fields.');
}
Re: While loop problem
Posted: Fri Sep 11, 2009 7:36 am
by Weiry
Brad7928 wrote:i only have one problem with the code now... if i leave a row of the table empty, say only insert 4 rows, it inserts the 5th blank row into mySQL... what would be the best way to check if the field isn't empty?
Well i cant be sure if you mean before you post data to the database or after retrieving it.
But, assuming that you arent talking about checking the input to the database, and only checking it when retrieving from the database, you could use.
Code: Select all
while($row = mysql_fetch_assoc($mysqlResult)){
if(!empty($row['YourFieldHere'])){
// code here
}else{
//print error code
}
}
Or on the other hand if you were checking BEFORE submission to the database:
Code: Select all
if(!empty($_POST['field1']) || !empty($_POST['field2']) || !empty($_POST['field3'])){
//insert field1,field2,field3 into database
}else{
// print an error here
}
Re: While loop problem
Posted: Fri Sep 11, 2009 8:18 am
by Brad7928
Sorry, yes i want to check the data before BEFORE submitting it to the database. not sure how to implement it with the while loop here...
maybe something like:
Code: Select all
$i = 1
while ($i < 5) {
check if fields are empty
if !empty put into some sort of array?
then INSERT INTO jobs from array?
Just an idea, i have no idea how arrays work... and i may be making this harder than it should be...
Re: While loop problem
Posted: Fri Sep 11, 2009 11:00 am
by Weiry
i didnt realise you were submitting multiple entries at once,
but assuming you have added the jobs data into an array, which was inserted into a main holding array for all jobs being inserted:
Code: Select all
MainData(
FirstData => array( "field1" => "test", "field2" => "foo"),
SecondData => array( "field1" => "foo", "field2" => "")
);
You would run through like:
Code: Select all
foreach($mainData as $dataArray){
if(!empty($dataArray['field1']) && !empty($dataArray['field2'])){
//insert to database
// FirstData will be inserted to database
}else{
//return an error
// SecondData will return an error because "field2" does not have a value
}
}