Hello everybody,

I'm stuck with this and I hope there is somebody who can help me.

I have a Form

<html> <form action="" method="POST"> <input type="text" name="credentials[firstname]"> <input type="text" name="credentials[lastname]"> <input type="text" name="credentials[email]"> <input type="text" name="game[points]"> <input type="text" name="game[wins]"> <input type="submit" name="submit"> </form> </html>

and I want to post the values of this array to mysql

The array look like this when I insert some stuff

Array
(
    [credentials] => Array
        (
            [firstname] => rute
            [lastname] => dkp
            [email] => mail
        )

    [game] => Array
        (
            [points] => 12
            [wins] => 99
        )

    [submit] => Query verzenden
)

but the script only prints the credential parts.

this is the script

<?php

print "<pre>";
print_r($_POST);
print "</pre>";

if (isset($_POST['credentials'])){

    $myvalue = $_POST['credentials'];

    foreach ($myvalue as $value){

        echo $value;

    }

}

?>

What did I try?

I also tried

if (isset($_POST['credentials']['game'])){

    $myvalue = $_POST['credentials']['game'];

    foreach ($myvalue as $value){

        echo $value;

    }

But then the values won't print. I see the right output in the array though.

I hope someone can help me.
I don't have the insert statement yet.

Thanks
Rem

if (isset($_POST['credentials'])){
    $myvalue = $_POST['credentials'];
    foreach ($myvalue as $key => $value){
        echo $key." : ".$value.'<br />';
    }
}

if (isset($_POST['game'])){
    $myvalue = $_POST['game'];
    foreach ($myvalue as $key=>$value){
        echo $key." : ".$value.'<br />';
    }
}
Member Avatar for diafol

Unless you plan to serialize this data why are you placing them in formname arrays? Firstname would be just as good as credentials[firstname] or perhaps I.m missing something you haven.t mentioned.

@all

I forgot to mention that I want to create two sql statements.
$_POST['credentials'] => insert into table credentials
$_POST['game'] => update table game

I will look this week at my script and will post it here

All help appreciated..thanks

Rem

@all,

I made this far to difficult. This is the new form.

    <html> 
    <form action="" method="POST"> 
    <input type="text" name="firstname"> 
    <input type="text" name="lastname"> 
    <input type="text" name="email"> 
    <input type="text" name="points"> 
    <input type="text" name="wins"> 
    <input type="submit" name="submit"> 
    </form> 
    </html>

And just create two statement like this.

 if (isset($_POST['submit'])){

    $firstname = mysqli_real_escape_string($conn, $_POST['firstname']);
    $lastname = mysqli_real_escape_string($conn, $_POST['lastname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $points = mysqli_real_escape_string($conn, $_POST['points']);
    $wins = mysqli_real_escape_string($conn, $_POST['wins']);

    $sql = "INSERT into credentials (`firstname` etc) VALUES ('$firstname', ....etc)";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $sql2 = " INSERT into game (`points', ect) Values ('$'points',.....ect)";

    if ($conn->query($sql2) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

 }

Works as expected.

Thansk for the help

Just wanted to comment that I recommend strongly using prepare and execute instead of escaping strings, as that is a very error-prone method and may lead to SQL injections. See this code snippet for an easy to use function you can use instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.