Asked  7 Months ago    Answers:  5   Viewed   42 times

I am trying to look through some input fields in a form and add them to a database. The user sets the number of fields, so I can't do something like the code below because there is no specific number of fields.

for($i=0; $i<(number-of-fields); $i++)
{
    $_REQUEST['Question+$i']
}

I have tried this as well:

<?php
$con=mysqli_connect("","test","test","Flashcards");

foreach($_REQUEST['Question[]'] as $value)
    {
    $newcards="INSERT INTO Cards(Questions)
    VALUES($value)";
    mysqli_query($con,$newcards);
    }

mysqli_close($con);
?>

It just doesn't add anything to my database. How should I go about doing this? I am new to PHP and SQL and can't seem to figure this out.

 Answers

58

Given:

<input type="text" name="foo[]" />
<input type="text" name="foo[]" />
etc...

in your form, you'd loop over them with

foreach($_POST['foo'] as $index => $value) {
    ...
}

The [] in the field name will be stripped off by PHP and used as a hint that it should expect multiple values with the same name, causing it to create a sub-array inside $_GET/$_POST to accomodate those extra values.

You can also suggest which array keys PHP should use, e.g.

<input type="text" name="foo[1]" value="hi there" />
<input type="text" name="foo[abc]" value="TGIF!" />

echo $_POST['foo'][1]; // outputs "hi there"
echo $_POST['foo']['abc'] // outputs "TGIF!"

Multi-dimensional arrays are also supported, using the same notation/access methods.

Wednesday, March 31, 2021
 
rasmusx
answered 7 Months ago
86

You have nested objects, try the following:

echo '<table>';

foreach ($obj as $key => $value) {
    echo '<tr>';
    echo '<td>' . $value->Name . '</td>';
    echo '<td>' . $value->date . '</td>';
    echo '<td>' . $value->dealType . '</td>';
    echo '<td>' . $value->id . '</td>';
    echo '</tr>';
}

echo '</table>';
Wednesday, March 31, 2021
 
innovation
answered 7 Months ago
75

First insert the employees.

INSERT INTO mitarbeiter
            (maanrede,
             ...)
            VALUES(?,
                   ...);

Then insert the SIM card. Use last_insert_id() to get the ID created for the employee record.

INSERT INTO mobilfunkkarten
            (mfktarif,
             ...,
             maID)
            VALUES (?,
                    ...,
                    last_insert_id());
Saturday, May 29, 2021
 
pocketfullofcheese
answered 5 Months ago
95

Unwrap and downcast the objects to the right type, safely, with if let, before doing the iteration with a simple for in loop.

if let currentUser = currentUser, 
    let photos = currentUser.photos as? [ModelAttachment] 
{
    for object in photos {
        let url = object.url
    }
}

There's also guard let else instead of if let if you prefer having the result available in scope:

guard let currentUser = currentUser, 
    let photos = currentUser.photos as? [ModelAttachment] else 
{
    // break or return
}
// now 'photos' is available outside the guard
for object in photos {
    let url = object.url
}
Monday, August 9, 2021
 
Warrior
answered 3 Months ago
72

You're using a double-quoted string, so the rn will be seen as literal carriage return and line feed characters. You'll need to double-escape them as well: \r\n.

The "on line 2" in the error message is evidence of this - there's no actual second line if your query, but because of the embedded newline/carriage return, there is once it gets to MySQL.

Thursday, September 2, 2021
 
Yrtymd
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share