ElricLives Posted January 9, 2013 Share Posted January 9, 2013 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(); ?> Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted January 9, 2013 Share Posted January 9, 2013 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; Quote Link to comment Share on other sites More sharing options...
ElricLives Posted January 9, 2013 Author Share Posted January 9, 2013 I forgot to check if it worked (which it did). Thanks a lot for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.