How can I use another function to check if the email is in the right format ([number of symbols]#[number of symbols].[number of symbols]) and if it's unique while adding it using the insertUser? (All info is read from a .txt file).
Code:
class User{
function randomPassword($length = 8){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function insertUser($firstName, $lastName, $email){
global $DB;
$DB->query("INSERT INTO `users` (`fname`, `lname`, `password` , `email`) values ('$firstName', '$lastName', '" . randomPassword() . "', '$email') ");
}
In the index file:
$USER = new User();
$data = readData();
foreach($data as $nr => $user){
$USER->insertUser($user['first_name'], $user['last_name'], $user['email']);
}
Call as $this->randomPassword();
Email validation
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])
?*#([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email)){
list($username,$domain)=split('#',$email);
if(!checkdnsrr($domain,'MX')) {
return false;
}
return true;
}
return false;
}
To insert the random string you should do this :
Change the insertUser function to :
function insertUser($firstName, $lastName, $email){
global $DB;
$DB->query("INSERT INTO `users` (`fname`, `lname`, `password` , `email`) values ('$firstName', '$lastName', '" . $this->randomPassword() . "', '$email') ");
}
Related
I have the following code
$name = 'myname';
$mail = 'my#mail.it';
$qwt = "INSERT INTO `agenz` (`name`, `email`) VALUES (?,?)";
$result = $connessione->prepare($qwt);
$result->bind_param('ss', $name, $mail);
$result->execute();
I want print the query
"INSERT INTO `agenz` (`name`, `email`) VALUES ('myname','my#mail.it')"
for creating a log.So how to do that?
thanx.
Try fullQuery like below:
$name = 'myname';
$mail = 'my#mail.it';
$qwt = "INSERT INTO `agenz` (`name`, `email`) VALUES (?,?)";
$result = $connessione->prepare($qwt);
$result->bind_param('ss', $name, $mail);
$result->execute();
echo $result->fullQuery;
or
$result->debugQuery();
var_dump($result->debugBindedVariables());
if you want to get the error
var_dump($result->errno);
I have solved!
$qwt = "INSERT INTO `age` (`name`,`email`) VALUES (?,?)";
$par = array($name, $mail);
$result = $connessione->prepare($qwt);
$result->bind_param('ss', $par[0], $par[1]);
$result->execute();
echo "contenuto: " .SqlDebug($qwt, $par);
function SqlDebug($raw_sql, $params=array())
{
$keys = array();
$values = $params;
foreach ($params as $key => $value)
{
// check if named parameters (':param') or anonymous parameters ('?') are used
if (is_string($key)) { $keys[] = '/:'.$key.'/'; }
else { $keys[] = '/[?]/'; }
// bring parameter into human-readable format
if (is_string($value)) { $values[$key] = "'" . $value . "'"; }
elseif (is_array($value)) { $values[$key] = implode(',', $value); }
elseif (is_null($value)) { $values[$key] = 'NULL'; }
}
$raw_sql = preg_replace($keys, $values, $raw_sql, 1, $count);
return $raw_sql;
}
You can use variable directly in the statement like following
$name="ABC";
$mail="me#someone.com";
$value= "I am $name mail id $mail";
I'm trying to add information in two tables by checking the user type. If user will be a normal user then record must be entered in first table and if user type is doctor then record must be entered in both tables. I'm working in CodeIgniter 3.6
public function model_signup()
{
$nameVar = $this->input->post("signup_name");
$emailVar = $this->input->post("signup_email");
$phoneVar = $this->input->post("signup_phone");
$passwordVar = $this->input->post("signup_password");
$ifDoctorVar = $this->input->post("signup_ifdoctor");
$pmdcVar = $this->input->post("signup_pmdc");
$signu_query = $this->db->query("SELECT * FROM `doc_users` WHERE `user_email`='".$emailVar."'");
if($signu_query->num_rows() >0){
return false;
}
else
{
$this->db->query("INSERT INTO `doc_users`
(`user_nicename`,
`user_login`,
`user_email`,
`display_name`,
`user_phone`,
`user_pass`,
`user_type`,
`user_registered`)
VALUES ('$nameVar',
'$emailVar',
'$emailVar',
'$nameVar',
'$phoneVar',
'$passwordVar',
'$ifDoctorVar',
NOW())");
$user_id = $this->db->insert_id();
if($ifDoctorVar=='Doctor') { // checking if user is doctor or not
$this->db->query("INSERT INTO `doc_doc_details`
(`ID`,
`pmdc_id`,
`email`,
`phone)
VALUES ('$user_id',
'$pmdcVar',
'$emailVar',
'$phoneVar')");
return true;
}
}
}
public function model_signup()
{
$nameVar = $this->input->post("signup_name");
$emailVar = $this->input->post("signup_email");
$phoneVar = $this->input->post("signup_phone");
$passwordVar = $this->input->post("signup_password");
$ifDoctorVar = $this->input->post("signup_ifdoctor");
$pmdcVar = $this->input->post("signup_pmdc");
$signu_query = $this->db->query("SELECT * FROM
`doc_users` WHERE `user_email`='".$emailVar."'");
if($signu_query->num_rows() >0){
return false;
}
else
{
$this->db->query("INSERT INTO `doc_users`
(`user_nicename`,
`user_login`,
`user_email`,
`display_name`,
`user_phone`,
`user_pass`,
`user_type`,
`user_registered`)
VALUES ('$nameVar',
'$emailVar',
'$emailVar',
'$nameVar',
'$phoneVar',
'$passwordVar',
'$ifDoctorVar',
NOW())");
$user_id = $this->db->insert_id();
if($ifDoctorVar=='Doctor') { // checking if user is doctor or not
$this->db->query("INSERT INTO `doc_doc_details`
(`ID`,
`pmdc_id`,
`email`,
`phone)
VALUES ('$user_id',
'$pmdcVar',
'$emailVar',
'$phoneVar')");
// return true remove this
}
}
}
I use the Paypal IPN. When the user buys articles, I use this code to generate codes they can redeem. However, it's not working:
public function clave(){
$Valores = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$ValorTemporal = "";
for($i=0;$i<10;$i++) {
$ValorTemporal .= substr($Valores,rand(0,37),1);
}
return $ValorTemporal;
}
public function cupon($items){
$mysqli = $this->connection();
for ($i = 1; $i <= $items; $i++) {
$yz= $this->clave();
$sqli.$i = $mysqli->query("INSERT INTO ventas (id_venta, id_usuario,id_producto ,used,cupon) VALUES ('$txn_id','$username','$item_name','$used','$yz')");
}
return true;
}
$items is the number of products
I don't know if I'm using the for() statement correctly.
public function cupon($items){
global $txn_id, $username, $item_name, $used;
$mysqli = $this->connection();
for ($i = 1; $i <= $items; $i++) {
$yz = $this->clave();
$mysqli->query("
INSERT INTO `ventas`
(`id_venta`, `id_usuario`, `id_producto`, `used`, `cupon`)
VALUES
('$txn_id', '$username', '$item_name', '$used', '$yz')
");
}
return true;
}
OR little better
public function cupon($items, $txn_id, $username, $item_name, $used){
$mysqli = $this->connection();
for ($i = 1; $i <= $items; $i++) {
$yz = $this->clave();
$mysqli->query("
INSERT INTO `ventas`
(`id_venta`, `id_usuario`, `id_producto`, `used`, `cupon`)
VALUES
('$txn_id', '$username', '$item_name', '$used', '$yz')
");
}
return true;
}
I'm working on a domain shortening service called "sHTTP". It uses a MySQL database to store the shortened URLs. I can't insert them though.
Here's my code:
function db(){
$link = mysqli_connect('sqlserver', 'user', 'pass', 'db') or die(mysqli_error());
return $link;
}
$url = mysqli_real_escape_string(db(), $_POST['url']);
$ip = $_SERVER['REMOTE_ADDR'];
function checkexists($name){
// check if shttp exists
$q = mysqli_num_rows(mysqli_query(db(),"SELECT name FROM shttp WHERE name = '$name'"));
if($q > 0){
return true;
} else {
return false;
}
}
function generateRandStr($length){
// generate string for placeholder name
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
if(checkexists($name)){
die('sHTTP name exists already!');
}
if($_POST['url'] == ''){
die('No URL entered!');
}
if(!$_POST['name']){
$name = generateRandStr(5);
}else{
$name = mysqli_real_escape_string(db(), $_POST['name']);
}
//THIS IS MY MAIN PROBLEM HERE GUISE
$query = "INSERT INTO shttp(name, url, ip) VALUES ($name, $url, $ip)";
//Y U NO WORKING
$exe = mysqli_query(db(), "INSERT INTO shttp (name, url, ip) VALUES ($name, $url, $ip)");
if(!$exe){
//I'M GETTING THE DIE HALP
die('Error: Could not be processed');
} else {
echo 'sHTTP created!<br>URL: <a href=http://shttp.tk/ '.$name.'>http://shttp.tk/'.$name.'</a>';
}
I'm getting the "Error: Could not be processed" I set up.
Also, my DB table is this:
name varchar(255)
url varchar(255)
ip varchar(255)
I believe that's how I set it up in my code as well.
If anyone can help, I would appreciate it. Thank you for your time.
You need to use quote for the string values as
$query = "INSERT INTO shttp(name, url, ip) VALUES ('$name', '$url', '$ip')" ;
this might be a bit tricky. First here is my function:
public function transfer_smf_userinfo($username)
{
$sys_columns = implode(', ', $this->_config['bbauth']['columns']['sys']);
$smf_columns = implode(', ', $this->_config['bbauth']['columns']['smf']);
$question_marks;
for($i = 0; $i > sizeof($this->_config['bbauth']['columns']['sys']); $i++)
{
if(sizeof($this->_config['bbauth']['columns']['sys']) == $i - 1)
{
$question_marks .'?';
}
else {
$question_marks .'?, ';
}
}
$user = $this->smf_db->query('SELECT '. $smf_columns .' FROM `smf_members` WHERE member_name = ? LIMIT 1', array($username));
if($user->num_rows > 0)
{
$user = $user->row_array();
$user['register_date'] = date('Y-m-d H:i:s', $user['register_date']);
$user['last_login'] = date('Y-m-d H:i:s', $user['last_login']);
$transfer_user = $this->sys_db->query('INSERT INTO `members` ('. $sys_columns .') VALUES ('. $question_marks .')', array());
if($transfer_user)
{
return true;
}
else {
return false;
}
}
else {
return false;
}
}
$sys_columns outputs:
id, username, password, email, url, avatar, b_date, r_date, l_date
$smf_columns outputs:
id_member, member_name, passwd, email_address, website_url, avatar, birthdate, date_registered, last_login
I need the array at the end of the line on the $transfer_user query to have a array like so.
array($user['column'], $user['another']);
But I need to do it dynamically with each row from $smf_columns.
If I understand the issue correctly. Try:
$data=array();
foreach($this->_config['bbauth']['columns']['smf'] as $column)
$data[]=$user[$column];
$transfer_user = $this->sys_db->query(
'INSERT INTO `members` ('. $sys_columns .') VALUES ('. $question_marks .')',
$data);