简体   繁体   中英

How to insert data in mysql table through PHP?

I can create table structure by PHP. But unable to insert data.

TABLE STRUCTURE

<?php

$tbl[]="
CREATE TABLE IF NOT EXISTS ".$table_prefix."admin_user (
  `admin_id` int(14) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(100) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `role` varchar(100) NOT NULL,
  `date_created` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
  `ip_address` varchar(50) NOT NULL,  
  `user_lang` int(14) NOT NULL,
  `email_address` varchar(255) NOT NULL,
  `lost_password_code` varchar(255) NOT NULL,
  `session_token` varchar(255) NOT NULL,
  `last_login` datetime NOT NULL,
  `user_access` text NOT NULL,
  PRIMARY KEY (`admin_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
";

/*insert user account*/

     $params_user=array( 
    'username'=>$data['username'],
    'password'=>md5($data['password']),
    'date_created'=>date('c'),
    'ip_address'=>$_SERVER['REMOTE_ADDR'],
    'user_access'=>$user_all_access
  );
  echo "<p class=\"uk-icon-chevron-right\"> Inserting user ...</p>";      
  $truncate="TRUNCATE TABLE ".$table_prefix."admin_user";   
  $db->raw($truncate);
  $db->create( $table_prefix."admin_user", $params_user); 
  echo "<div> [done]</div>";

Any help would be appreciated.

There is no actual line of code that tells the SQL-Server to put in any kind of data. To put in data, the SQL query should look like

"INSERT INTO ".$table_prefix."admin_user [...]"

Also: Even if you had some content in this table, the line

$truncate="TRUNCATE TABLE ".$table_prefix."admin_user";

is deleting everything inside. So every single row that might have been writen into this table is gone afterwards.

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