gpt4 book ai didi

php - wordpress中的数据插入

转载 作者:行者123 更新时间:2023-11-30 21:54:01 27 4
gpt4 key购买 nike

这是我的数据库连接的php文件

Connection.php

<?php
$connection = mysqli_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$db = mysqli_select_db($connection, 'wp');
if (!$db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>

这是向数据库插入记录的表单

add.php

        <?php
/*
Template Name: Student
*/
?>
<?php //global $pc_theme_object; /* Reference theme framework class */ ?>
<?php get_header(); ?>

<html>
<head>

</head>
<body>
<table>
<form action="?php echo $_SERVER["PHP_SELF"];?" method="POST" >
<tr><td>Name</td><td><input type="text" name="name"><br></td></tr>

<tr><td>Designation</td>
<td><select name="des">
<option value="ADMN. OFFICER">ADMN. OFFICER</option>
<option value="MANAGER">MANAGER</option>
<option value="SUPERVISER">SUPERVISER</option>
<option value="DTP DESIGNER">DTP DESIGNER</option>
<option value="MACHINE OPERATOR">MACHINE OPERATOR</option>
<option value="BINDER">BINDER</option>
<option value="BINDING ASST.">BINDING ASST.</option>
<option value="OFFICE ASSISTANT">OFFICE ASSISTANT</option>
</select></td></tr>
<tr><td>Salary:</td><td><input type="text" name="sal"></td></tr>
<tr><td>Staff_ID:</td><td><input type="text" name="staff_id"></td></tr>


<tr colspan="2"><td><input type="submit" name="submit" value="add"></td>
</tr>
</table>
<a href="http://localhost/wp/view-staff/">View</a>
</form>
</body>
</html>
<?php
include('connection.php');
global $wpdb;

if ( isset( $_POST['submit'] ) ){
$wpdb->insert( 'staff', array(
'name' => $_POST['name'],
'des' => $_POST['des'],
'sal' => $_POST['sal'],
'staff_id' => $_POST['staff_id']));

echo "submiteed";

}
?>


<?php get_footer(); ?>

虽然我提供数据以形成它显示找不到页面

page not found

当我单击按钮而不提交任何数据时,它会工作并在表中存储为空记录

empty submission

我需要你的帮助。请帮我。提前致谢

最佳答案

你绝对应该避免 https://codex.wordpress.org/WordPress_Query_Vars#Query_variables

使用最新代码

   <table>
<form method="POST" >
<tr><td>Name</td><td><input type="text" name="uname"><br></td></tr>

<tr>
<td>Designation</td>
<td>

<select name="des">
<option value="ADMN. OFFICER">ADMN. OFFICER</option>
<option value="MANAGER">MANAGER</option>
<option value="SUPERVISER">SUPERVISER</option>
<option value="DTP DESIGNER">DTP DESIGNER</option>
<option value="MACHINE OPERATOR">MACHINE OPERATOR</option>
<option value="BINDER">BINDER</option>
<option value="BINDING ASST.">BINDING ASST.</option>
<option value="OFFICE ASSISTANT">OFFICE ASSISTANT</option>
</select>

</td>

</tr>
<tr><td>Salary:</td><td><input type="text" name="sal"></td></tr>
<tr><td>Staff_ID:</td><td><input type="text" name="staff_id"></td></tr>
<tr colspan="2"><td><input type="submit" name="submit" value="add"></td>
</tr>
<a href="http://localhost/wp/view-staff/">View</a>
</form>
</table>

PHP代码

global $wpdb;
if ( isset( $_POST['submit'] ) ){
$wpdb->insert( 'staff', array(
'name' => $_POST['uname'],
'des' => $_POST['des'],
'sal' => $_POST['sal'],
'staff_id' => $_POST['staff_id']));

echo "submiteed";
}

关于php - wordpress中的数据插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46012335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com