[英]How do I insert user_id with ManyToOne relationship in symfony?
[英]How do I insert data based on user_id
这是我的代码,我需要帮助
我需要将user_id链接到第二个表中的user_location,这是我在此问题底部写的
<?php
/**
* Class UserInfo
* handles the insert information about a paricular User
*/
class UserInfo {
private $db_connection = null;
public $errors = array();
public $messages = array();
public function __construct() {
if(isset($_POST["info"])) {
$this->Insert();
}
}
/**
* Handles the Insertion of user Information into database
**/
private function Insert() {
if (empty($_POST['user_location'])) {
$this->errors[] = "Please fillout a location.";
} elseif (!empty($_POST['user_location'])) {
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$this->db_connection->connect_errno) {
$user_id = $_SESSION['user_name'];
$user_location = $this->db_connection->real_escape_string($_POST['user_location']);
$insert = "insert into users_info (user_location) values ('".$user_location."');";
$check_insert = $this->db_connection->query($insert);
if ($check_insert) {
$this->messages[] = "Location inserted into database successfully!";
} else {
$this->errors[] = "Location could not be inserted into database.";
}
}
}
}
}
?>
And here is my Login script
<?php
/**
* Class Login
* handles the user's login and logout process
*/
class Login {
/**
* @var object The database connection
*/
private $db_connection = null;
/**
* @var object Collection of error messages.
*/
public $errors = array();
/**
* @var Collection of success / neutral messages
*/
public $messages = array();
/**
* The function "__construct()" automatically starts whenever an object of this class is created,
* you know, when you do $login = new Login();"
*/
public function __construct() {
// create/read session
session_start();
// check the possible login actions:
// if the user tried to logout ( happens when user clicks logout button )
if (isset($_GET["logout"])) {
$this->doLogout();
}
// login via post data ( if the user just submitted a login form )
elseif (isset($_POST["login"])) {
$this->doLoginWithPostData();
}
} // end function __construct();
/**
* log in with post data
*/
private function doLoginWithPostData() {
// check login form contents
if (empty($_POST['user_name'])) {
$this->errors[] = "Username field is empty.";
} elseif (empty($_POST['user_password'])) {
$this->errors[] = "Password field was empty.";
} elseif (!empty($_POST['user_name']) && !empty($_POST['user_password'])) {
// create a database connection, using the constants from config/db.php
// which we loaded in index.php
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// change character set to utf8 and check it
if(!$this->db_connection->set_charset("utf8")) {
$this->errors[] = $this->db_connection->error;
}
// if no connection errors (working database connection)
if (!$this->db_connection->connect_errno) {
// escape the POST stuff
$user_name = $this->db_connection->real_escape_string($_POST['user_name']);
// database query, getting all the info of the selected user ( allows via email address
// in the username field. )
$sql = "select user_name, user_email, user_password_hash
from users
where user_name = '".$user_name."' or user_email = '".$user_name."';";
$result_of_login_check = $this->db_connection->query($sql);
// if this user exists
if ($result_of_login_check->num_rows == 1) {
// get result row (as an object)
$result_row = $result_of_login_check->fetch_object();
// using PHP 5.5's password_verify() function to check if the provided password fits
// the hash of that user's password
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {
// write user data into PHP SESSION (a file on your server)
$_SESSION['user_name'] = $result_row->user_name;
$_SESSION['user_email'] = $result_row->user_email;
$_SESSION['user_login_status'] = 1;
} else {
$this->errors[] = "Wrong password. Try again.";
}
} else {
$this->errors[] = "This user does not exist.";
}
} else {
$this->errors[] = "Database connection problem.";
}
}
}
/*
* Preform the logout
*/
public function doLogout() {
// delete the session of the user
$_SESSION = array();
session_destroy();
// return a little feeback message
$this->messages[] = "You have been logged out.";
}
/**
* simply return the current state of the user's login
* @return boolean user's login status
*/
public function isUserLoggedIn() {
if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) {
return true;
}
// default return
return false;
}
} // ------> Close class Login
?>
Here is my Database (mySQL)
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(64) NOT NULL,
user_password_hash varchar(255) NOT NULL,
user_email varchar(64) NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY user_name (user_name),
UNIQUE KEY user_email (user_email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS users_info (
user_id int(11) NOT NULL,
user_location varchar(255) NOT NULL,
PRIMARY KEY (user_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
外来约束= login2,用户,user_id(对不起,尚无法发布图片,需要10个信誉)
这是哪种编码? 您正在打破OOPS的所有概念。
现有代码存在问题,必须首先解决-
类UserInfo
您正在创建公共的数据成员。 它打破了数据隐藏的概念。 应该通过函数为数据成员分配值。 代替:public $ errors = array();
公共$ messages = array(); 使用:私人$ errors = []; PHP> = 5.4
私人$ messages = []; PHP> = 5.4
构造函数不应用于此类操作。
班级登录
再次缺少数据隐藏概念
不要在构造函数中使用会话启动,而要在输入脚本的第一行中使用会话启动。
表列数和插入查询中的值相等。
例如
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3)
所以您的插入查询将是
$insert = "insert into users_info (user_location) values ( '".$user_location."')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.