dlebowski Posted February 2, 2009 Share Posted February 2, 2009 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> Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 2, 2009 Share Posted February 2, 2009 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"> Quote Link to comment Share on other sites More sharing options...
dlebowski Posted February 2, 2009 Author Share Posted February 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
Maq Posted February 2, 2009 Share Posted February 2, 2009 * 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']); '> '> } < Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 2, 2009 Share Posted February 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 2, 2009 Share Posted February 2, 2009 Double post trying to fix my previous one... seems I cannot delete this one. Oh well. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 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()); } Quote Link to comment Share on other sites More sharing options...
dlebowski Posted February 2, 2009 Author Share Posted February 2, 2009 That is what I needed. I have a hard time explaining what I am trying to do in these forums and I appreciate your patience. Ryan Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 I have a hard time explaining what I am trying to do in these forums ... Is it easier to explain in other forums? Seriously though, try to explain your needs in a non-technical way and let the discussion proceed from there. Quote Link to comment Share on other sites More sharing options...
dlebowski Posted February 2, 2009 Author Share Posted February 2, 2009 No. Forums in general for me are rough. As for "non-technical", that was what I was trying to do when I "slopped" code in there. Thanks again. Ryan Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.