我有HTML
代码,用于将form
数据发布到database
:
` <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" class="form-horizontal" id="createform">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="name" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="address" class="col-sm-2 control-label">Address</label>
<div class="col-sm-10">
<textarea name="address" id="address" cols="30" rows="3" placeholder="Address"></textarea>
</div>
</div>
<div class="form-group">
<label for="contact" class="col-sm-2 control-label">Contact</label>
<div class="col-sm-10">
<textarea name="contact" id="contact" cols="30" rows="3" placeholder="Contacts"></textarea>
</div>
</div>
<div class="form-group">
<label for="contactemail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" name="emailcontact" class="form-control" id="contactemail" placeholder="Email">`enter code here`
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Create" name="save"/>
</div>
</div>
</form>`
PHP
以下HTML Form
的代码:
` if($_POST['save'] == "Create"){
$current_user = intval($_SESSION['id']);
$query = "insert into addresses(name,address,contact,emailcontact,user_id) values('".$_POST['name']."','".$_POST['address']."','".$_POST['contact']."','".$_POST['emailcontact']."','".$current_user."')";
mysqli_query($conn, $query);
header("Location: addressbook.php");
}`
我的问题是,每当我通过点击Post
按钮尝试Create
带有数据的表单时,warning
下面显示的是web page
:
`Notice: Undefined index: save in C:\xampp\htdocs\working_files\Online_Address_Book_PHP\php\utility.php on line 9
Notice: Undefined index: delete in C:\xampp\htdocs\working_files\Online_Address_Book_PHP\php\utility.php on line 21
`
在我的网站中有很多$_POST['--']
个数据。并且对于all of them
我的browser
正在显示此warning messages
。我怎样才能克服这个问题?任何帮助表示赞赏。
答案 0 :(得分:1)
哇的!我自己解决了这个问题。只需在每个code
之前使用check
:
`if(isset($_POST['save'])){
.......
}`
我的代码解决方案如下:
`if(isset($_POST['save'])){
if($_POST['save'] == "Create"){
$current_user = intval($_SESSION['id']);
$query = "insert into addresses(name,address,contact,emailcontact,user_id) values('".$_POST['name']."','".$_POST['address']."','".$_POST['contact']."','".$_POST['emailcontact']."','".$current_user."')";
mysqli_query($conn, $query);
header("Location: addressbook.php");
}
}`