oni-kun Posted December 22, 2009 Share Posted December 22, 2009 I have an 'exercise' log that I wanted to do as a project for myself and my mind, lol. Basically it has the format: [checkbox] [txtbox - length] [txtbox - curweight] And there are 31 sets boxes going down, named: [did_or_not], [dec25len], [dec25weight] [did_or_not], [dec26len], [dec26weight] ..etc.. I know how to create a table to put them all in, but I want to come back to the table (page), say in a week, and update 2-3 tables at a time, if I couldn't before. What command can I use to update all? the table, rather than to (replace?/make a new one on submit?) Or does it update the table when I submit the form and overwrite the table anyway? The table will have standard incrementing ID's, with the three entries (did/didn't exercise, length, weight) Quote Link to comment Share on other sites More sharing options...
megaresp Posted December 22, 2009 Share Posted December 22, 2009 I'm not entirely clear what you're asking to do here. Have you written a PHP script that updates a database table with data supplied by a form? If so, surely when you return in 2-3 weeks the web page is fetching data from the database and creating the table on the fly? If not, then this is what you need to do. Or perhaps I've misunderstood entirely. It might be useful to see the page you're talking about. Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted December 22, 2009 Share Posted December 22, 2009 Try a loop or array to update all the records that are checked only? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 Try a loop or array to update all the records that are checked only? Lets say I enter the result for day 1 (pretend the checkbox doens't say anything), If I update one box and hit sumbmit, than later enter a few more and hit submit, as long as the other previous boxes are filled with previous records, they'd replace the old ones? (ones I already entered will appear, so will it just 'update' the previous ID's) and update the new one? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 I think I have it resolved. So my final question is. A) Can insert blank tables, IE "" and.. B) How do I overwrite those blank tables, if I enter a value finally into the corresponding box? will it automatically with insert, or do I need UPDATED? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 ? Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 22, 2009 Share Posted December 22, 2009 In MySQL, Being able to insert (create a new row) with _blank_ values depends entirely on how you made the table (more specifically, what options and default values you gave the column you want to be blank). You can pass blank values, and you will have to use UPDATE, if you want to change a row. -CB- Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 In MySQL, Being able to insert (create a new row) with _blank_ values depends entirely on how you made the table (more specifically, what options and default values you gave the column you want to be blank). You can pass blank values, and you will have to use UPDATE, if you want to change a row. -CB- Since there are many forms, that I may want to leave blank, I guess I need to use: if (!empty($_POST['len31']) { UPDATE ... //the value } else { INSERT... // "", blank space to retain the table } This should be right. I'll need to write A LOT of code if I don't use a for loop in this, Time to have fun Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted December 22, 2009 Share Posted December 22, 2009 You only need to update, if you are updating an existing row in the mysql table. If you are adding a new row (like a new person/log entry) then you can insert . So it's more like this: <?php // connect to to mysql // loop each id in the post array foreach($_POST['row_id_array'] As $thisid){ // Query, Uses the value of the ID fields to find the row (if there is one), in MySQL $query = "SELECT `id` FROM `mytable` WHERE `log_id`='".$thisid; $result = mysql_query($query) or die(mysql_error()); // Count number of rows that matched the query. $rows = mysql_num_rows($result); // What to do. if($rows == 0){ // Insert }else if($rows == 1){ // Update }else{ // More than one match. Umm - Shouldnt happen with unique id's } } ?> <!-- FORM --> <form action=".." method="post"> First Record <input type="hidden" name="row_id_array[0]" value="56" /> <input type="text" name="length[0]" value="" /> <input type="text" name="weight[0]" value="" /> Second Record <input type="hidden" name="row_id_array[1]" value="165" /> <input type="text" name="length[1]" value="" /> <input type="text" name="weight[1]" value="" /> ... </form> So, each row, will have a hidden "ID" field that tells the script the ID number of that row. It uses a POST array so that you can submit as many rows to update as you want. Then you just go through the loop (above), for each row_id_array, check if the row exists and act accordingly. (you can change the query only to match if the results are exactly the same, then update only those changed, but would require to recode the conditional statements). Hope it makes sense to you . -CB- 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.