Help! PHP debugging probs

For an assignment, I am using PHP to add a new customer to mySQL database. I am getting this error message:

Notice: Undefined index: name in C:\xampp\htdocs\bookorama\insert_customer.php on line 10

Notice: Undefined index: address in C:\xampp\htdocs\bookorama\insert_customer.php on line 11

Notice: Use of undefined constant customerid - assumed ‘customerid’ in C:\xampp\htdocs\bookorama\insert_customer.php on line 14
You have not entered all the required details.
Please go back and try again.

[php]new_customer.html

Book-O-Rama - New Customer Entry

Book-O-Rama - New Customer Entry

Name
Address
City

insert_customer.php

Book-O-Rama Customer Entry Results

Book-O-Rama Book Customer Results

<?php // create short variable names $customerid=$_POST['customerid']; $name=$_POST['name']; $address=$_POST['address']; $city=$_POST['city']; if (!customerid || !$name || !$address || !$city) { echo "You have not entered all the required details.
" ."Please go back and try again."; exit; } if (!get_magic_quotes_gpc()) { $customerid = addslashes($customerid); $name = addslashes($name); $address = addslashes($address); $city = addslashes($city); } @ $db = new mysqli('localhost', 'root', 'pac3', 'books'); if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } $query = "insert into customers values ('".customerid."', '".$name."', '".$address."', '".$city."')"; $result = $db->query($query); if ($result) { echo $db->affected_rows." customers inserted into database."; } else { echo "An error has occurred. The item was not added."; } $db->close(); ?> [/php]

This is one of the most common errors. Naming your form items.

In your HTML, you have NAME and the text box that goes with it is labelled name=“author”.
Also, you have ADDRESS and the text box that goes with it is labelled address=“title”.

You must have a “NAME” for each textbox you want to send in your form.
Usually a textbox for the NAME field would be name=“name” and for ADDRESS, it would be name=“address”.
The names in input fields are assigned using name="…" and it is used in the PHP file as $_POST[’…’];

Hope that helps…

Thanks! I’m still getting similar error messages:

Notice: Undefined index: address in C:\xampp\htdocs\bookorama\insert_customer.php on line 11

Notice: Undefined index: city in C:\xampp\htdocs\bookorama\insert_customer.php on line 12
You have not entered all the required details.
Please go back and try again.

Around line 12 on the php file you are missing a $ at the beginning of your variable

if (!customerid || !$name || !$address || !$city) {

Should be if (!$customerid…

Always check your code for variable spelling and matching form/$_POST names… Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service