Form feilds array

Hi Chaps, i don’t understand what ive done wrong, however when i try to echo form fields, theres alway noe missing :frowning:

Database

 $dim = @mysql_query('SELECT dims.dimid, dims.dimdesc, dimlinks.prodid, dimlinks.dimlink FROM dimlinks LEFT JOIN dims ON dimlinks.dimlink = dims.dimid WHERE dimlinks.prodid = "20"');
  $row_dim = mysql_fetch_assoc($dim);
  if (!$dim) {
    exit('<p>Unable to obtain links from the database.</p>');
  }

and then the loop:

<?php
 while ($links = mysql_fetch_array($dim)) {
 $linkname3 = htmlspecialchars($links['dimdesc']);
echo
"<label><input name='option[]' type='text' value='' />$linkname3</label>";
 }
?>

Ive tried just a simple echo of results and theyre fine…any ideas?..thanks in advance
b

Yep. You are reading the first result above your if statement, discarding it, and then looping over the remaining results. Remove the $row_dim = mysql_fetch_assoc($dim); above your if statement and you’ll see all the results you were expecting.

As an aside: mysql is being deprecated and it is preferred to use mysqli or PDO.

Thanks…i was rushing :slight_smile:

Now, i have another problem (title is still relevant so wont bother adding a new post)

Ive never done dynamic field forms from a db before, so im in a bit of a mess…how to a take the field values and insert into a db. Below is where i am at the mo…itll update the values already in the form (hidden field/$utid), however the field where i add data isnt being inserted (“val3”)

 <?php
 
  if(isset($_POST['Submit']))  
  
  {
$ndim= $_POST["val3"];
$i=0;

foreach($_POST['id'] as $id)
        {

          $add =
		  
		  "INSERT INTO testdims (proid,  dimid, dimvalue) 
VALUES ('$utid', '$id', '$ndim')";


 $result_update = mysql_query($add);
    $i++;
    }
	}
 ?>

from:

  <?php
$result = mysql_query("SELECT dims.dimid, dims.dimdesc, dimlinks.prodid, dimlinks.dimlink FROM dimlinks LEFT JOIN dims ON dimlinks.dimlink = dims.dimid WHERE dimlinks.prodid = '$utid'");
while($row=mysql_fetch_array($result))
{
?>
<div class="field">
  <label><?php echo $row['dimdesc']; ?></label>
    <input name="val3[]" value="" />
<input name="id[]" type="hidden" value="<?php echo $row['dimid']; ?>" />

</div>


<?php } ?> 

Do you get any errors, or it just doesn’t insert?

Try the following two things.

  1. Echo the value of $add before you can mysql_query, so you can see what query is being executed (might be a syntax issue or an unescaped value).
  2. add “or die(mysql_error())” after mysql_query($add)

Got it sorted …good idea to echo $add, as it showed that the data actually exists…there was a problem with the database structure :frowning: so i was just entering ‘0’…
thanks anyways B