Jump to content

help with form processing from loop


Peterod

Recommended Posts

I've read the php manual and a good few posts with similar but not identical problems and I am struggling to understand how to do this.

 

Ive tailored my test site to help describe the problems that  I have.

 

www.peterod.co.uk 

 

the form is the first link in the left hand bar.

 

Here is the code that creates the form.

<?php

// make connection
require_once "../classes/sqlconselfbuild.php";

$query = mysql_query("SELECT * FROM `materials` ORDER BY `id` ASC"); 

?>
<form action="../engines/testengine1.php" method="post">

<?php
// results
while ($row = mysql_fetch_array($query)):
    $id = $row['id'];
?>
<p>
    id number: <input type="text" name="materials[<?php echo $id; ?>][name]" value="<?php echo $row['id']; ?>" />
    Material:  <input type="text" name="materials[<?php echo $id; ?>][material]" value="<?php echo $row['material']; ?>" />
    Stage: 	  <input type="text" name="materials[<?php echo $id; ?>]    [stage]" value="<?php echo $row['stage']; ?>" />
    Qantity:  <input type="text" name="materials[<?php echo $id; ?>][quantity]" value="<?php echo $row['quantity']; ?>" />
    Unit:    <input type="text" name="materials[<?php echo $id; ?>][unit]" value="<?php echo $row['unit']; ?>" />
    
    <br />
</p>
<?php endwhile; ?>
<input type="submit" name="submit" value="Submit" />
</form>






 

I am unsure how to get this information to insert into the database.  How do you use the UPDATE query with a loop generated form

?

 

 




			
		
Link to comment
Share on other sites

<?php
foreach ($_POST['materials'] as $mater){
    $name = $mater['name']; //or is it $id
    $material = $mater['material'];
    $stage = $mater['stage'];
    //and so on
    // satanize inputs
    mysql_query("UPDATE table_name SET material='$material', stage='stage' etc. WHERE id='$name'");
}
?>

and add disabled="disabled" atribute to your input tag for id_number

Link to comment
Share on other sites

heres the engine, as based on above.  I only have a little spare time at night due to new baby so I dont get long to mess around with the site, so basically any help you guys give is 100% appreciated.

 

<?php

// make conn

require_once "../classes/sqlconselfbuild.php";

foreach ($_POST['materials'] as $mater){
    $name = $mater['name']; //or is it $id  (not sure... anyone more help)
    $material = $mater['material'];
    $stage = $mater['stage'];
$unit = $mater ['unit'];

    // satanize inputs
    mysql_query("UPDATE materials SET material='$material', stage='stage', quantity='quantity',unit='unit' WHERE id='$name'");
}



?>

its live at the above address.  I get a blank screen at the moment

 

I think there are a few things wrong.  please help


update

 

heres how i have it at the moment but still not working

 

<?php

// make conn

$conn = require_once "../classes/sqlconselfbuild.php";

foreach ($_POST['materials'] as $mater){
    $name = $mater['name']; //or is it $id  (not sure... anyone more help)
    $material = $mater['material'];
    $stage = $mater['stage'];
$unit = $mater ['unit'];

    // satanize inputs
$sql = mysql_query("UPDATE materials SET material='$material', stage='stage', quantity='quantity',unit='unit' WHERE id='$name'");
}

mysql_select_db('peterodc_selfbuild');
$retval = mysql_query( $sql,$conn);
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>

 

Link to comment
Share on other sites

try

<?php
mysql_connect('localhost', 'username', 'password'); //change to your username and password
mysql_select_db('peterodc_selfbuild');

foreach ($_POST['materials'] as $mater){
    $name = $mater['name']; //or is it $id  (not sure... anyone more help)
    $material = $mater['material'];
    $stage = $mater['stage'];
    $unit = $mater ['unit'];
    // satanize inputs
$sql = mysql_query("UPDATE materials SET material='$material', stage='$stage', quantity='$quantity',unit='$unit' WHERE id='$name'");
}
?>

Link to comment
Share on other sites

I thought this was working but now i get an invalid augment.

 

when i hit submit i nothing appeared to happen, the page returned was black so i did view source and there was the error.

 

Could someone please double check the above code as I cant see whats wrong.

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.