KulerGary
KulerGary

Reputation: 217

PHP Form that updates multiple form element to different columns

I am trying to build a simple form that will update my mySql database. I can succeed if I only have 1 form element (input) on the page but I can not figure out how to have more than one form element (inputs) per pgae.

When I add more than one input the database will not add any of the content.

I know that my code is close, but I am at a loss as the exactly where and what to do about it.

P.S. - I am still new to this and am learning ...

Here is what I have

<?php

$host = 'hostName';
$user = 'userName';
$password = 'password';

$link = mysql_connect($host, $user, $password);

$selected = mysql_select_db('dbName', $link);

if(!isset($_POST['text-input']))
{


echo '<html>
        <body>
                <form action="post.php" method="post">
                        <input type="text" name="text-input" id="text-input" value="Update MyDataColumn" style="width:300px;" />
                        <input type="submit" value="Submit" />
                </form>
        </body>
</html>'; }


else {
$form_input =  $_POST['text-input'] ;


mysql_query('INSERT INTO `tableName` (columnName) VALUES ("' . $form_input . '");');

echo '
<html>
        <body>
                <script type="text/javascript">
                        alert(\'Database now contains: <?php echo $form_input ?>. Redirecting...\');
                        window.location  = \'http://url.com\';
                </script>

        </body>
</html>';
}
?>

I would like to sort out how to post to numerous columns within the same db/table.

Ok, from the answers below I have modified the code to look like this:

<?php

                $host = 'dbHost';
                $user = 'dbUser';
                $password = 'dbPassword';

                $link = mysql_connect($host, $user, $password);

                $selected = mysql_select_db('dbName', $link);

                if(!isset($_POST['text-input']))
                {


                echo '
                    <form action="index.php" method="post">
                        <input type="text" name="text-input" id="text-input" value="Update itemName" style="width:300px;" />
                        <input type="text" name="text-input2" id="text-input2" value="Update itemDescription" style="width:300px;" />
                        <input type="text" name="text-input3" id="text-input3" value="Update productID" style="width:300px;" /> 
                        <input type="text" name="text-input4" id="text-input4" value="Update itemPrice" style="width:300px;" />    
                        <input type="submit" value="Submit" />
                    </form>'
                    ; }


                else {
                $form_input =  $_POST['text-input'] ;
                $form_input2 =  $_POST['text-input2'] ;
                $form_input3 =  $_POST['text-input3'] ;
                $form_input4 =  $_POST['text-input4'] ;


               mysql_query('INSERT INTO `items` (itemName, itemDescription, productID, itemPrice)
               VALUES ("' . $form_input . '", "' . $form_input2 . '", "' . $form_input3 . '", "' . $form_input4 . '");

                echo '
                <html>
                        <body>
                                <script type="text/javascript">
                                        alert(\'Database has been updated. Redirecting to previous url.\');
                                        window.location  = \'http://url.com\';
                                </script>

                        </body>
                </html>';
                }
                ?>

What happens with this code is I get a syntax error, unexpected '>'

Upvotes: 1

Views: 2400

Answers (2)

WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

You just have to modify your query to set multiple columns.

HTML:

<form action="post.php" method="post">
    <input type="text" name="text-input" id="text-input" value="Update MyDataColumn" style="width:300px;" />
    <input type="text" name="text-input2" id="text-input2" value="Update MyDataColumn2" style="width:300px;" />
    <input type="text" name="text-input3" id="text-input3" value="Update MyDataColumn3" style="width:300px;" />    
    <input type="submit" value="Submit" />
</form> 

PHP:

$form_input =  $_POST['text-input'] ;
$form_input2 =  $_POST['text-input2'] ;
$form_input3 =  $_POST['text-input3'] ;


mysql_query('INSERT INTO `tableName` (columnName, columnName2, columnName3) VALUES ("' . $form_input . '","' . $form_input2 . '","' . $form_input3 . '");'); 

Upvotes: 1

SDwarfs
SDwarfs

Reputation: 3239

In your HTML output you need to add another field like this one (already in your code):

<input type="text" name="text-input" id="text-input"
value="Update MyDataColumn" style="width:300px;" />

BUT, it must have a different "name" (!);

<input type="text" name="text-input2" id="text-input2"
value="Another Input" style="width:300px;" />

Now we fetch the new variable ("text-input2") out of the $_POST array like this:

$form_input =  $_POST['text-input']; // Your code...
$form_input2 = $_POST['text-input2']; // for the new input field

And we need to change the SQL command to this:

mysql_query('INSERT INTO `tableName` (columnName) VALUES ("' . $form_input . '");
mysql_query('INSERT INTO `tableName` (columnName) VALUES ("' . $form_input2 . '");

The above SQL command would add both form inputs as a seperate row in your table, they are not connected to eachother. To have them in the same row you need to change your table first and add another column to it. The SQL command for that is:

ALTER TABLE tableName ADD columnName2 VARCHAR(255);

This will add another column named 'columName2' to the table. And we finally can add both values as one row of the table:

mysql_query('INSERT INTO `tableName` (columnName, columName2)
VALUES ("' . $form_input . '", "' . $form_input2 . '");

That's it ;-)

Upvotes: 0

Related Questions