if(isset($_POST['save'])) {
$name = $_POST['name'];
$product = $_POST['product'];
$price = $_POST['price'];
$email = $_POST['email'];
$uid = $_POST['uid'];
mysqli_begin_transaction($mysqli);
try {
/* Добавление значений */
$stmt = mysqli_prepare($mysqli, "INSERT INTO tovar (name,product,price,email) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'ssss', $name, $product, $price, $email);
mysqli_stmt_execute($stmt);
$stmt = mysqli_prepare($mysqli, "UPDATE shtuck SET sht = sht - 1 WHERE id=?");
mysqli_stmt_bind_param($stmt, 's', $uid);
mysqli_stmt_execute($stmt);
/* Если код достигает этой точки без ошибок, фиксируем данные в базе данных. */
mysqli_commit($mysqli);
} catch (mysqli_sql_exception $exception) {
mysqli_rollback($mysqli);
throw $exception;
}
mysqli_close($mysqli);
}
<?php
function curr_month_year() {
setlocale(LC_TIME, 'et_EE.UTF-8');
$month_year = strftime('%B %Y', time());
return $month_year;
}
echo curr_month_year();
<?php
function curr_month_year() {
setlocale(LC_TIME, 'et_EE.UTF-8');
return date('F Y');
}
echo curr_month_year();
create table types (
id int auto_increment primary key,
title varchar(255)
);
insert into types (title) values ('Одежда');
create table categories (
id int auto_increment primary key,
type_id int,
title varchar(255),
foreign key (type_id) references types(id)
);
insert into categories (type_id, title) values (1, 'Брюки'), (1, 'Рубашки');
create table goods (
id int auto_increment primary key,
category_id int,
title varchar(255),
foreign key goods_category (category_id) references categories(id)
);
insert into goods (category_id, title) values (1, 'Брюки мужские'), (2, 'Рубашка поло спорт');
select
goods.id,
types.title as type,
categories.title as category,
goods.title
from goods
join categories on goods.category_id = categories.id
join types on categories.type_id = types.id
;
<?php
$mystring = "The new brave world!";
$delimiter = ' ';
echo explode($delimiter, $mystring, 2)[1];
echo PHP_EOL;
echo trim(strstr($mystring, $delimiter));
echo PHP_EOL;
echo substr($mystring, strpos($mystring, $delimiter)+1);
echo PHP_EOL;
create table `calendar` (
`event` varchar(64),
`year` smallint unsigned,
`month` tinyint unsigned,
`day` tinyint unsigned,
`hour` tinyint unsigned,
`minute` tinyint unsigned
);
insert into calendar values
('December 31 every year at 23:55', null, 12, 31, 23, 55),
('every minute in december 2020', 2020, 12, null, null, null),
('every hour at first of month', null, null, 1, null, 0)
;
select `event`
from `calendar`
where
(`year` is null or `year` = year(now())) and
(`month` is null or `month` = month(now())) and
(`day` is null or `day` = day(now())) and
(`hour` is null or `hour` = hour(now())) and
(`minute` is null or `minute` = day(now()))
;
<?php
class StaticClass {
static $FILE_NAME = 'SOME_STATIC_FILE';
public static function getName()
{
return self::$FILE_NAME;
}
}
echo StaticClass::getName();
echo PHP_EOL . '====================' . PHP_EOL;
class InstansedClass {
private $FILE_NAME = 'SOME_INSTANCE_FILE';
public function getName()
{
return $this->FILE_NAME;
}
}
$instanseOfInstansedClass = new InstansedClass();
echo $instanseOfInstansedClass->getName();
$words = preg_split('/\W/', "Hello World");
function translate($word) {
$array = array(
'almaz' => "good",
'echo' => "php",
'html' => "css",
'sis' => "ki",
'hello' => 'привет',
);
$word = strtolower($word);
if (array_key_exists($word, $array)) {
echo $array[$word];
} else {
echo "Ключа $word не найдено.";
}
echo PHP_EOL;
}
foreach($words as $word) {
translate($word);
}
<?php
function split4bites ($input) {
$result = [];
$start = 0;
while ($start < strlen($input)) {
array_push($result, substr($input, $start, 4));
$start = $start + 4;
}
return $result;
}
$result = split4bites('jtoir75nsdt56odj');
print_r($result);
create table records (
`id` int not null auto_increment,
`text` text,
primary key(`id`)
);
<?php
//Получить значение текстового поля из запроса
$text = $_GET['text'];
//Подключиться к БД MySQL
$servername = "localhost";
$username = "username";
$password = "password";
try {
$pdo = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
throw new \PDOException($e->getMessage());
}
//Вставить данные в таблицу БД
$query = "insert into records (text) values (?);";
$stmt = $pdo->prepare($query);
$stmt->execute([$text]);
//Проверить вставленные данные
$query = "select * from records;";
$stmt = $pdo->prepare($query);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($data);
<?php
$arr = array("hello" => "world", "how" => "are", "you" => "dude");
$result = array_reduce(
array_keys($arr),
function($acc, $key) use ($arr) {
return $acc . $key . '=' . $arr[$key];
},
''
);
echo $result;
['products'] = [должен быть
'products' => [
$txt = "Doctor";
$str_replsace = str_replace(" ", "+", $txt);
$content = file_get_contents("https://www.bbc.co.uk/search?q=$str_replsace");
preg_match_all('/<a [^>]+><span aria-hidden="false">([^<]+)<\/span><\/a>/im', $content, $matches);
$titles= array_unique($matches[1]);
//var_dump($titles);
foreach ($titles as $title) {
echo $title . PHP_EOL;
}
WHERE DATE_FORMAT(column_1, '%Y-%m') = '2020-10';
foreach($stack as $value){
$value = explode(',', $value);
$xflist .= "<div class=" . $value[0] . ">" . $value[1] . "</div>";
}