Hello friends,

I am creating a registration form page.In that i have to edit and delete.So in edit page..I am getting the error.Here I will post the codings and let me know my queries...

If i am run the edit.php i am getting this error -" Error! "

<?php

 function renderForm($id, $name, $email,$error)
 {
 ?>

 <html>
 <head>
 <title>Edit Record</title>
 </head>
 <body>
 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 

 <form action="" method="post">
 <input type="hidden" name="id" value="<?php echo $id; ?>"/>
 <div>
 <p><strong>ID:</strong> <?php echo $id; ?></p>
 <strong>Your Name: *</strong> <input type="text" name="name" value="<?php echo $name; ?>"/><br/>
 <strong>email: *</strong> <input type="text" name="email" value="<?php echo $email; ?>"/><br/>
 <p>* Required</p>
 <input type="submit" name="submit" value="Submit">
 </div>
 </form> 
 </body>
 </html> 
 <?php
 }



 // connect to the database
 include('dbadmin.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['id']))
 {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
  $email= mysql_real_escape_string(htmlspecialchars($_POST['email']));

 if ($name == '' ||  $email == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($id, $name, $email,$error);
 }
 else
 {
 // save the data to the database
 mysql_query("UPDATE login SET name='$name',email='$email' WHERE id='$id'")
 or die(mysql_error()); 


 header("Location: viewrec.php"); 
 }
 }
 else
 {

 echo 'Error!';
 }
 }
 else

 {

 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 // query db
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM login WHERE id=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);


 if($row)
 {


 $name = $row['name'];
 $email = $row['email'];

 renderForm($id, $name,$email, '');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else

 {
 echo 'Error!';
 }
 }
?>

thanks..

    When posting code, please use the forum markup tags described in the FAQs to mark up your code to make it readable.

    Find the place in your code that produces the error message, and work out what has to happen for that bit of code to be reached. That should tell you something about what is wrong. (Of course, if it were well-written, the error message itself would have some information about what the error was.)

      Weedpacket;11016763 wrote:

      When posting code, please use the forum markup tags described in the FAQs to mark up your code to make it readable.

      Find the place in your code that produces the error message, and work out what has to happen for that bit of code to be reached. That should tell you something about what is wrong. (Of course, if it were well-written, the error message itself would have some information about what the error was.)

      When i run this program i displays output as - Error!

      But i want to edit the data from database.

      thanks.

        If i'm not mistaken your cod is working as you wish, just has a confusing part of code.

        First, you got to the edit page by passing the id: viewrec.php?id=232323

        Okay, if you can see the form the program works. Then on a submit, you run the SQL string and
        the table is updating, then you have a wrong redirect!

          header("Location: viewrec.php");  

        See? If you load this PHP file, your $_GET["id"] hasn't been set and doesn't have numeric type,
        this is the reason you got this Error! message. Lets have a think of a better redirection on a success UPDATE.

          Find the place in your code that produces the error message, and work out what has to happen for that bit of code to be reached. That should tell you something about what is wrong. (Of course, if it were well-written, the error message itself would have some information about what the error was.)

            Write a Reply...