A
A
alihang2018-04-11 12:30:12
PHP
alihang, 2018-04-11 12:30:12

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')");
        }
    }
}

With this code, I am trying to get the data of VK community subscribers. All the data I need (names, surnames and id) I get as I need. Then I need to write them to the database and they are written, but in the following format: 5acdd528e2820838411243.png
That is, zero is inserted through the line there. In principle, this does not bother me, but when receiving data from the database and displaying them in the html table, empty lines appear. How can I get rid of them?
PS I tried to delete zeros from the table - it turned out somehow crooked and all id were replaced with zeros

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
vism, 2018-04-11
@alihang

you have 2 inserts in your code.
one inserts IDs, the other names. Everything is correct in the base. Exactly according to your algorithm)

S
Stalker_RED, 2018-04-11
@Stalker_RED

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);

V
Vlad_fox, 2018-04-11
@Vlad_fox

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')");
        }

K
Kuzzy, 2014-03-08
@pashulke

Here's a quick jsfiddle.net/9m7z9

R
rumkin, 2014-03-08
@rumkin

First of all:

var a = Math.round(Math.random()*99);
var b = Math.round(Math.random()*(99-a));

So you will always get the sum up to 100.
After the button, add the elements:
<span id="formula"></span>
<input type="text" id="answer"/>

Then:
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');
}

Then you will figure it out yourself.
Secondly, you can do without PHP.
Thirdly, no one hangs event handlers this way for a long time.
You can measure time in seconds (up to a thousandth) using Date.now() directly in the browser.
jsfiddle.net/z3dyR/2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question