使用POST将数据插入数据库

时间:2014-05-21 13:05:15

标签: php mysql post

我需要编写一个代码来将联系人添加到数据库中。当我运行addcontact.html表单时,我的数据库中会添加一个新行,但是唯一包含任何数据的字段是autonumber字段。其余的都是空白的。我知道我的代码中一定有错误,但我似乎无法找到它的位置。

这是我的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);


?>

这是我在addcontact.html上的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"/>

我确实回显了我的$ sql,这是我得到的错误:

( ! ) 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 ('', '', '', 
'', '', '', '', '')

所以,然后我拿出echo $ sql,并确保删除标题代码,这样它就不会转发我,错误仍然存​​在。所以我至少到了某个地方。

2 个答案:

答案 0 :(得分:0)

Undefined index: address in D:\wamp\www\it665\insert.php on line 8表示您没有$_POST['address']。您没有发送您认为自己的POST数据。

var_dump数组上执行$_POST并查看其中的内容。

答案 1 :(得分:0)

您可以尝试执行此操作(如果contac_id是自动删除的话)

$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',  '')";

代替

$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',  '')";