Halløjsa
Jeg fumler med validering af en formular til opdatering af allerede indtastede oplysninger. Jeg har følgede formular med validering, og som gemmer de indtastede oplysninger i en MySQL database. Dette virker helt fint og valideringen giver de forventede oplysninger om fejl, så som manglende indtastning:
- [code lang="PHP"]<?php
- error_reporting(0);
-
- $myconn = mysql_connect("localhost", "root", "") or die(mysql_error());
- mysql_select_db("my_database", $myconn) or die(mysql_error());
- ?>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <title>Create Record</title>
-
- <script type="text/javascript">
- <!--
- function GoToURL()
- {
- window.location="record_form_create.php";
- }
- //-->
- </script>
- </head>
-
- <body>
-
- <h1>Create Record</h1>
-
- <form action="record_form_create.php" method="post">
-
- <?php
- $ValidateForm = $_POST['ValidateForm'];
-
- if ($ValidateForm == "Yes") {
-
- $My_Column_1 = $_POST['My_Column_1'];
- $My_Column_2 = $_POST['My_Column_2'];
-
- if ($My_Column_1 == "") {
- ($Error_My_Column_1 = "Yes");
- }
-
- if ($My_Column_2 == "") {
- ($Error_My_Column_2 = "Yes");
- }
- }
-
- if (($ValidateForm == "")
- or ($Error_My_Column_1 == "Yes")
- or ($Error_My_Column_2 == "Yes")) {
- ?>
-
- <p>My Column 1 <?php if ($Error_My_Column_1 == "Yes") echo "Error"?>
- <br /><input name="My_Column_1" style="width: 100px" type="text" value="<?php echo $My_Column_1?>" /></p>
-
- <p>My Column 2 <?php if ($Error_My_Column_2 == "Yes") echo "Error"?>
- <br /><input name="My_Column_2" style="width: 100px" type="text" value="<?php echo $My_Column_2?>" /></p>
-
- <input name="submit" type="submit" value="SUBMIT" />
- <input name="reset" onclick="GoToURL();" type="reset" value="RESET" />
-
- <input name="ValidateForm" type="hidden" value="Yes" />
- </form>
-
- </body>
- </html>
-
- <?php
- } else {
-
- mysql_query("INSERT INTO my_table (My_Column_1, My_Column_2) VALUES ('$My_Column_1', '$My_Column_2')") or die(mysql_error());
-
- mysql_query($query);
-
- mysql_close($myconn);
-
- header("Location: records.php");
- }
- ?>
[/code]
... men altså, i nedenstående eksempel har jeg problemer med for det første at eet felt husker de indtastede oplysninger, når der er fejl i et andet felt, og for det andet at valideringen ikke giver de forventede oplysninger om fejl, så som manglende indtastning:
- <?php
- error_reporting(0);
-
- $Column_ID = mysql_real_escape_string($_GET['Column_ID']);
-
- $myconn = mysql_connect("localhost", "root", "") or die(mysql_error());
- mysql_select_db("my_database", $myconn) or die(mysql_error());
-
- $result = mysql_query("SELECT * FROM my_table WHERE Column_ID='$Column_ID'") or die(mysql_error());
-
- $row = mysql_fetch_array($result);
- ?>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <title>Edit Record</title>
-
- <script type="text/javascript">
- <!--
- function GoToURL()
- {
- window.location="record_form_edit.php?Image_ID=<?php echo $Column_ID?>">;
- }
- //-->
- </script>
- </head>
-
- <body>
-
- <h1>Edit Record</h1>
-
- <form action="record_form_edit.php?Column_ID=<?php echo $Column_ID?>" method="post">
-
- <?php
- $My_Column_1_Form = $_POST['My_Column_1'];
- $My_Column_1_Database = $row['My_Column_1'];
-
- $My_Column_2_Form = $_POST['My_Column_2'];
- $My_Column_2_Database = $row['My_Column_2'];
-
- $ValidateForm = $_POST['ValidateForm'];
-
- if ($ValidateForm == "Yes") {
-
- $My_Column_1 = $row['My_Column_1'];
- $My_Column_2 = $row['My_Column_2'];
-
- if ($My_Column_1 == "") {
- ($Error_My_Column_1 = "Yes");
- }
-
- if ($My_Column_2 == "") {
- ($Error_My_Column_2 = "Yes");
- }
- }
-
- if (($ValidateForm == "")
- or ($Error_My_Column_1 == "Yes")
- or ($Error_My_Column_2 == "Yes")) {
- ?>
-
- <p>My Column 1 <?php if ($Error_My_Column_1 == "Yes") echo "Error"?>
- <br /><input name="My_Column_1" style="width: 100px" type="text" value="<?php if (isset($_POST['submit'])) echo $My_Column_1_Form;
- else
- echo $My_Column_1_Database?>" /></p>
-
- <p>My Column 2 <?php if ($Error_My_Column_2 == "Yes") echo "Error"?>
- <br /><input name="My_Column_2" style="width: 100px" type="text" value="<?php if (isset($_POST['submit'])) echo $My_Column_2_Form;
- else
- echo $My_Column_2_Database?>" /></p>
-
- <input name="submit" type="submit" value="SUBMIT" />
- <input name="reset" onclick="GoToURL();" type="reset" value="RESET" />
-
- <input name="ValidateForm" type="hidden" value="Yes" />
-
- </form>
-
- </body>
- </html>
-
- <?php
- } else {
-
- $My_Column_1 = $_POST['My_Column_1'];
- $My_Column_2 = $_POST['My_Column_2'];
-
- $query = "UPDATE my_table SET My_Column_1='$My_Column_1', My_Column_2='$My_Column_2' WHERE Column_ID='$Column_ID'";
-
- mysql_query($query);
- mysql_close($myconn);
-
- header("Location: records.php");
- }
- ?>
Jeg har tidligere brugt en lignende metode i ASP med to variabler, een for databaseoplysninger og een for formularoplysninger, for at det enkelte felt kunne "maintain state" og det virkede helt fint.
Er der nogen, der kan give en hjælpende hånd?