简体   繁体   中英

In wordpress, Not able to insert Custom registration form data into Database and my PHP code is printing on the Form(which i have kept in shortcode)

I am using Custom Form to Collect the data from Customer. So i used below PHP code to insert the data into DB.

`

<?php
require_once(‘../../../wp-load.php’);
global $wpdb;
$first_name = $_POST[‘first_name’];
$last_name = $_POST[‘last_name’];
$user_email = $_POST[‘user_email’];
$table_name = $wpdb ->prefix . “Customer”;
$wpdb->insert( $table_name, array(‘cust_id’=>2, ‘first_name’ =>$first_name, ‘last_name’ =>$last_name ,user_email=>$user_email) ); ?>

`

This Code i have taken from google and I dont see any error on php code. But it is printing from prefix.”Customer” ….. to till the end of the code in Registration form while loading. After a very long trial i identified that the “>” symbol on the code is not taking and it is printing. So i changed all “>” symbol to “>” then i dont see any line printing on my Registration Form.

But Once i Click Register button it is not inserting data into my Custom Table(“Customer” table) rather it is not showing any error too..

So I guess my short code is not taking php code, Please let me know what if anyone came across this kind of issue.

Thanks in advance.

I'm not familiar with WP however I believe I can decipher what is going on and what is needed.

Try this. It will pull the highest cust_id and add one to it, and assign it as a variable to the insert query.

<?php
require_once(‘../../../wp-load.php’);
global $wpdb;    
$first_name = $_POST[‘first_name’];
$last_name = $_POST[‘last_name’];
$user_email = $_POST[‘user_email’];
$table_name = $wpdb ->prefix . “Customer”;
$custID = $wpdb->get_results(" SELECT cust_id FROM $table_name ORDER BY cust_id DESC limit 1 ");
$wpdb->insert( $table_name, array(‘cust_id’=>($custID + 1), ‘first_name’ =>$first_name, ‘last_name’ =>$last_name ,user_email=>$user_email) ); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM