Computing.Net > Forums > Web Development > PHP arrays and MySQL

PHP arrays and MySQL

Reply to Message Icon

Original Message
Name: cwoelkers
Date: July 26, 2008 at 11:28:51 Pacific
Subject: PHP arrays and MySQL
OS: OpenSuSE 11.0
CPU/Ram: P4 2.8Ghz/1GB
Comment:

I am attempting to create a database for my rather large collection of DVD-Rs. I have used phpMysql to create the database and have created two tables, one each for the two main types of content that is stored on the DVDs. Each DVD will be identified by a unique number. The content will then by split up by Title and other identifiers.
What I have been trying to do is create a PHP/HTML form that I can use to easily enter information into the database. I have the form created in HTML and some of the PHP done. My problem is that whenever I enter information into the form and hit submit, I then have to hit the back button to enter new information.

What I want to do is have two different buttons, one labeled Next and the other labeled Save. The Next button should enter the current form into an array and then clear the form. The Save button should send all of the information into the array to the MySQL database. I have almost no idea on how to code this but I do have something that resembles a flowchart that I came up with.
while loop
display form
when next button pushed
place form data into array
clear form
continue while loop
when save button pushed
for loop
place array data into mysql statement and send
end for loop when all array data has been sent
end while loop
I came up with this while thinking about using C for this but I don't feel like coding an entire interface for it.

If anyone can help me than please do.
Lastly, I have found the website http://www.php-mysql-tutorial.com which has been a big help in getting the basics of this down.
Thanks


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: July 26, 2008 at 16:22:58 Pacific
Reply: (edit)

If I am understanding you correctly you already have a page where you can anter information and have it saved to the database. But, the problem is that it then goes to another page where you have to hit the back button to see the form again.

So you have two pages: a form and a processing page. I am assuming the processing page has no output. You could then just use an include at the end of the processing page to include the form page.

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: cwoelkers
Date: July 26, 2008 at 16:41:43 Pacific
Reply: (edit)

While it does seem like two pages, it is actually only one source file. It can be found here: http://www.stpauls-fsu.org/newmance...

What you are suggesting Michael J might be possible but it would require a loop structure. An infinite while loop would work but how do I implement something like that?


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael J (by mjdamato)
Date: July 27, 2008 at 10:44:24 Pacific
Reply: (edit)

That makes it even easier. It is fairly simple to create such a page where if the data is entered succesfully then you show the form again to enter a new record. And, if it fails, to show the form again with the previously entered data to be corrected.

Here is an example:

<?php

//Check if data was POSTed
if ($_POST['submit'])
{
//Validate the data
$errors = false;

if (strlen($_POST['title'])<10)
{
$errors[] = "Title must be at least 10 characters.";
}
if (!is_numeric($_POST['year']) || strlen($_POST['year'])!=4)
{
$errors[] = "Year must be a 4-digit value.";
}

//If no errors insert into DB
if (!$errors) {
//mysql_query("INSERT INTO table (title, year) VALUES ('$title', '$year')");
$errorTxt = 'Data entered succesfully.';
}
else
{
$errorTxt = "<span style=\"color:#ff0000\">";
$errorTxt .= "The following errors occured:<ul>";
$errorTxt .= '<li>' . implode('</li><li>', $errors) . '</li>';
$errorTxt .= "</ul></span>";
$title = $_POST['title'];
$year = $_POST['year'];
}
}
?>
<html>
<body>
<?php echo $errorTxt; ?>
<form method="POST">
Title: <input type="test" name="title" value="<?php echo $title; ?>" />

Year: <input type="test" name="year" value="<?php echo $year; ?>" />

<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>

Michael J


Report Offensive Follow Up For Removal

Response Number 4
Name: cwoelkers
Date: July 27, 2008 at 19:18:24 Pacific
Reply: (edit)

Lets see if I got this. The page should be coded so that you have the following sections in this order.
1. php error checking
2. if no errors insert into DB
3. if errors setup error message and place info back into form without inserting into DB
4. start HTML
5. use php to display any errors
6. use HTML to setup and display form and end HTML

It's so simple that it just might work!
Thanks :)


Report Offensive Follow Up For Removal

Response Number 5
Name: cwoelkers
Date: July 27, 2008 at 19:54:05 Pacific
Reply: (edit)

OK I got it to work with a few minor modifications to your design. First was to clear all the form variables, such as $title and $year, after inserting them into the database. The other was that I defined and filled all of the form variables first, therefore removing all of the $_POST['year'] stuff in the validation and error reporting spots.
Other than that it worked great. Thanks again for the help.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP arrays and MySQL 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge