Solved

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

Posted on 2011-03-14
4
278 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now