Solved

Using PHP (example needed), how can I save fields on a form to a database

Posted on 2011-03-14
4
285 Views
Last Modified: 2013-12-13
Can someone please provide me with an example with the following parameters?

I have a database named "SiteConfig" and a table called "configurations".
I have created the following fields in the "configurations" table:

- version
- helpdesk
- shopping_cart
- backup_server
- notes

-I need an example of a PHP page that can connect to a database
-A PHP page with the fields from the database:
  - The fields helpdesk, shopping_cart and backup_server are SELECT fields
  -  The field notes is a TEXT AREA field
  -  The field version is a TEXT BOX

I need this data to be populated in their specified areas when the page is loaded
I need an "update" button that will update the new values to the database
When this page is updated, I need a message "Variables have been updated"
0
Comment
Question by:Computer Guy
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35132237
Not tested. The select fields - you would have to tell what are the options for them

<?php
$link = mysql_connect("yourserver","youruser","yourpass") or die(mysql_error());
mysql_select_db(SiteConfig) or die(mysql_error());

if(!empty($_POST)){
 $version = $_POST[version];
 $notes = $_POST[notes];
 $helpdesk = $_POST[helpdesk];
 $shopping_cart = $_POST[shopping_cart];
 $backup_server = $_POST[backup_server];

 $query = "update configurations set version = '$version', notes = '$notes', helpdesk = '$helpdesk', shopping_cart = '$shopping_cart', backup_server = '$backup_server' ";
 $result = mysql_query($query) or die(mysql_error());

 echo"Record updated !";
}

$query = "select * from configurations";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>

<form action="#" method="post">
<input type="textbox" name="version" value="<?php echo"$row[version]";?>" />
<textarea name="notes"><?php echo"$row[notes]";?></textarea>
<select name="helpdesk">
 -----------------
</select>
<select name="shopping_cart">
----------------
</select>
<select name="backup_server">
------------------
</select>
</form>

Open in new window

0
 
LVL 3

Author Comment

by:Computer Guy
ID: 35132265
Can you please also include an update button
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35132304
Right ! Sorry :) Mind that will not work as it needs the options between SELECTS

<?php
$link = mysql_connect("yourserver","youruser","yourpass") or die(mysql_error());
mysql_select_db(SiteConfig) or die(mysql_error());

if(!empty($_POST)){
 $version = $_POST[version];
 $notes = $_POST[notes];
 $helpdesk = $_POST[helpdesk];
 $shopping_cart = $_POST[shopping_cart];
 $backup_server = $_POST[backup_server];

 $query = "update configurations set version = '$version', notes = '$notes', helpdesk = '$helpdesk', shopping_cart = '$shopping_cart', backup_server = '$backup_server' ";
 $result = mysql_query($query) or die(mysql_error());

 echo"Record updated !";
}

$query = "select * from configurations";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>

<form action="#" method="post">
<input type="textbox" name="version" value="<?php echo"$row[version]";?>" />
<textarea name="notes"><?php echo"$row[notes]";?></textarea>
<select name="helpdesk">
 -----------------
</select>
<select name="shopping_cart">
----------------
</select>
<select name="backup_server">
------------------
</select>
<input type="submit" value="update" />
</form>

Open in new window

0
 
LVL 3

Author Comment

by:Computer Guy
ID: 35151621
Sorry for the cunfusion.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question