Jump to content

Recommended Posts

Posted

This has been bugging me all morning, and i cannt for the life of me find where im going wrong, when previewing within the browser, im getting 3 notices:

 

 

Notice: Undefined index: number in /Applications/MAMP/htdocs/CRUD_project/backend/luckyloves.php on line 71

 

Notice: Undefined index: email in /Applications/MAMP/htdocs/CRUD_project/backend/luckyloves.php on line 72

 

Notice: Undefined index: web in /Applications/MAMP/htdocs/CRUD_project/backend/luckyloves.php on line 73

 

for the Script :

 


<?php
//This is the Update and Create Page. 

include('connection.php');
//Connect to the databae and then create the form function
//Make sure all variables are defined here

function renderForm($name = '', $number = '', $email = '', $web = '', $error = '', $id = '')

   { ?>

       <!DOCTYPE html>
       <html>
       <head>
           <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           <title>
               <?php if($id != '') { echo "Edit A Love"; } else { echo "Create A New Love"; } ?>
           </title>
       </head>
       <body>

       <?php 
       // This is getting the errors and displaying them (If any)
       if($error != '') 
       {
           echo "<div style='padding:10px; border:1px solid red; color:red'/>" . $error . "</div>";
       }

       ?>

       <!-- Form to Process the Edit and Delete -->

       <form action="" method="post">
           <?php if($id != '') { ?>
               <!-- We hide the id -->
               <input type="hidden" name="id" value="<?php echo $id; ?>"/>
           <?php } ?>
               <p>Name:</p>
               <input type="text" name="name" value="<?php echo $name; ?>"/>
               <p>Number:</p>
               <input type="text" name="name" value="<?php echo $number; ?>"/>
               <p>Email:</p>
               <input type="text" name="name" value="<?php echo $email; ?>"/>
               <p>Web Address:</p>
               <input type="text" name="name" value="<?php echo $web; ?>"/>
               <br/>
               <input type="submit" name="submit" value="submit"/>
       </form>



       </body>
       </html>

<?php }

   //we check to see if the id is set
   //if it is then we EDIT else we CREATE NEW
   if(isset($_GET['id']))
   {
       //editing the exisitng record
       renderForm(NULL, NULL, NULL, NULL, NULL, $_GET['id']);
   }
   else
   {
       //Create a new record
       if(isset($_POST['submit']))
       {
           //htmlentities converts all characters to html entities.
           //ENT_QUOTES will prevent SQL injections
           $name = htmlentities($_POST['name'], ENT_QUOTES);
           $number = htmlentities($_POST['number'], ENT_QUOTES);
           $email = htmlentities($_POST['email'], ENT_QUOTES);
           $web = htmlentities($_POST['web'], ENT_QUOTES);

           if($name == '' || $number == '' || $email == '' || $web == '')
           {
               $error = 'ERROR: Please fill in all required fields';
               //error above displayed, but we dont want the user to reenter
               //the fields so we present the form with the already entered
               //results from above.
               renderForm($name, $number, $email, $web, $error);
           }

       }   
       else 
       {
           renderForm();
       }

   }


?>

 

Can anyone see where im going wrong? Im stating the variables within the renderForm function I cannot understand why im getting these Notices.

 

Kind Regards.

Posted

If you update the form fields name value to match properly, those errors should disappear:

 

<input type="text" name="name" value="<?php echo $name; ?>"/>
               <p>Number:</p>
               <input type="text" name="name" value="<?php echo $number; ?>"/>
               <p>Email:</p>
               <input type="text" name="name" value="<?php echo $email; ?>"/>
               <p>Web Address:</p>
               <input type="text" name="name" value="<?php echo $web; ?>"/>

Notice how the name="name" sections are all the same? It should be:

 

<input type="text" name="name" value="<?php echo $name; ?>"/>
               <p>Number:</p>
               <input type="text" name="number" value="<?php echo $number; ?>"/>
               <p>Email:</p>
               <input type="text" name="email" value="<?php echo $email; ?>"/>
               <p>Web Address:</p>
               <input type="text" name="web" value="<?php echo $web; ?>"/>

Posted

Damn it, it is always the simplest thing... I was looking for something within my code that was wrong, and didn't pay attention to the form elements.

 

I guess its all part of the learning curve. Thanks again Ben.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...