Answer the question
In order to leave comments, you need to log in
Why are zeros written to the database?
Hello, I understand that the question is noob, but I'm still studying php and this situation arose.
if ($get_users != null) {
//Получаем id всех участников группы
$r = $get_users ['response'] ['count'];
//Циклом запписываем все полученные id в базу данных
for ($i=0; $i<= $r; $i++) {
$id = $get_users['response'] ['items'] [$i] ['id'];
$q = mysqli_query($db_connect, "INSERT INTO `users` (`id`) VALUES ('$id')");
//Получаем именна и фамилии участников
$first_name = $get_users['response']['items'][$i]['first_name'];
$last_name = $get_users['response']['items'][$i]['last_name'];
//Проверяем на пустоту переменные с именами и фамилиями
if (!empty($first_name) && !empty($last_name)) {
//Записываем имена и фамилии в базу
$query = mysqli_query($db_connect, "INSERT INTO `users` (`names`) VALUES ('$first_name $last_name')");
}
}
}
Answer the question
In order to leave comments, you need to log in
you have 2 inserts in your code.
one inserts IDs, the other names. Everything is correct in the base. Exactly according to your algorithm)
It would be more correct to throw away this horror, collect everything in a pile and insert it with one request:
INSERT INTO tbl_name
(a,b,c)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
the problem in your code is that you are doing 2 insert operations:
- mysqli_query($db_connect, "INSERT INTO `users` (`id`) VALUES ('$id')");
inserts a record with a filled id field, and an empty name field, since the value for insertion in the field is not specified
- the penultimate line of code inserts a record into the table with an unspecified id field value (0 by default) and a name value.
You need to write down the values of both fields in one insertion operation.
comment out or delete the line where the first insertion of the entry with the id is commented out or deleted,
and rewrite the penultimate one like this:
$query = mysqli_query($db_connect, "INSERT INTO `users` (`id`, `names`) VALUES ('$id', '$first_name $last_name')");
}
First of all:
var a = Math.round(Math.random()*99);
var b = Math.round(Math.random()*(99-a));
<span id="formula"></span>
<input type="text" id="answer"/>
var button=document.getElementById('start'); // Кнопка
var formula=document.getElementById('formula'); // Формула
var answer=document.getElementById('answer'); // Тестовое поле
var a,b;
button.onclick = function () {
a = Math.round(Math.random()*99);
b = Math.round(Math.random()*(99-a));
formula.textContent=a + ' + ' + b + ' = ' ;
};
answer.onkeyup=function(){
if (this.value-0 === a+b) alert('Ok');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question