在不同的 table sql/ php 中插入数据
Insert data in a different table sql/ php
你能帮帮我吗
我有两个不同的 tables
table tbl_users:
CREATE TABLE `tbl_users` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`prenom` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`adresse` varchar(100) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`roleid` tinyint(4) DEFAULT NULL,
`isActive` tinyint(4) DEFAULT 0,
`idjob`int(11),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
table 辛苦:
create table `travail`(
`id`int(11) NOT NULL,
`job` varchar(200) Default ' ' NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
我把idjob
作为外键.*
我想在用户中插入作业的值 table 但我不知道如何操作
“工作”部分的以下代码
register.php:
<div class="form-group">
<ul>
<li>
<label >
<input type="radio" name="job" value="traiteur" checked/>
<img class="imag" src="../img/traite.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="cuisinier" />
<img src="../img/cui.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="patissier" />
<img src="../img/patis.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="stylistcheveux" />
<img src="../img/ha.webp" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="makeupartist" />
<img src="../img/make.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="stylistvetements" />
<img src="../img/stv.png" />
</label>
</li>
<li>
<label >
<input type="radio" name="job" value="photographe" />
<img src="../img/ph.png" />
</label>
</li>
<li>
<label >
<input type="radio" name="job" value="musicien" />
<img src="../img/d.png" />
</label>
</li>
<li>
<label >
<input type="radio" name="job" value="dj" />
<img src="../img/dj.png" />
</label>
</li>
</ul>
</div>
<div class="form-group">
<button type="submit" name="register" class="btn btn-success">Register</button>
</div>
Users.php:
// User Registration Method
public function userRegistration($data){
$name = $data['name'];
$username = $data['username'];
$email = $data['email'];
$mobile = $data['mobile'];
$prenom = $data['prenom'];
$roleid = $data['roleid'];
$password = $data['password'];
$checkEmail = $this->checkExistEmail($email);
if ($name == "" || $username == "" || $email == "" || $mobile == "" || $password == "" ) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Please, User Registration field must not be Empty !</div>';
return $msg;
}elseif (strlen($username) < 3) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Username is too short, at least 3 Characters !</div>';
return $msg;
}elseif (filter_var($mobile,FILTER_SANITIZE_NUMBER_INT) == FALSE) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Enter only Number Characters for Mobile number field !</div>';
return $msg;
}elseif(strlen($password) < 5) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Password at least 6 Characters !</div>';
return $msg;
}elseif(!preg_match("#[0-9]+#",$password)) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Your Password Must Contain At Least 1 Number !</div>';
return $msg;
}elseif(!preg_match("#[a-z]+#",$password)) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Your Password Must Contain At Least 1 Number !</div>';
return $msg;
}elseif (filter_var($email, FILTER_VALIDATE_EMAIL === FALSE)) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Invalid email address !</div>';
return $msg;
}elseif ($checkEmail == TRUE) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Email already Exists, please try another Email... !</div>';
return $msg;
}else{
$sql = "INSERT INTO tbl_users(name, username, email, password, mobile,prenom,roleid) VALUES(:name, :username, :email, :password, :mobile,:prenom, :roleid)";
$stmt = $this->db->pdo->prepare($sql);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':username', $username);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', SHA1($password));
$stmt->bindValue(':mobile', $mobile);
$stmt->bindValue(':prenom', $prenom);
$stmt->bindValue(':roleid', $roleid);
$result = $stmt->execute();
if ($result) {
$msg = '<div class="alert alert-success alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success !</strong> Wow, you have Registered Successfully !</div>';
return $msg;
}else{
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Something went Wrong !</div>';
return $msg;
}
在您的 register.php 中,将职位 ID 作为值而不是职位名称会更实用。然后你就可以像插入名字和其他任何东西一样,把这个数字插入插入物中。您可以用标签包围您的输入,而不是将作业名称放在值中,如下所示:
<label><input type=radio name=job value=0> Cook</label>
<label><input type=radio name=job value=1> Driver</label>
<label><input type=radio name=job value=2> Superhero</label>
然后您在 php 一侧验证字段,就像您对所有其他字段所做的一样,并将其添加到查询中。 FK只是一个常规字段。
$sql = "INSERT INTO tbl_users(name, username, email, password, mobile,prenom,roleid) VALUES(:name, :username, :email, :password, :mobile,:prenom, :roleid, :jobid)";
$stmt = $this->db->pdo->prepare($sql);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':username', $username);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', SHA1($password));
$stmt->bindValue(':mobile', $mobile);
$stmt->bindValue(':prenom', $prenom);
$stmt->bindValue(':roleid', $roleid);
$stmt->bindValue(':jobid', $jobid);
$result = $stmt->execute();
你能帮帮我吗 我有两个不同的 tables
table tbl_users:
CREATE TABLE `tbl_users` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`prenom` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`adresse` varchar(100) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`roleid` tinyint(4) DEFAULT NULL,
`isActive` tinyint(4) DEFAULT 0,
`idjob`int(11),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
table 辛苦:
create table `travail`(
`id`int(11) NOT NULL,
`job` varchar(200) Default ' ' NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
我把idjob
作为外键.*
我想在用户中插入作业的值 table 但我不知道如何操作
“工作”部分的以下代码
register.php:
<div class="form-group">
<ul>
<li>
<label >
<input type="radio" name="job" value="traiteur" checked/>
<img class="imag" src="../img/traite.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="cuisinier" />
<img src="../img/cui.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="patissier" />
<img src="../img/patis.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="stylistcheveux" />
<img src="../img/ha.webp" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="makeupartist" />
<img src="../img/make.png" />
</label>
</li>
<li>
<label>
<input type="radio" name="job" value="stylistvetements" />
<img src="../img/stv.png" />
</label>
</li>
<li>
<label >
<input type="radio" name="job" value="photographe" />
<img src="../img/ph.png" />
</label>
</li>
<li>
<label >
<input type="radio" name="job" value="musicien" />
<img src="../img/d.png" />
</label>
</li>
<li>
<label >
<input type="radio" name="job" value="dj" />
<img src="../img/dj.png" />
</label>
</li>
</ul>
</div>
<div class="form-group">
<button type="submit" name="register" class="btn btn-success">Register</button>
</div>
Users.php:
// User Registration Method
public function userRegistration($data){
$name = $data['name'];
$username = $data['username'];
$email = $data['email'];
$mobile = $data['mobile'];
$prenom = $data['prenom'];
$roleid = $data['roleid'];
$password = $data['password'];
$checkEmail = $this->checkExistEmail($email);
if ($name == "" || $username == "" || $email == "" || $mobile == "" || $password == "" ) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Please, User Registration field must not be Empty !</div>';
return $msg;
}elseif (strlen($username) < 3) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Username is too short, at least 3 Characters !</div>';
return $msg;
}elseif (filter_var($mobile,FILTER_SANITIZE_NUMBER_INT) == FALSE) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Enter only Number Characters for Mobile number field !</div>';
return $msg;
}elseif(strlen($password) < 5) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Password at least 6 Characters !</div>';
return $msg;
}elseif(!preg_match("#[0-9]+#",$password)) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Your Password Must Contain At Least 1 Number !</div>';
return $msg;
}elseif(!preg_match("#[a-z]+#",$password)) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Your Password Must Contain At Least 1 Number !</div>';
return $msg;
}elseif (filter_var($email, FILTER_VALIDATE_EMAIL === FALSE)) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Invalid email address !</div>';
return $msg;
}elseif ($checkEmail == TRUE) {
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Email already Exists, please try another Email... !</div>';
return $msg;
}else{
$sql = "INSERT INTO tbl_users(name, username, email, password, mobile,prenom,roleid) VALUES(:name, :username, :email, :password, :mobile,:prenom, :roleid)";
$stmt = $this->db->pdo->prepare($sql);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':username', $username);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', SHA1($password));
$stmt->bindValue(':mobile', $mobile);
$stmt->bindValue(':prenom', $prenom);
$stmt->bindValue(':roleid', $roleid);
$result = $stmt->execute();
if ($result) {
$msg = '<div class="alert alert-success alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success !</strong> Wow, you have Registered Successfully !</div>';
return $msg;
}else{
$msg = '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error !</strong> Something went Wrong !</div>';
return $msg;
}
在您的 register.php 中,将职位 ID 作为值而不是职位名称会更实用。然后你就可以像插入名字和其他任何东西一样,把这个数字插入插入物中。您可以用标签包围您的输入,而不是将作业名称放在值中,如下所示:
<label><input type=radio name=job value=0> Cook</label>
<label><input type=radio name=job value=1> Driver</label>
<label><input type=radio name=job value=2> Superhero</label>
然后您在 php 一侧验证字段,就像您对所有其他字段所做的一样,并将其添加到查询中。 FK只是一个常规字段。
$sql = "INSERT INTO tbl_users(name, username, email, password, mobile,prenom,roleid) VALUES(:name, :username, :email, :password, :mobile,:prenom, :roleid, :jobid)";
$stmt = $this->db->pdo->prepare($sql);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':username', $username);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', SHA1($password));
$stmt->bindValue(':mobile', $mobile);
$stmt->bindValue(':prenom', $prenom);
$stmt->bindValue(':roleid', $roleid);
$stmt->bindValue(':jobid', $jobid);
$result = $stmt->execute();