Jump to content

Recommended Posts

Is it possible to only use one submit button to update all the values of a query in a form?  What I have is the results of a query associated with input fields.  There will be up to 10 results per page.  Each has it's own individual entry in the database.  Can I go through somehow and update all the input fields and then click submit once and update all the fields?  Below is a rough sketch of what I am trying to do.  I would like to avoid javascript if possible.  Thank you for your help.

 


$query="SELECT table1, table2 FROM database;
$result=mysql_query($query);
$num=mysql_numrows($result);

<form> 

while($row = mysql_fetch_array($result)){

$table1 = ($row['table1']);
$table2 = ($row['table2']);

<input type='text' name='ud_table1' value='<? echo $table1 ?>'>
<input type='text' name='ud_table2' value='<? echo $table2 ?>'>

} 

<input type='submit' value=submit></form>

All inputs, selects, and textareas within a single HTML form tag will be submitted when the user presses submit.

 

Also, your form tag needs to have action and method attributes. Action defines where the form inputs are submitted to, method defines how they are submitted.

 

<form method="post" action="some_php_file.php">

I don't think I explained what I am trying to do very well.  The form submission will be to an update query.  How does that update query know to go through each input in the form submission and update the database?

 

The results of my query below will return many existing entries from the database for update.  I want to click the submit button once to update all of them.

* Not tested *

 

1) You're not opening closing your PHP tags...

2) Why are you selecting tables from a database?  You should be selecting columns from tables.

3) You need to tell what method and action to do in the form tag.

4) I still have no clue what you're trying to do, so please give an example or something.

5) Never use short tags ''.

 

if(isset($_POST['submit'])) {
  
  $query="SELECT table1, table2 FROM database;
  $result=mysql_query($query);
  $num=mysql_numrows($result);
}
?>

</pre>
<form method="POST" action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>">

while($row = mysql_fetch_array($result)){

$table1 = $row['table1'];
$table2 = $row['table2']);

'>
'>

}


<

I'm not 100% sure I follow you.

 

Are you trying to update every row in a table, and do this for multiple tables?

 

Or are you trying to update multiple rows in 1 or more tables?

 

These are vastly different things so it's important you be sure you know which you are doing.

 

If it's the former, you can do this pretty simply. Tables need to be predefined so there shouldn't be much need to get their names from the database. Besides, your query...

 

$query="SELECT table1, table2 FROM database;

 

...wouldn't return the names of the tables anyway. The syntax is

SELECT [rows] FROM [table]

- you have to select and use a single database to begin with.

 

You'd simply need to pass on the table names into the input fields. You could use a foreach loop and a switch to do the updates:

 

<?php
foreach ($_POST as $key => $value) {
   switch ($key) {
      case 'table1':
      case 'table2':
      case 'table3':
         $query = 'UPDATE '.$key.' SET '.$value;
         mysql_query($query);
         break;
      default:
         break;
    }
}

 

You should certainly verify incoming information though. It's always good to assume that the user has changed anything/everything they can to exploit your system and that the data they're submitting is malicious in any number of ways.

 

As for the latter, it'd be a bit more complicated as you would need to differentiate between tables while submitting individual row ID's. If this is what you're after, try reading this:

http://hostprogressive.com/support/php_5_docs/faq.html.html

 

Between the above code and that article you should be able to figure out an effective way of doing it.

 

I would stress again though, validate user input! If you're allowing people to submit even parts of SQL queries directly you best be sure you know what is being sent.

Assuming you are dealing with records from a single table and each record has a unique ID, here is an example of how it could be done

 

Form page:

//Query for the current records
$query="SELECT id, field1, field2 FROM table";
$result=mysql_query($query) or die(mysql_error());

//Open the form
echo "<form action=\"somepage.php\" method=\"POST\">\n";

//Create input fields for each record with current values
//Each field is an array with the id as the index
while($row = mysql_fetch_array($result))
{
    echo "<input type=\"text\" name=\"ud_field1[{$row['id']}]\" value=\"{$row['field1']}\" />\n"
    echo "<input type=\"text\" name=\"ud_field2[{$row['id']}]\" value=\"{$row['field2']}\" /><br>\n"
}

echo "<input type=\"submit\" value=\"submit\">\n";
echo "</form>\n";

 

Processing page (no validation/error handling included)

//Iterate through each input "set" and run query
foreach($_POST['ud_field1'] as $id => $field1)
{
    $field2 = $_POST['ud_field2'][$id];
    $query = "UPDATE table SET field1='{$field1}', field2='{$field2}' WHERE id='{$id}'";
    $result=mysql_query($query) or die(mysql_error());
}

I have a hard time explaining what I am trying to do in these forums ...

 

Is it easier to explain in other forums?  :D

 

Seriously though, try to explain your needs in a non-technical way and let the discussion proceed from there.

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.