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 '>'