Jump to content

Hello Everyone! I'd like some help please...


ElricLives

Recommended Posts

Hi.

 

I'm trying to add a Customer registration page, where the customers name and address are stored in separate tables, and I'm having issues.

 

This is the insert_ac.php page, where all the error messages direct me to line 21 and 23 (Undefined index: HouseNumberName and Undefined index: CityTown respectively)

 

Just in case it's not too obvious, PHP isn't my strong suit.

 

<?php

 

$host=''; // Host name

$username=''; // Mysql username

$password=''; // Mysql password

$db_name='comp 5130'; // Database name

$tbl_client='client'; // 1st Table name

$tbl_location= 'location'; //2nd table name

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// Get values from form

$cid=['ID'];

$lid=['ID'];

$firstname=$_POST['FirstName'];

$lastname=$_POST['LastName'];

$email=$_POST['EmailAddress'];

$cnumber=$_POST['ContactNumber'];

$house=$_POST['HouseNumberName'];

$street=$_POST['StreetName'];

$city=$_POST['CityTown'];

$postcode=$_POST['PostCode'];

 

// Insert data into client

$sql ="INSERT INTO $tbl_client(ID, FirstName, LastName, EmailAddress, ContactNumber)

VALUES('', '{$firstname}', '{$lastname}', '{$email}', '{$cnumber}')";

 

echo $sql;

 

$result = mysql_query($sql);

 

if (!$result) { die('Invalid query: ' . mysql_error());}

 

// Insert Data Into location

$sql2 ="Insert Into $tbl_location(ID, HouseNumberName, StreetName, CityTown, PostCode)

Values ('', '$house', '$street', '$city', '$postcode')";

 

echo $sql2;

 

$result2 = mysql_query($sql2);

 

if (!$result2) { die('Invalid query: ' . mysql_error());}

 

// if successfully insert data into client, displays message

if($result) {

 

echo "sucessful customer add";

echo "<BR>";

echo "<a href= 'insert.php'>Back To Main Page</a>";

}

else {

echo "That can't be good, Customer Error...";

}

 

// if successfully insert data into location, displays message

if($result2) {

 

echo "sucessful address";

echo "<BR>";

echo "<a href= 'insert.php'>Back To Main Page</a>";

}

else {

echo "That can't be good, Address Fail...";

}

?>

 

<?php

// close connection

mysql_close();

?>

Link to comment
Share on other sites

Undefined index means that you are attempting to access data inside an array (in this case the $_POST array) using a key that doesn't exist. Your error is due to either the form field name being spelled different to HouseNumberName and CityTown, or there is no data present in the field. What you are getting is a notice (more of a warning than an error) and your code should still function. However to clean this up check your form field names are correct and the same as what you are using as the $_POST array key and always check that data exists and is set before attempting to use it in any way i.e

 

// check if data is present and store in variable. if not variable will be FALSE
$house = (isset($_POST['HouseNumberName']) && !empty($_POST['HouseNumberName'])) ? $_POST['HouseNumberName'] : FALSE;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.