1
\$\begingroup\$

Is there a way to simplify the code below? I want to simplify it, but I don't know if it is possible.

In the table below, I insert data which will end up in a MySQL table.

HTML

<table border="0">
    <tr>
        <td align="center">Dată / Interval orar</td>
        <td align="center">Denumire</td>
        <td align="center">Moderatori</td>
        <td align="center">Detalii</td>
    </tr>
    <tr>
        <td align="center"><input name="DataInterval1" type="text" id="DataInterval1"></td>
        <td align="center"><input name="Denumire1" type="text" id="Denumire1"></td>
        <td align="center"><input name="Moderatori1" type="text" id="Moderatori1"></td>
        <td align="center"><input name="Detalii1" type="text" id="Detalii1"></td>
    </tr>
    <tr>
        <td align="center"><input name="DataInterval2" type="text" id="DataInterval2"></td>
        <td align="center"><input name="Denumire2" type="text" id="Denumire2"></td>
        <td align="center"><input name="Moderatori2" type="text" id="Moderatori2"></td>
        <td align="center"><input name="Detalii2" type="text" id="Detalii2"></td>
    </tr>
    ...
    <tr>
        <td align="center"><input name="DataIntervaln" type="text" id="DataIntervaln"></td>
        <td align="center"><input name="Denumiren" type="text" id="Denumiren"></td>
        <td align="center"><input name="Moderatorin" type="text" id="Moderatorin"></td>
        <td align="center"><input name="Detaliin" type="text" id="Detaliin"></td>
    </tr>
</table>

Part of the PHP code that I want to simplify:

$DataInterval1=$_POST['DataInterval1'];
$Denumire1=$_POST['Denumire1'];
$Moderatori1=$_POST['Moderatori1'];
$Detalii1=$_POST['Detalii1'];   
$DataInterval2=$_POST['DataInterval2'];
$Denumire2=$_POST['Denumire2'];
$Moderatori2=$_POST['Moderatori2'];
$Detalii2=$_POST['Detalii2'];
...
$DataIntervaln=$_POST['DataIntervaln'];
$Denumiren=$_POST['Denumiren'];
$Moderatorin=$_POST['Moderatorin'];
$Detaliin=$_POST['Detaliin'];   

// Insert data into mysql
$sql="INSERT INTO tabel (DataInterval, Denumire, Moderatori, Detalii)VALUES
('".$DataInterval1."', '".$Denumire1."', '".$Moderatori1."', '".$Detalii1."'),
('".$DataInterval2."', '".$Denumire2."', '".$Moderatori2."', '".$Detalii2."'),
... 
('".$DataIntervaln."', '".$Denumiren."', '".$Moderatorin."', '".$Detaliin."')";
$result=mysql_query($sql);
\$\endgroup\$
1
  • \$\begingroup\$ The mysql extension has been deprecated; no longer in active development, and no longer supported in php5.5x. Using the old mysql extension will make your code less future proof. Look into PDO and mysqli (-mysql "improved" extension) and also take advantage of their support for OOP. \$\endgroup\$
    – shmuli
    Sep 15, 2013 at 18:51

2 Answers 2

1
\$\begingroup\$

Use indexing in HTML name attributes

<table>
    <tr>
        <td>Dată / Interval orar</td>
        <td>Denumire</td>
        <td>Moderatori</td>
        <td>Detalii</td>
    </tr>
    <tr>
        <td><input name="DataInterval[0]" type="text" id="DataInterval1"></td>
        <td><input name="Denumire[0]" type="text" id="Denumire1"></td>
        <td><input name="Moderatori[0]" type="text" id="Moderatori1"></td>
        <td><input name="Detalii[0]" type="text" id="Detalii1"></td>
    </tr>
    ...
    <tr>
        <td><input name="DataInterval[n]" type="text" id="DataIntervaln"></td>
        <td><input name="Denumire[n]" type="text" id="Denumiren"></td>
        <td><input name="Moderatori[n]" type="text" id="Moderatorin"></td>
        <td><input name="Detalii[n]" type="text" id="Detaliin"></td>
    </tr>
</table>

Use the indexing on the server side

<?php
$data = array();
foreach ($_POST["DataInterval"] as $index => $value) {
    $data[] = array(
        "DataInterval" => $_POST["DataInterval"][$index],
        "Denumire" => $_POST["Denumire"][$index],
        "Moderatori" => $_POST["Moderatori"][$index],
        "Detalii" => $_POST["Detalii"][$index],
    );
}

After this you only have to iterate throught the $data array and execute paramtrized INSERT SQL commands.

  • do not use old mysql lib
  • learn about PDO and prepared statements
\$\endgroup\$
1
  • \$\begingroup\$ no i don't know the rest... \$\endgroup\$ Sep 9, 2013 at 16:12
0
\$\begingroup\$

I use this function:

        function InsertToTable($table, $data) {
            $total = count($data);

            $sql = "INSERT INTO `$table` (";

            $runs = 0;
                foreach($data as $name => $value) {
                    $runs++;
                    $sql .= "`$name`";

                    if($runs != $total) {
                        $sql .= ',';
                    }
                }


            $sql .= ') VALUES(';

            $runs = 0;
                foreach($data as $name => $value) {
                    $runs++;
                    $sql .= "'$value'";

                    if($runs != $total) {
                        $sql .= ',';
                    }
                }

            $sql .= ')';

            if(mysql_query($sql)) {
                return true;
            } else {
                return false;
            }
        }

And then you have an array with the data, like so:

            // columns to insert (array)
            // like this:
            // 'NAME_OF_FIELD_IN_TABLE' => 'CONTENT (i.e. the $_POST['field'] value)'
            $columnsToInsert = array(
                    $DataInterval1 => $_POST['DataInterval1'],
                    $Denumire1 => $_POST['Denumire1'],
                    $Moderatori1 => $_POST['Moderatori1'],
                );

            if(InsertToTable('tabel', $columnsToInsert)) {
                echo 'Hurray! Success';
            } else {
                echo 'An error happend :(';
            }

Hope it solves your question.

\$\endgroup\$
1
  • \$\begingroup\$ This is in conjunction with what Peter Kiss wrote? \$\endgroup\$ Sep 11, 2013 at 8:38

Your Answer

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

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