Jump to content

Select, Update and Display


VicHost

Recommended Posts

Guys, you are so going to be sick of me. But I really appreciate the help I am receiving here thus far.

 

I have the code below sending the data from the form to the database. That's excellent. However, where I am stuck is doing the following two:

 

1. Updating currently inserted data

2. Displaying the data on the site.

 

Here is settings.php:

<?php
include "../settings.php";
include "../lang/english.php";
include "../config/config.php";
$errors = array();
// condition and map inputs
$fields = array('site_name'=>'sitename','site_email'=>'email','your_name'=>'name','meta_description'=>'meta-description','meta_keywords'=>'meta-keywords');
foreach($fields as $var => $field){
   $$var = isset($_GET[$field]) ? trim($_GET[$field]) : '';
}
// check if the form was submitted
if(isset($_GET['submit'])){
   // connect to/select database
   mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error());
   mysql_select_db("$db_database") or die(mysql_error());
   
   // basic validation (not empty) and escape data
   foreach($fields as $var => $field){
      if(empty($$var)){
         $errors[] = "The form field: $field, is empty!";
      }
      // escape the data
      $$var = mysql_real_escape_string($$var);
   }
   // if no validation errors, insert the data
   if(empty($errors)){
      $insert = sprintf("INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('%s','%s','%s','%s','%s')",
      $site_name,
      $meta_description,
      $meta_keywords,
      $site_email,
      $your_name
      );
      if(!mysql_query($insert)){
         // a query error occurred
         // check if due to duplicate data
         if(mysql_errno() == 1062){
            // 1062 = duplicate primary key error (your error number might be different depending on your table definition)
            $errors[] = "The site name: $site_name, already exists and cannot be inserted!";
         } else {
            // all other query errors -
            $errors[] = "A database error occurred and your query cannot be processed!";
            trigger_error(mysql_error()); // use error_reporting/display_errors/log_errors to display/log the error condition
         }
      } else {
         echo "The data was successfully inserted!";
      }
   }
}
	//database_connect();
				$sql = "UPDATE settings
						SET site_name='$site_name', description='$description', keywords='$keywords', email='$site_email', name='$your_name'
						WHERE id='$id'"; 


		$query = mysql_query($sql)or die("There's a problem with the query: ". mysql_error()); 	
		if($query) echo "<br>The settings have been updated.<br>";
		$_SESSION['tekst']="";
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $site_name; ?> :: :: Powered by osPHPSite</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <link  href="css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
        <div id="header">
            <h1><?php echo $sitename; ?></h1>
            <ul id="top-navigation">
                    <li><a href="index.php"><?php echo $lang_button_index; ?></a></li>
                    <li><a href="settings.php" class="active"><?php echo $lang_button_settings; ?></a></li>
                    <li><a href="pages.php"><?php echo $lang_button_pages; ?></a></li>
                    <li><a href="gallery.php"><?php echo $lang_button_gallery; ?></a></li>
                    <li><a href="/"><?php echo $lang_button_viewsite; ?></a></li>
                    <li><a href="logout.php"><?php echo $lang_button_logout; ?></a></li>
            </ul>
        </div>
        <div id="middle">
            <div id="left-column">
                <h3><?php echo $eng_navigation; ?></h3>
                <ul class="nav">
                    <li><a href="index.php"><?php echo $lang_button_index; ?></a></li>
                    <li><a href="settings.php"><?php echo $lang_button_settings; ?></a></li>
                    <li><a href="pages.php"><?php echo $lang_button_pages; ?></a></li>
                    <li><a href="gallery.php"><?php echo $lang_button_gallery; ?></a></li>
                    <li><a href="/"><?php echo $lang_button_viewsite; ?></a></li>
                    <li><a href="logout.php"><?php echo $lang_button_logout; ?></a></li>
                </ul>
                <a href="http://www.osphpsite.com" target="_blank" class="link">osPHPSite</a>
                <a href="http://www.osphpsite.com/forums/" target="_blank" class="link">Support Forums</a>
                <a href="http://www.vichost.com" target="_blank" class="link">VicHost.Com</a>
            </div>
            <div id="center-column">
                <div class="table">
            <?php
               if(!empty($errors)){
                  echo 'The following errors occurred:<br />';
                  foreach($errors as $error){
                     echo "$error<br />";
                  }
               }
            ?>
                    <form action="" id="settings" name="settings">
                    <table class="listing form" cellpadding="0" cellspacing="0">
                        <tr>
                            <th class="full" colspan="2">Site Configuration</th>
                        </tr>
                        <tr>
                            <th colspan="2">From the options below, define the default settings for your website.</th>
                        </tr>
                        <tr>
                            <th colspan="2"></th>
                        </tr>
                        <tr>
                           <td>Site name: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td>
                        </tr>
                        <tr>
                           <td>Email: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td>
                        </tr>
                        <tr>
                           <td>Name: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td>
                        </tr>
                        <tr>
                           <td>Meta Description: </td> <td><input type="text" name="meta-description" value="<?php echo $meta_description; ?>" width="172" /> <em>SEO</em></td>
                        </tr>
                        <tr>
                           <td>Meta Keywords: </td> <td><input type="text" name="meta-keywords" value="<?php echo $meta_keywords; ?>" width="172" /> <em>Separate with Commas</em></td>
                        </tr>
                       <tr>
                       <td><input type="submit" class="button" name="submit" value="Submit"></td>
                       </tr>
                    </table>
      </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

This will be a huge hurdle if I can get passed this one, I am well and truly on my way.

Any help, advice etc that you can give, and what needs to go where, I would really appreciate it and I promise not to annoy you guys unless absolutely necessary.

 

Link to comment
Share on other sites

you already have an update query in the code...?

	
$sql = "UPDATE settings
SET site_name='$site_name', description='$description', keywords='$keywords', email='$site_email', name='$your_name'
WHERE id='$id'"; 

 

 

have a look at some mysql/php tutorials. You'll need to do a select query and then echo the results.

start here

Link to comment
Share on other sites

What you are attempting to do is to write a script to Create, Read, Update, and Delete (CRUD) database information. There are countless php scripts/classes and tutorials posted all over the Internet that you can either use or look at to learn how you can do this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.