0

I am not really familiar with array.

I try to save my data to database but its seem not working the only it save was please check my sample image.

Fields

and the database it only save was

DB

and it loop 4times

my codes below

<form method="post">
<table>
    <tr>
        <td>F Name</td>
        <td>M Name</td>
        <td>L Name</td>
    </tr>
    <tr>
        <td><input type="text" name="fname" value="" /></td>
        <td><input type="text" name="mname" value="" /></td>
        <td><input type="text" name="lname" value="" /></td>
    </tr>
    <tr>
        <td><input type="text" name="fname" value="" /></td>
        <td><input type="text" name="mname" value="" /></td>
        <td><input type="text" name="lname" value="" /></td>
    </tr>
</table>
<br />
<input type="submit" name="SubmitText" value="Save" />
</form>

if ( isset( $_POST['SubmitText'] ) ) {
    $fn = $_POST['fname'];
    $mn = $_POST['mname'];
    $ln = $_POST['lname'];
    $value = array();

    foreach( $_POST as $keys => $value)
    $db = mysql_query( "INSERT INTO jon_test VALUE( '', '".$value['fn']."', '".$value['mn']."', '".$value['ln']."' )" );

    echo $value['fn'] . '<br />';
    echo $db == true ? 'Yes' : 'No';
}

is there anything wrong with my codes?

4
  • 1
    input name are the same so u get only the last result Commented Jul 9, 2013 at 14:37
  • 2
    Obligatory: The mysql_* functions will be deprecated in PHP 5.5. It is not recommended for writing new code as it will be removed in the future. Instead, either the MySQLi or PDO and be a better PHP Developer. Commented Jul 9, 2013 at 14:41
  • 1
    How about everything. Everything is wrong with your code. 1. you're not escaping your strings, enabling sql injections. 2. you don't indent your code making it unreadable and thus prone to errors. 3. you're using mysql_* functions which are deprecated. Use mysqli or PDO. Commented Jul 9, 2013 at 14:42
  • thank you for your adviced sir Jason McCreary Commented Jul 9, 2013 at 14:51

3 Answers 3

2
<form method="post">
<table>
    <tr>
        <td>F Name</td>
        <td>M Name</td>
        <td>L Name</td>
    </tr>
    <tr>
        <td><input type="text" name="input[0][fname]" value="" /></td>
        <td><input type="text" name="input[0][mname]" value="" /></td>
        <td><input type="text" name="input[0][lname]" value="" /></td>
    </tr>
    <tr>
         <td><input type="text" name="input[1][fname]" value="" /></td>
        <td><input type="text" name="input[1][mname]" value="" /></td>
        <td><input type="text" name="input[1][lname]" value="" /></td>
    </tr>
</table>
<br />
<input type="submit" name="SubmitText" value="Save" />
</form>

After that you will need to iteratate the post parameters:

foreach($_POST['input'] as $input) {
    $db = mysql_query( "INSERT INTO jon_test VALUE( '', '".$input['fname']."', '".$input['mname']."', '".$input['lname']."' )" );

   echo $input['fname'] . '<br />';
   echo $db == true ? 'Yes' : 'No';
}
Sign up to request clarification or add additional context in comments.

4 Comments

@user1538668 your code is vulnerable to sql injections, it's good if you use PDO or Mysqli with prepared statements to escape the values before they are inserted in the database. php.net/manual/en/book.mysqli.php php.net/manual/en/book.pdo.php
yes i have class of mysqli its just that i used this mysql for test only. sorry next time i will post my class for mysqli
thank you I apply it to my system, it works not on my WebShop @lexmihaylov
sorry don't think i understood you right, so it is NOT working on your website. If that is the case could you post some code that we could review
2

Wrap your PHP code within <?php ?> tags like this:

<?php
if ( isset( $_POST['SubmitText'] ) ) {
    $fn = $_POST['fname'];
    $mn = $_POST['mname'];
    $ln = $_POST['lname'];
    $value = array();

    foreach( $_POST as $keys => $value)
    $db = mysql_query( "INSERT INTO jon_test VALUE( '', '".$value['fn']."', '".$value['mn']."', '".$value['ln']."' )" );

    echo $value['fn'] . '<br />';
    echo $db == true ? 'Yes' : 'No';
}
?>

Also, i recommend you use mysqli_* functions instead of mysql_* functions, because mysql_* functions are deprecated and will be removed in the futere.

1 Comment

I think the OP knows that already, maybe he forgot to type the tags in the questions. He managed to insert a few things to the database already.
1

input name are the same so you get only the last result use [] to create an array of values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.