PHP表单未将数据提交到数据库

时间:2018-11-30 23:11:29

标签: php mysql forms session

我正在尝试创建一个表单,该表单用于处理数据并将其提交到一个php文件中的数据库。当我将数据填充到表中并按Submit按钮时,它会按预期返回index.php页面。但是,我需要它来处理数据并将其发送到数据库(该数据库没有使用),并返回成功消息并再次返回空白表单。

我还有其他文件(更新和删除)成功地处理和更新了数据库,但是没有一个文件-我看不到我在做什么或对此插入新的联系人文件做错了什么。请帮忙!

PHP文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add new contact</title>
</head>
<body>

<?php

require('db.php');
include("authenticate.php");
$status = "";

if(isset($_POST['name'])) {
$name =$_REQUEST['name'];
$address = $_REQUEST['address'];
$phone = $_REQUEST['phone'];
$mobile = $_REQUEST['mobile'];
$email = $_REQUEST['email'];
$submittedby = $_SESSION["username"];

// Insert record in mycontacts database
    $ins_query = "INSERT INTO contact
(`conName`,`conAddress`,`conPhone`, `conMobile`, `conEmail`) VALUES
('$name','$address', '$phone', '$mobile', '$email');";
    mysqli_query($con,$ins_query)
or die(mysqli_connect_error());
$status = "New contact successfully added.
</br></br><a href='view.php'>View Inserted Record</a>";
echo $status;
}
else {
?>


<div class="form">
<p><a href="edit_contact.php">View all contacts</a> 
| <a href="logout.php">Logout</a></p>
<div>
<h1>Insert New Record</h1>
<form name="add contact" action="<?php echo ($_SERVER['PHP_SELF']); ?>" 
method="post" ;>
<input type="hidden" name="id" value="1" />
<table border="1" cellpadding="2">
<caption>Add/Edit Contact</caption>
<tr>
<td><label for="name">Name</label></td>
<td><input type="text" name="name" placeholder="Enter name" size="30" 
maxlength="50" tabindex="1" required/></td>
</tr>
<tr>
<td><label for="address">Address</label></td>
<td><textarea name="address" placeholder="Enter address" cols="45" rows="5" 
tabindex="2"></textarea></td>
</tr>
<tr>
<td><label for="phone">Phone</label></td>
<td><input type="text" name="phone" placeholder="Enter phone" size="20" 
maxlength="20" tabindex="3"/></td>
</tr>
<tr>
<td><label for="mobile">Mobile</label></td>
<td><input type="text" name="mobile" placeholder="Enter mobile" size="20" 
maxlength="20" tabindex="4"/></td>
</tr>
<tr> 
<td><label for="email">Email</label></td>
<td><input type="email" name="email" placeholder="Enter email" size="30" 
maxlength="50" tabindex="5"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" 
value="Update" tabindex="6"/>
</td>
</tr>
</table>
<input type="hidden" id="contactID" name="contactID" value=""/>
</form>

</div>
</div>
<?php
}
?> 

</body>
</html>

0 个答案:

没有答案
相关问题