Jump to content


Photo

Anyone spot the issue?


  • Please log in to reply
2 replies to this topic

#1 CLU

CLU

    Member

  • Member
  • PipPip
  • 24 posts

Posted 07 December 2011 - 04:26 AM

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.
  • 0

#2 Ben

Ben

    Administrator

  • Administrators
  • 5,638 posts
  • LocationChico, CA

Posted 07 December 2011 - 08:05 AM

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; ?>"/>

  • 0
Benjamin Falk
Falken Creative : Twitter

#3 CLU

CLU

    Member

  • Member
  • PipPip
  • 24 posts

Posted 07 December 2011 - 08:41 AM

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.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users