Jump to content

lenstanbera

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

lenstanbera's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. glad you've sorted the problem pal. I'm in Scotland not far from u; ;-P
  2. hi Pikachu2000, thanks for sharing your knowledge, and sparing time solving the script errors with us mate. I'll test the code and will post findings.
  3. sorry guyz, struggling a lot with this code. I pasted pikachu2000 code, jut updated database connection details, and I get following error: Notice: Undefined variable: redisplay in C:\xampp\xampp\htdocs\sql site\formphp3.php on line 70 ___________________________ any help will be appreciated
  4. hi, I've made some changes to previous code: <?php //defining variables $error_quote=NULL; $error_author=NULL; $error_date=NULL; $error_url=NULL; $error_flag = true; $genre=NULL; $quote=NULL; $author=NULL; $date=NULL; $url=NULL; $genre=NULL; $redisplay = TRUE; if (isset($_POST['submit'])) { array_map('trim', $_POST); // trim() the entire $_POST array at once. if ( !isset($_POST['quote']) || strlen($_POST['quote']) < 2 || strlen($_POST['quote']) > 16 ) { // if form field 'quote' value not set, or too short or too long, error $error_quote="Please enter a <b>Quote</b> between 2 to 16 characters long <br/>"; $error_flag = true; } else { // otherwise, assign value of form field 'quote' to the $quote variable. $quote = $_POST['quote']; } if( !isset($_POST['author']) || strlen($_POST['author']) < 2 || strlen($_POST['author']) > 16) { // this is same as above $error_author="Please enter an <b>Author</b> between 2 to 16 characters long <br/>"; $error_flag = true; } else { $author = $_POST['author']; } if( !isset($_POST['q_date']) || strlen($_POST['q_date']) < 2 || strlen($_POST['q_date']) > 24) { // same as above $error_date="Please enter a <b>Date</b> between 2 to 24 characters long <br/>"; $error_flag = true; } else { $q_date = $_POST['q_date']; } if( !isset($_POST['url']) || strlen($_POST['url']) < 2 || strlen($$_POST['url']) > 246) { // same as above $error_url="Please enter a <b>URL</b> between 2 to 246 characters long <br/>"; $error_flag = true; } else { $url = $_POST['url']; } if( !isset($_POST['genre']) || !in_array($_POST['genre'], array( 'humour', 'politics', 'romance')) ) { // if form field 'genre' not set, or isn't one of the only three permitted values, error $error_genre=" - Please select a <b>Genre</b>. <br/>"; $error_flag = true; } else { // else assign value to variable $genre = $_POST['genre']; } if ($error_flag !== true) { // If there are no validation errors, run the DB query. $dbhost = 'localhost'; $dbuser = 'lenstanbera'; $dbpass = '27yriloba'; $dbname = 'anevins'; // make a connection to the database $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Connection failed: '. mysql_error()); // select the database mysql_select_db($dbname) OR die('Database select failed: '. mysql_error()); // set up the query to insert the new data $query = "INSERT INTO quotes (id, quote, author, q_date, url, genre) VALUES ('', '$quote', '$author', '$q_date', '$url', '$genre')"; $result = mysql_query($query) OR die('Query failed: ' . mysql_error()); if( mysql_affected_rows() > 0 ) { // check to see if a row was actually inserted. echo "<p>Thank you for adding your quote</p>"; $redisplay = FALSE; // if a record was inserted, set $redisplay = FALSE to prevent form redisplay } else { echo "Sorry, your quote was not added to the database."; } } } if( $redisplay !== FALSE ) { // Only redisplay form if $redisplay has not been set to FALSE ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form to add quotes</title> </head> <body> </body> <form method="post" action=""> <!-- Using echo $_SERVER['PHP_SELF'] is a known XSS vulnerability. Use action="" instead --> <table> <tr><td>Quote</td><td><input type="text" name="quote" value="<?php if (isset($quote)) echo $quote ; ?>"></td><td><?php echo $error_quote; ?></td></tr> <tr><td>Author</td><td><input type="text" name="author" value="<?php if (isset($author)) echo $author ; ?>"></td><td><?php echo $error_author;?></td></tr> <tr><td>Date</td><td><input type="text" name="q_date" value="<?php if (isset($q_date)) echo $q_date ; ?>"></td><td><?php echo $error_date;?></td></tr> <tr><td>URL</td><td><input type="text" name="url" value="<?php if (isset($url)) echo $url ; ?>"></td><td><?php echo $error_url;?></td></tr> </table> <p>Genre:</p> <p> <input type="radio" name="genre" value="humour" <?php if (isset($genre) AND $genre=="humour") echo $genre; ?> />Humour<br /> <input type="radio" name="genre" value="politics" <?php if (isset($genre) AND $genre=="politics")echo $genre; ?> />Politics<br /> <input type="radio" name="genre" value = "romance" <?php if (isset($genre) AND $genre=="romance") echo $genre; ?>/>Romance<br /> </p> <p><input type = "submit" name="submit" value = "add quote" /></p> </form> </body> </html> <?php } ?> ____________________________ and get tis error: Notice: Undefined variable: www.example.com in C:\xampp\xampp\htdocs\sql site\formphp2.php on line 41
  5. hello. I took pikachu2000 code and tested. Got problems with undefined variables. I've changed the code to: <?php //defining variables $error_quote=NULL; $error_author=NULL; $error_date=NULL; $error_url=NULL; $genre=NULL; $quote=NULL; $author=NULL; $date=NULL; $url=NULL; $genre=NULL; $redisplay = TRUE; if (isset($_POST['submit'])) { array_map('trim', $_POST); // trim() the entire $_POST array at once. if ( !isset($_POST['quote']) || strlen($_POST['quote']) < 2 || strlen($_POST['quote']) > 16 ) { // if form field 'quote' value not set, or too short or too long, error $error_quote="Please enter a <b>Quote</b> between 2 to 16 characters long <br/>"; $error_flag = true; } else { // otherwise, assign value of form field 'quote' to the $quote variable. $quote = $_POST['quote']; } if( !isset($_POST['author']) || strlen($_POST['author']) < 2 || strlen($_POST['author']) > 16) { // this is same as above $error_author="Please enter an <b>Author</b> between 2 to 16 characters long <br/>"; $error_flag = true; } else { $author = $_POST['author']; } if( !isset($_POST['q_date']) || strlen($_POST['q_date']) < 2 || strlen($_POST['q_date']) > 24) { // same as above $error_date="Please enter a <b>Date</b> between 2 to 24 characters long <br/>"; $error_flag = true; } else { $q_date = $_POST['q_date']; } if( !isset($_POST['url']) || strlen($_POST['url']) < 2 || strlen($$_POST['url']) > 246) { // same as above $error_url="Please enter a <b>URL</b> between 2 to 246 characters long <br/>"; $error_flag = true; } else { $url = $_POST['url']; } if( !isset($_POST['genre']) || !in_array($_POST['genre'], array( 'humour', 'politics', 'romance')) ) { // if form field 'genre' not set, or isn't one of the only three permitted values, error $error_genre=" - Please select a <b>Genre</b>. <br/>"; $error_flag = true; } else { // else assign value to variable $genre = $_POST['genre']; } if ($error_flag !== true) { // If there are no validation errors, run the DB query. $dbhost = 'localhost'; $dbuser = 'lenstanbera'; $dbpass = '27yriloba'; $dbname = 'anevins'; // make a connection to the database $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Connection failed: '. mysql_error()); // select the database mysql_select_db($dbname) OR die('Database select failed: '. mysql_error()); // set up the query to insert the new data $query = "INSERT INTO quotes (id, quote, author, q_date, url, genre) VALUES ('', '$quote', '$author', '$q_date', '$url', '$genre')"; $result = mysql_query($query) OR die('Query failed: ' . mysql_error()); if( mysql_affected_rows() > 0 ) { // check to see if a row was actually inserted. echo "<p>Thank you for adding your quote</p>"; $redisplay = FALSE; // if a record was inserted, set $redisplay = FALSE to prevent form redisplay } else { echo "Sorry, your quote was not added to the database."; } } } if( $redisplay !== FALSE ) { // Only redisplay form if $redisplay has not been set to FALSE ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Form to add quotes</title> </head> <body> <form method="post" action=""> <!-- Using echo $_SERVER['PHP_SELF'] is a known XSS vulnerability. Use action="" instead --> <table> <tr><td>Quote</td><td><input type="text" name="quote" value="<?php if (isset($quote)) echo $quote ; ?>"></td><td><?php echo $error_quote; ?></td></tr> <tr><td>Author</td><td><input type="text" name="author" value="<?php if (isset($author)) echo $author ; ?>"></td><td><?php echo $error_author;?></td></tr> <tr><td>Date</td><td><input type="text" name="q_date" value="<?php if (isset($q_date)) echo $q_date ; ?>"></td><td><?php echo $error_date;?></td></tr> <tr><td>URL</td><td><input type="text" name="url" value="<?php if (isset($url)) echo $url ; ?>"></td><td><?php echo $error_url;?></td></tr> </table> <p>Genre:</p> <p> <input type="radio" name="genre" value="humour" <?php if (isset($genre) AND $genre=="humour") echo $genre; ?> />Humour<br /> <input type="radio" name="genre" value="politics" <?php if (isset($genre) AND $genre=="politics")echo $genre; ?> />Politics<br /> <input type="radio" name="genre" value = "romance" <?php if (isset($genre) AND $genre=="romance") echo $genre; ?>/>Romance<br /> </p> <p><input type = "submit" name="submit" value = "add quote" /></p> </form> </body> </html> <?php } ?> but now I get following message when I click submit: Notice: Undefined variable: www.example.com in C:\xampp\xampp\htdocs\sql site\formphp2.php on line 40 Notice: Undefined variable: error_flag in C:\xampp\xampp\htdocs\sql site\formphp2.php on line 53 Thank you for adding your quote _________________________________ any suggestions?
  6. I've pasted your code, created database and table accordingly, and when I run it, this is what it gave me: see attachment [attachment deleted by admin]
  7. I'm new to PHP but done some C++, but as much as I see they are very similar. when you assign value to variable, it's stored in memory until it's unset "PHP". you using the same variable $timestamp in stop script as in start, statement: $timestamp = date('Y-m-d H:i:s'); in stop script gives $timestamp date-time value of time stop script is executed, hence reassigning new date-time value. In stop script, replace $timestamp variable with something different eg: $stop_timestamp or anything you fancy, so stop script should look like this: <?php date_default_timezone_set('Europe/Brussels'); $stop_timestamp = date('Y-m-d H:i:s'); $conn = mysql_connect('server', 'user', 'pw') or die(mysql_error()); mysql_select_db('hystudi_timesheets', $conn) or die(mysql_error()); $records = mysql_query("SELECT id, t_start_timestamp FROM timesheet ORDER BY id DESC LIMIT 1"); while ($row = mysql_fetch_array($records)) { $id = $row['id']; $start_timestamp = date("U", strtotime($row['t_start_timestamp'])); } $duration = (date("U", strtotime($stop_timestamp)) - $start_timestamp) / 3600; // duration mysql_query("UPDATE timesheet SET t_end_timestamp = '$stop_timestamp', t_calculated_time = '".round($duration,2)."' WHERE id = $id", $conn) or die(mysql_error()); ?> As I've mentioned I'm new to PHP, so hopefully this solution helps. If not, hopefully somebody else will come up with it.
×
×
  • 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.