L
L
Lev Rozanov2016-02-04 21:08:13
PHP
Lev Rozanov, 2016-02-04 21:08:13

How to write input content to database from PHP loop?

Hello dear developers.
I apologize for this wording of the question, but I could not do it differently.
I ask for help.
There are two database tables users and deposit.
A selection of username and card is made from the users table.
It is necessary to add a text field to each selection result and at the very end one submit button.
After submitting the form, the values ​​of the text field and the username under which this field was filled should be recorded in the deposit table.
Accordingly, there are as many sample results as there are records in the deposit table.
Below is the code that makes the selection and in which you need to make changes.
I apologize in advance if the question turned out to be elementary.
I will be very grateful for your help!
I wish you success!

function getLimitedIdPath2($limit, $id){
  global $db;
  $user = mysqli_fetch_assoc(mysqli_query($db['lnk'], "SELECT * FROM `users` WHERE `id`='".$id."' LIMIT 1"));
  $chain = array();
  $i = 0;
  while(((int)$user['id'] != 0) && ($i < $limit)){
    array_unshift($chain, array($user['id'], $user['username'], $user['card']));
    $parent_id = $user['parent_id'];
    $user = mysqli_fetch_assoc(mysqli_query($db['lnk'], "SELECT * FROM `users` WHERE `id`='".$parent_id."' LIMIT 1"));
    $i++;
  } 
  return $chain;
}
  	
  $id = $_SESSION['id'];
  $limit = '11'; 
  $chain = getLimitedIdPath2($limit, $id);
             foreach($chain as $cur){
                 echo 'username: '.$cur[1];
                 echo 'card: '.$cur[2];
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
ZLOFENIX, 2016-02-04
@MetisKot

Terrible code.
Well, let's say this:
foreach($chain as $cur)
And then:
foreach($_POST['arr'] as $k=>&$v)
$k - name, $v - form value.

A
asd111, 2016-02-04
@asd111

You have sql injection in your code. Use PDO or prepared statements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question