Jump to content

Simple, PHP input using the same form?


oni-kun

Recommended Posts

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?  :confused:

 

The table will have standard incrementing ID's, with the three entries (did/didn't exercise, length, weight)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :P.

 

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-

 

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.