I need to write a code to add contacts to a database. When I run the addcontact.html form, a new line is added to my database, but the only field that has any data is the autonumber field. The rest are blank. I know there must just be an error in my code, but I cannot seem to find where it is.
Here is my insert.php
<?php
$con=mysqli_connect("localhost","root","","mycontacts");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = mysqli_real_escape_string($con, $_POST['name']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$city = mysqli_real_escape_string($con, $_POST['city']);
$state = mysqli_real_escape_string($con, $_POST['state']);
$zip = mysqli_real_escape_string($con, $_POST['zip']);
$phone = mysqli_real_escape_string($con, $_POST['phone'])
$sql="INSERT INTO Contacts (contac_id, contact_name, contact_address, contact_city,
contact_state, contact_zip_code, contact_phones, contact_website)
VALUES ('', '$name', '$address', '$city', '$state', '$zip',
'$phone', '')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
header('Location:mainmenu.php');
session_start();
$_SESSION['loggedIn'] = true;
mysqli_close($con);
?>
And here is my code for my html form on addcontact.html:
<form action="insert.php" method='post'>
<table width="250">
<tr>
<p>
<td align="right" valign="middle">
Name:
</td>
<td align="right" valign="middle">
<input type="text" class= "textbox" name="name" />
</td>
</p>
</tr>
<tr>
<p>
<td align="right" valign="middle">
Address:
</td>
<td align="right" valign="middle">
<input type="text" class= "textbox" name="address" />
</td>
</p>
</tr>
<tr>
<p>
<td align="right" valign="middle">
City:
</td>
<td align="right" valign="middle">
<input type="text" class= "textbox" name="city" />
</td>
</p>
</tr>
<tr>
<p>
<td align="right" valign="middle">
State:
</td>
<td align="right" valign="middle">
<input type="text" class= "textbox" name="state" />
</td>
</p>
<tr>
<p>
<td align="right" valign="middle">
Zip:
</td>
<td align="right" valign="middle">
<input type="text" class= "textbox" name="zip" maxlength='10'/>
</td>
</p>
</tr>
<tr>
<p>
<td align="right" valign="middle">
Phone
</td>
<td align="right" valign="middle">
<input type="text" class= "textbox" name="phone" />
</td>
</p>
</tr>
<tr>
<td colspan="2">
<center>
<br>
<input type='submit' class= "submitbutton" value="Submit"/>
I did echo out my $sql, and this is the error I get:
( ! ) Notice: Undefined index: name in D:\wamp\www\it665\insert.php on line 7
( ! ) Notice: Undefined index: address in D:\wamp\www\it665\insert.php on line 8
( ! ) Notice: Undefined index: city in D:\wamp\www\it665\insert.php on line 9
( ! ) Notice: Undefined index: state in D:\wamp\www\it665\insert.php on line 10
( ! ) Notice: Undefined index: zip in D:\wamp\www\it665\insert.php on line 11
( ! ) Notice: Undefined index: phone in D:\wamp\www\it665\insert.php on line 12
INSERT INTO Contacts (contac_id, contact_name, contact_address, contact_city,
contact_state, contact_zip_code, contact_phones, contact_website) VALUES ('', '', '',
'', '', '', '', '')
So, I then took out the echo $sql, and made sure to remove the header code so it would not forward me, and the errors still remained. So I am getting somewhere at least.
Undefined index: address in D:\\wamp\\www\\it665\\insert.php on line 8
means that you don't have a $_POST['address']
. You are not sending POST data that you think you are.
Do a var_dump
on the $_POST
array and see what's in it.
You can try this(if contac_id is auto increament)
$sql="INSERT INTO Contacts (contact_name, contact_address, contact_city,
contact_state, contact_zip_code, contact_phones, contact_website)
VALUES ('$name', '$address', '$city', '$state', '$zip',
'$phone', '')";
Instead of
$sql="INSERT INTO Contacts (contac_id, contact_name, contact_address, contact_city,
contact_state, contact_zip_code, contact_phones, contact_website)
VALUES ('', '$name', '$address', '$city', '$state', '$zip',
'$phone', '')";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.