Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have found some info here, but can comment to ask additional info. So my problem is: I want to select my data from mySQL. I have two tables: customers (id,name,ak,numeris) prekes (id, customer_id, prek_name, prek_value)

id in both tables is auto incremented.

I try to fill array?

I have only one value passed (customers.id). there are 5 records with same prekes.customer_id.

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM prekes WHERE customer_id=' . $pirkejas . ''; //$pirkejas = id passed via $_POST.
$q = $pdo->prepare($sql);
foreach ($pdo->query($sql) as $row) {
    //      if ($row['prek_pav'] != '') {
    $prekes = array($row['prek_name'], $row['prek_value']);

    Database::disconnect();

How to fill array $prekes in correct way?

Edit:

I want to print value in my form:

<table class="table-bordered">
<tr>
<td><input class="input-medium" name="prekes[1][pavadinimas]" type="text"  placeholder="Prekė" value=""></td>
<td><input class="input-medium" name="prekes[1][kaina]" type="text"  placeholder="Kaina" value=""></td>
</tr>
<tr>
<td><input class="input-medium" name="prekes[2][pavadinimas]" type="text"  placeholder="Prekė" value=""></td>
<td><input class="input-medium" name="prekes[2][kaina]" type="text"  placeholder="Kaina" value=""></td>
</tr>
<tr>
<td><input class="input-medium" name="prekes[3][pavadinimas]" type="text"  placeholder="Prekė" value=""></td>
<td><input class="input-medium" name="prekes[3][kaina]" type="text"  placeholder="Kaina" value=""></td>
</tr>
<tr>
<td><input class="input-medium" name="prekes[4][pavadinimas]" type="text"  placeholder="Prekė" value=""></td>

I my action does:

$pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO customers (name,pavarde,ak,data, numeris) values(?, ?, ?, ?,?)";
    $q = $pdo->prepare($sql);
    $q->execute(array($name, $pavarde, $ak, date("Y-m-d H:i:s", time()), $numeris));

    $pirkejo_id = $pdo->lastInsertId();

    foreach ($prekes as $preke) {
        //prekiu uzpildymas
        $sql = "INSERT INTO prekes (customer_id,prek_name,prek_value) values(?, ?, ?)";
        $q = $pdo->prepare($sql);
        $q->execute(array($pirkejo_id, $preke['pavadinimas'], $preke['kaina']));
    }

    Database::disconnect();
    header("Location: default.php");

I don't know how to get all values from database,

share|improve this question
    
depends on what you consider to be "the correct way". What do you want the array to look like exactly ? – Tularis Mar 6 '14 at 23:32
    
I updated my question. I am making this array to set values in input forn to edit them and put to database again. – pvaitonis Mar 6 '14 at 23:42

Don't inject values into your SQL queries. Use parameter binding instead.

$q = $pdo->prepare('SELECT id, prek_name, prek_value FROM prekes WHERE customer_id = ?');
$q->execute([$pirkejas]); // if PHP < 5.4, use array($pirkejas)
$prekes = $q->fetchAll(PDO::FETCH_ASSOC);

Now $prekes will be an array of rows where each row is an associative array.

<?php foreach ($prekes as $row) : ?>
<tr>
    <td>
        <input name="prekes[<?= (int) $row['id'] ?>][pavadinimas]"
               value="<?= htmlspecialchars($row['prek_name']) ?>">
    </td>
    <td>
        <input name="prekes[<?= (int) $row['id'] ?>][kaina]"
               value="<?= htmlspecialchars($row['prek_value']) ?>">
    </td>
</tr>
<?php endforeach ?>
share|improve this answer
    
How were his quotes messed up? He had an unnecessary ` . ''` at the end, but they didn't hurt anything. – Barmar Mar 6 '14 at 23:38
    
@Barmar Removed that note. I see two single quotes and I think SQL escaping :) – Phil Mar 6 '14 at 23:39
    
I don't know how to insert my new values back to my database 'prekes' using your code. – pvaitonis Mar 7 '14 at 10:44
    
<tr> <td> <input name="prekes[0][pavadinimas]" value=""> </td> <td> <input name="prekes[0][kaina]" value=""> </td> </tr> <tr> <td> <input name="prekes[0][pavadinimas]" value=""> </td> <td> <input name="prekes[0][kaina]"value=""> </td> </tr> value is not passed to form. – pvaitonis Mar 7 '14 at 11:07

PDO has a method that does this for you. Also, you shouldn't substitute variables directly into the query, you should use parameters.

$q = $pdo->prepare('SELECT * FROM prekes WHERE customer_id= :id');
$q->execute(array(':id' => $pirkejas));
$prekes = $q->fetchAll();
share|improve this answer
    
php.net/… – Phil Mar 6 '14 at 23:34
    
Fixed it -- it's so easy to get mixed up between PDO and mysqli. – Barmar Mar 6 '14 at 23:36
    
I think he meant you are not checking the return value of execute, to see if the query failed. if( $q && $q->execute() ) $prekes = $q->fetchAll(); – Rahly Mar 6 '14 at 23:37
    
The question has ERRMODE_EXCEPTION set, so I don't think it's necessary to test it. – Barmar Mar 6 '14 at 23:38
1  
I think you may be confusing queries that return no rows with queries that get an error. – Barmar Mar 6 '14 at 23:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.