0

Hi Guys,

Completely new to PHP! Just wanted to play around with Fields and submit stuff to a database on localhost through xampp.
This code writes to the database no problem, except no insert is happening with 'Telephone' and it throws this error;

Notice: Undefined index: Telephone in C:\xampp\htdocs\Inputs\update.php on line 9

Could you please inform me how i can maybe improve this code, and sort this error out?
Logic behind it is when the fields are all filled in great! However I was to bring into this if the same email has
been entered then it will throw an exception. I have set in the database 'Code' to be UNIQUE when tested it throws a message
saying duplicate entry. How can I improve this?

Many Thanks! :)

<!DOCTYPE html>
<html>
<head>
<title>Wowcher Code Submission</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
    <div class="container">
        <form method ="post" action="update.php">
            <label>First name:</label><br>
            <input type="text" name="First_Name"><br>
            <label>Last name:</label><br>
            <input type="text" name="Last_Name"><br>
            <label>Email:</label><br>
            <input type="text" name="Email"><br>
            <label>Wowcher Code:</label><br>
            <input type="text" name="Code"><br>
            <label>Telephone Number:</label><br>
            <input type="text" name="Phone"><br>
            <input type="submit" value="Submit">
        </form>
    </div>


</body>
</html>





<?php
    mysql_connect ("localhost", "root", "root") or die ('Error: ' . mysql_error());
    mysql_select_db ("wowcher") or die ('Error: ' . mysql_error());

    $First_name = $_POST['First_Name'];
    $Surname = $_POST['Last_Name'];
    $Email = $_POST['Email'];
    $Code = $_POST['Code'];
    $Telephone = $_POST['Telephone'];
    $id=isset($_POST['$Telephone']) ? trim($_POST["$Telephone"]) : "";
        $query="INSERT INTO wowcher_code(First_name, Last_Name, Email, Code, Telephone)VALUES ('".$First_name."', '".$Surname."', '".$Email."', '".$Code."', '".$Telephone."')";
        mysql_query($query) or die ('Error updating database because: '.mysql_error());
    //if(isset($_POST['notify_box'])){ $notify = $_POST['notify_box']; }



        echo "Details succesfully added!!"



?>
3
Contributors
10
Replies
36
Views
3 Years
Discussion Span
Last Post by Paul_17
Featured Replies
  • 1

    It looks like it doesn't know what `$Telephone = $_POST['Telephone'];` is since you are not posting `Telephone` you are posting: `Phone` so do this: $Telephone = $_POST['Telephone']; $id=isset($_POST['$Telephone']) ? trim($_POST["$Telephone"]) : ""; // has to be : $Telephone = $_POST['Phone']; $id=isset($Telephone) ? trim($Telephone) : ""; This should work Read More

  • 1

    First off, it's wrong. It should be: `$id = isset($Telephone) ? trim($Telephone) : " ";` The expression is a `Ternary` logic operator and it will assign `$id` to whatever is set. So it's just like doing the following: if(isset($Telephone) { $id = $Telephone; }else{ $id = ""; } Hope this … Read More

  • 1

    1. You can use `header()`, but you can't output the message then or you'll get a warning. 2. If you have a unique index on those columns, the insert will fail and you can catch that. If that's not what you want, do a select query first to see if … Read More

1

It looks like it doesn't know what $Telephone = $_POST['Telephone']; is since you are not posting Telephone you are posting: Phone so do this:

$Telephone = $_POST['Telephone'];
    $id=isset($_POST['$Telephone']) ? trim($_POST["$Telephone"]) : "";

// has to be :

$Telephone = $_POST['Phone'];
    $id=isset($Telephone) ? trim($Telephone) : "";

This should work

Edited by phorce

0

Fantastic!!! This works Brilliant!

I am a little confused though however. Need to understand the logic of what this does. My first question is ok ive set this;

$Telephone = $_POST['Phone'];
    $id=isset($_POST['$Phone']) ? trim($_POST["$Phone"]) : "";

But why isn't the same code applied for rest of the variables set for the PHP??

1

First off, it's wrong.

It should be:

$id = isset($Telephone) ? trim($Telephone) : " ";

The expression is a Ternary logic operator and it will assign $id to whatever is set. So it's just like doing the following:

if(isset($Telephone)
{
   $id = $Telephone;
}else{
  $id = "";
}

Hope this helps

EDIT:

So in theory what we do is:

1) Initialise a variable "$id" to hold the value passed through $_POST['Telephone'];

2) Using the tinary operator, assign $id with a value based on some logic: `if(isset($Telephone) // is there a vaue ? // yes there is $Telephone) // return $Telephone : // no there isn't a value, so 'id' gets stored as null || empty

Edited by phorce

0

Ok That makes sense now, Thank you very much for your help. I have no errors at all. Can you give me a tip what to do and where to look for for the other tweaks I want to do such as;

  1. After Message is displayed "Database Successfully updated!"
    for it to return back to the html form it was originally on?

  2. Also a tutorial somewhere on how to handle if Email and Code is UNIQUE and same values have already been entered it displays an error message specific to Email or Code?

0

P.S I am not after (gimme the code) Just a gentle push in the right direction. I like to try and figure things out for myself. Obviously me starting the topic was last straw for me.

1
  1. You can use header(), but you can't output the message then or you'll get a warning.
  2. If you have a unique index on those columns, the insert will fail and you can catch that. If that's not what you want, do a select query first to see if there is already a record with that infornmation.
1

After Message is displayed "Database Successfully updated!" for it to return back to the html form it was originally on?

Suppose you could use: header("Location: form.html");

Also a tutorial somewhere on how to handle if Email and Code is UNIQUE and same values have already been entered it displays an error message specific to Email or Code?

You should be able to find these, doing a DaniWeb search, and/or a good search.. But I'll write some functions to get you started..

    function checkEmail($email1, $email2) {

        return (bool) ($email1 == $email2) ? 'true' : 'false';
    }   

    function checkRex($email)
    {
        return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
    }

We can use these as follows:

// assume you've asked the person to confirm their email address:

$Email;
$Emailconfirm;

if(!checkEmail($Email, $Emailconfirm){
 die("Your emails do not match");
}

// function to check if email is valid

if(!checkRex($Email))
{
   die("Your email is not valid");
}

Or to check if the email already exists:

    function checkExists($email)
    {
        // sql details      
        $query = "SELECT email FROM users WHERE email='$email'";
        $result = mysql_query($query);
        if(mysql_affected_rows($result) == 1)
        {
            return (bool) true;
        }else{
            return (bool) false;
        }
    }

Hope this helps

Edited by phorce

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.