php - 未定义索引:在第 29 行的 C:\xampp\htdocs\index.php 中提交
问题描述
注意:未定义索引:在第 29 行的 C:\xampp\htdocs\index.php 中提交
<?php
/**
* Plugin Name: Form Insert DB
* Plugin URI: http://solutionshint.com
* Description: Just Insert Data into Custom Form
* Version: 1.0
* Author: SolutionsHint
* Author URI: http://solutionshint.com
*/
function custom_form() {
?>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<label for name=""> Name:</label><br>
<input type = "text" name = "name" id = "name" placeholder = "Enter Name">
<label for name=""> City:</label><br>
<input type = "text" name = "city" id = "city" placeholder = "Enter City">
<label > State:</label><br>
<input type = "text" name = "state" id = "state" placeholder = "Enter State">
<label> Age:</label><br>
<input type = "text" name = "age" id = "age" placeholder = "Enter Age">
<input type = "submit" name = "submit" value = "Insert">
</form
<?php
}
// add_shortcode('display', 'custom_form');
if($_POST['submit']) {
global $wpdb;
$table_name ='student';
$name = $_POST['name'];
$city = $_POST['city'];
$state = $_POST['state'];
$age = $_POST['age'];
$success = $wpdb->insert("student", array(
"name" => $name,
"city" => $city,
"state" => $state,
"age" => $age ,
));
if($success) {
echo ' Inserted successfully';
} else {
echo 'not';
}
}
?>
这是我的代码,我是 php 新手,我正在开发一个插件,可以将表单数据保存到数据库中,但我得到了错误
注意:未定义索引:在第 29 行的 C:\xampp\htdocs\index.php 中提交
解决方案
试试这个代码。你需要使用if(isset($_POST['submit'])) {
<?php
/**
* Plugin Name: Form Insert DB
* Plugin URI: http://solutionshint.com
* Description: Just Insert Data into Custom Form
* Version: 1.0
* Author: SolutionsHint
* Author URI: http://solutionshint.com
*/
function custom_form() {
?>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<label for name=""> Name:</label><br>
<input type = "text" name = "name" id = "name" placeholder = "Enter Name">
<label for name=""> City:</label><br>
<input type = "text" name = "city" id = "city" placeholder = "Enter City">
<label > State:</label><br>
<input type = "text" name = "state" id = "state" placeholder = "Enter State">
<label> Age:</label><br>
<input type = "text" name = "age" id = "age" placeholder = "Enter Age">
<input type = "submit" name = "submit" value = "Insert">
</form
<?php
}
// add_shortcode('display', 'custom_form');
if(isset($_POST['submit'])) {
global $wpdb;
$table_name ='student';
$name = $_POST['name'];
$city = $_POST['city'];
$state = $_POST['state'];
$age = $_POST['age'];
$success = $wpdb->insert("student", array(
"name" => $name,
"city" => $city,
"state" => $state,
"age" => $age ,
));
if($success) {
echo ' Inserted successfully';
} else {
echo 'not';
}
}
?>
推荐阅读
- python - Keras 函数模型产生子类化错误
- c# - 在 Xamarin 中使用 AccessibilityService 类打开飞行模式
- elixir - 如何在 Elixir 和 Dialyzer 中覆盖函数或变量类型?
- mysql - 无法使用 docker-compose 创建准备好的 MySql DB
- flutter - 如何在 Flutter 中使用 Firebase 查询对象列表
- html - document.getElementsByName 返回未定义
- html - HTML5 三列布局,左/中可调整大小,右固定
- mysql - 用 2 个没有加入的表进行分组
- c# - 如何在 XAML 中访问 IEnumerable 的子属性?
- amazon-web-services - 让我们在 AWS Elastic Beanstalk 上加密