Jump to content

VicHost

Members
  • Posts

    40
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

VicHost's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I was hoping someone could help me a little further with this. The above code works perfectly for inserting data. However, where I am seriously stuck, I need to be able to update the inserted data from the form and have it displayed both on the site and in the form fields. I genually have tried to follow tutorials guys and get errors all the time. If I can get past this little bit of a truff, it will be half the battle. I hope someone can help.
  2. Hi Joel, Thanks mate. Yeah that was one of my attempts from a tutorial on w3schools
  3. 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.
  4. Mate, you are a legend. Thank you!
  5. Sorry mate. I am completely lost. Very sorry.
  6. hehe Got this: Query string: INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('', '', '', '', '') Produced error: Duplicate entry '' for key 1 Like, WTF?
  7. Sure mate. Here is my settings.php page: <?php include "../lang/english.php"; include "../config/config.php"; mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error()); mysql_select_db("$db_database") or die(mysql_error()); $insert = "INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('$sitename', '$meta_description', '$meta_keywords', '$site_email', '$your_name')"; mysql_query($insert) or die(mysql_error()); ?> <!DOCTYPE html> <html> <head> <title><?php echo $sitename; ?> :: <?php echo $eng_cp; ?> :: 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"> <form action="" id="settings" name="settings"> <table class="listing form" cellpadding="0" cellspacing="0"> <tr> <th class="full" colspan="2"><?php echo $lang_settings; ?></th> </tr> <tr> <th colspan="2"><?php echo $lang_settings_description; ?></th> </tr> <tr> <td><?php echo $lang_sitename; ?>: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td> </tr> <tr> <td><?php echo $lang_email; ?>: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td> </tr> <tr> <td><?php echo $lang_yourname; ?>: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td> </tr> <tr> <td><?php echo $lang_meta_description; ?>: </td> <td><input type="text" name="meta-description" value="<?php echo $description; ?>" width="172" /> <em>SEO</em></td> </tr> <tr> <td><?php echo $lang_keywords; ?>: </td> <td><input type="text" name="meta-keywords" value="<?php echo $keywords; ?>" width="172" /> <em>Separate with Commas</em></td> </tr> <tr> <td><input type="submit" class="button" name="submit" value="<?php echo $lang_button_savesettings; ?>"></td> </tr> </table> </form> </div> </div> <div id="right-column"> <strong class="h"><?php echo $eng_version; ?></strong> <div class="box">Current Stable Release:<h3><?php echo $eng_version; ?></h3></div> </div> </div> <div id="footer"><p><?php echo $eng_poweredby; ?><br/>Copyright © 2010 <a href="http://www.vichost.com" target="_blank">VicHost.Com</a>. All Rights Reserved.</p></div> </div> </body> </html> Not sure if that's all you need. I also notice when I login to phpmyadmin after submitting the data that a second empty row appears. I am new to this guys so I am unable to figure out why this is happening. Maybe I should be using an update query rather than an insert? I dunno.
  8. Yeah but I can't figure out where.
  9. Thanks for pointing that out mate. Yeah was just a typo. Has no impact really on the form because I am not sending the data to another file.
  10. Hey folks, Sorry for being a pain in the ass. I am trying to submit data to my database via a form and when I click Submit, I get: Duplicate entry '' for key 1 I understand that it means I have a duplicate entry with the ID of 1 or something like that. I can't find where the issue is. Here is the form: <form actin="" id="settings" name="settings"> <table class="listing form" cellpadding="0" cellspacing="0"> <tr> <th class="full" colspan="2"><?php echo $lang_settings; ?></th> </tr> <tr> <th colspan="2"><?php echo $lang_settings_description; ?></th> </tr> <tr> <td><?php echo $lang_sitename; ?>: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td> </tr> <tr> <td><?php echo $lang_email; ?>: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td> </tr> <tr> <td><?php echo $lang_yourname; ?>: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td> </tr> <tr> <td><?php echo $lang_meta_description; ?>: </td> <td><input type="text" name="meta-description" value="<?php echo $description; ?>" width="172" /> <em>SEO</em></td> </tr> <tr> <td><?php echo $lang_keywords; ?>: </td> <td><input type="text" name="meta-keywords" value="<?php echo $keywords; ?>" width="172" /> <em>Separate with Commas</em></td> </tr> <tr> <td><input type="submit" class="button" name="submit" value="<?php echo $lang_button_savesettings; ?>"></td> </tr> </table> </form> Here is the Insert code: $insert = "INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('$sitename', '$meta_description', '$meta_keywords', '$site_email', '$your_name')"; mysql_query($insert) or die(mysql_error()); Can anyone please tell me where I am going wrong here? Much appreciated.
  11. Thanks mate. I have never done this before mate. I tried to add it: $query = "SELECT * FROM users WHERE username = '$username' AND password = 'MD5('$password')'"; But getting this error now: Unable to verify user because : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password removed')'' at line 1
  12. Ok, got it working. It logs me in etc so that's awesome. Now, two other issues have arisen: 1. It will not let me in if I change the password in phpmyadmin to md5 encryption login.php: <? //always start the session before anything else!!!!!! session_start(); if(isset($_POST['username']) && $_POST['password']){ $username = $_POST['username']; //name of the text field for usernames $password = $_POST['password']; //likewise here just for the password //connect to the db include "../config/config.php"; mysql_connect("$db_hostname", "$db_username", "$db_password") or die(mysql_error()); mysql_select_db("$db_database") or die(mysql_error()); //run the query to search for the username and password the match $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); //this is where the actual verification happens if(mysql_num_rows($result) == 1){ //the username and password match //so set true to yes $true=yes; //and then move them to the index page or the page to which they need to go header('Location: index.php?confirm=$true'); }else{ $err = 'Incorrect username / password.' ; } //then just above your login form or where ever you want the error to be displayed you just put in echo "$err"; } echo "<html>"; echo "<head>"; echo "</head>"; echo "<body>"; echo "<form action=\"\" method=\"POST\">"; echo "<p>username:"; echo "<input name=\"username\" size=\"13\" />"; echo "</p>"; echo "<p>password:"; echo "<input type=\"password\" name=\"password\" size=\"13\" />"; echo "</p>"; echo "<input type=\"submit\" name=\"login\" value=\"Login\" />"; echo "</form>"; echo "</body>"; echo "</html>"; ?> 2. If I have the following in the beginning of my index.php I am redirected back to the login page, rather than logged in to the admin cp. if(!isset($_SESSION) || $_SESSION !== true){ header('Location: login.php'); } echo "You are currently logged in"; Without it, I can login fine. Any ideas?
  13. It was obviously a disaster of a tutorial.
  14. I put in what you provided mate and I just get: Parse error: syntax error, unexpected $end in /home/danoid/public_html/admin/login.php on line 61
×
×
  • 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.