使用codeigniter将数据输入数据库

时间:2018-09-04 11:03:40

标签: php laravel codeigniter

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model{
	function __construct(){
		$this->userTbl='login';

	}


public function insert($data=array()){
	if(!array_key_exists("created", $data)){
            $data['created'] = date("Y-m-d H:i:s");
        }

         if(!array_key_exists("modified", $data)){
            $data['modified'] = date("Y-m-d H:i:s");
        }

         $insert = $this->db->insert($this->userTbl, $data);


         if($insert){
            return $this->db->insert_id();;
        }else{
            return false;
        }
    }







}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('user');
    }


     /*
     * User registration
     */

     public function registration(){
     	$data=array();
     	$userData=array();


if($this->input->post(regisSubmit)){
	$this->form_validation->set_rules('name', 'Name', 'required');
	$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
	$this->form_validation->set_rules('password', 'password', 'required');
	$this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');

}

$userData=array(
	'name'=>strip_tags($this->input->post('name')), 
	'email'=>strip_tags($this->input->post('email')),
	'password'=>strip_tags($this->input->post('password')),
	'gender'=>strip_tags($this->input->post('gender')),
	'phone'=>strip_tags($this->input->post('phone'))

);

if($this->form_validation->run()==true){
	$insert=$this->user->insert($userData);
	if($insert){
		 $this->session->set_userdata('success_msg', 'Your registration was successfully. Please login to your account.');

		 
		redirect(users/login);

	}
	else{
		$data['error_msg']='Try again';

	}
}


     }
$data['user'] = $userData;
        //load the view
        $this->load->view('users/registration', $data);



    }
<!DOCTYPE html>
<html lang="en">  
<head>
<link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
<div class="container">
    <h2>User Registration</h2>
    <form action="" method="post">
        <div class="form-group">
            <input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
          <?php echo form_error('name','<span class="help-block">','</span>'); ?>
        </div>
        <div class="form-group">
            <input type="email" class="form-control" name="email" placeholder="Email" required="" value="<?php echo !empty($user['email'])?$user['email']:''; ?>">
          <?php echo form_error('email','<span class="help-block">','</span>'); ?>
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="phone" placeholder="Phone" value="<?php echo !empty($user['phone'])?$user['phone']:''; ?>">
        </div>
        <div class="form-group">
          <input type="password" class="form-control" name="password" placeholder="Password" required="">
          <?php echo form_error('password','<span class="help-block">','</span>'); ?>
        </div>
        <div class="form-group">
          <input type="password" class="form-control" name="conf_password" placeholder="Confirm password" required="">
          <?php echo form_error('conf_password','<span class="help-block">','</span>'); ?>
        </div>
        <div class="form-group">
            <?php
            if(!empty($user['gender']) && $user['gender'] == 'Female'){
                $fcheck = 'checked="checked"';
                $mcheck = '';
            }else{
                $mcheck = 'checked="checked"';
                $fcheck = '';
            }
            ?>
            <div class="radio">
                <label>
                <input type="radio" name="gender" value="Male" <?php echo $mcheck; ?>>
                Male
                </label>
            </div>
            <div class="radio">
                <label>
                  <input type="radio" name="gender" value="Female" <?php echo $fcheck; ?>>
                  Female
                </label>
            </div>
        </div>
        <div class="form-group">
            <input type="submit" name="regisSubmit" class="btn-primary" value="Submit"/>
        </div>
    </form>
    <p class="footInfo">Already have an account? <a href="<?php echo base_url(); ?>users/login">Login here</a></p>              
</div>
</body>
</html>

在这里,我需要使用codeigniter将数据插入mysql数据库。但数据未插入数据库,也未显示任何错误。以下是模态,控制器和视图页面的代码。在database.php文件中,一切正常。在这里,如何调试代码以及​​错误是什么。提前致谢。

1 个答案:

答案 0 :(得分:0)

您从不向控制器发布任何内容。看看你的视图文件的这一行。

import React, { Component } from "react";
import ExpenseItem from "./ExpenseItem.js";


class ExpensesList extends Component {
    state = {
        expenses: [{ id: '12345', name: 'Pizza', cost: '20' }],
        cost: "",
        name: ""
    };

    handleChange = (event, name) => {
       this.setState({ [name]: event.target.value });
    };

    handleSubmit = (event) => {
        event.preventDefault();

        let newExpense = {
           id: Math.random(),
           name: this.state.name,
           cost: this.state.cost
        }

        this.setState(prevState => ({
           expenses: [...prevState.expenses, newExpense],   // add new expense to expenses array
           cost: "", // reset this field
           name: "" // reset this field
        }));

    }

    render() {
        return (
            <div>
                <ul className="list-group">
                    {this.state.expenses.map((expense) => (
                        <ExpenseItem
                            id={expense.id}
                            name={expense.name}
                            cost={expense.cost}
                        />
                    ))}
                </ul>
                <div className="row mt-3">
                    <h2> Add Expenses </h2>
                    <form onSubmit={this.handleSubmit}>
                        <div className="row">
                            <div className="form-group">
                                <label for="name">Name</label>
                                <input
                                    required="required"
                                    type="text"
                                    className="form-control"
                                    id="name"
                                    value={this.state.name}
                                    onChange={e => this.handleChange("name")}
                                ></input>
                            </div>
                            <div className="form-group">
                                <label for="name">Cost</label>
                                <input
                                    required="required"
                                    type="text"
                                    className="form-control"
                                    id="cost"
                                    value={this.state.cost}
                                    onChange={e => this.handleChange("cost")}
                                ></input>
                            </div>
                            <div className="form-group">
                                <button type="submit" className="btn btn-primary"> Add Expense</button>
                            </div>
                        </div>
                    </form>
                </div>

            </div>


        );
    }
}

export default ExpensesList;

更改为:

<form action="" method="post">