escape_data function..

  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

I'm creating a login and I'm having some errors
I added the ecsape_data function in the database connection that I"m work with for Godaddy's server

Here's what i have for my code.. Thanks in advance if somebody can help me.. I got the info from larry ullman php and mysql book..but getting some errors.. As of right now I get this error: Parse error: syntax error, unexpected T_IF in /home/content/p/u/a/puacharm/html/eventcalendar/configproject3.php on line 22


PHP Code: [ Select ]
<?php
//Connect To Database
$hostname='hostname';
$username='user';
$password='pass';
$dbname='dbname';
$usertable='usertable ';
$yourfield = 'login';
 
$dbc = @mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
 
 
//select database
@mysql_select_db(db_name) or die ('could not not select the database: ' . mysql_error());
//creating funtion for escaping the  data
function escape_data ($data) {
 if (ini_get('magic_quotes_gpc')) {
 $data = stripslashes($data)
 
 //check for mysql_real_escape_string () support
 if (function_exists('mysql_real_escape_string')) {
 global $dbc;
 $data = mysql_real_escape_string
 (trim($data), $dbc);
 }else{
 $data = mysql_escape_string
 (trim($data));
 }
 return $data;
 }
 
$query = 'SELECT * FROM $usertable';
$result = mysql_query($query);
if($result) {
    while($row = mysql_fetch_array($result)){
        $name = $row['$yourfield'];
        echo 'Name: '.$name;
    }
}
?>
  1. <?php
  2. //Connect To Database
  3. $hostname='hostname';
  4. $username='user';
  5. $password='pass';
  6. $dbname='dbname';
  7. $usertable='usertable ';
  8. $yourfield = 'login';
  9.  
  10. $dbc = @mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
  11. mysql_select_db($dbname);
  12.  
  13.  
  14. //select database
  15. @mysql_select_db(db_name) or die ('could not not select the database: ' . mysql_error());
  16. //creating funtion for escaping the  data
  17. function escape_data ($data) {
  18.  if (ini_get('magic_quotes_gpc')) {
  19.  $data = stripslashes($data)
  20.  
  21.  //check for mysql_real_escape_string () support
  22.  if (function_exists('mysql_real_escape_string')) {
  23.  global $dbc;
  24.  $data = mysql_real_escape_string
  25.  (trim($data), $dbc);
  26.  }else{
  27.  $data = mysql_escape_string
  28.  (trim($data));
  29.  }
  30.  return $data;
  31.  }
  32.  
  33. $query = 'SELECT * FROM $usertable';
  34. $result = mysql_query($query);
  35. if($result) {
  36.     while($row = mysql_fetch_array($result)){
  37.         $name = $row['$yourfield'];
  38.         echo 'Name: '.$name;
  39.     }
  40. }
  41. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Sometimes "unexpected something on line X" means there is a missing semi-colon at the end of an earlier line.

So since your error messages says the problem was detected at line 22, you'ld start looking for missing semi-colons or other syntax errors at line 21, then 20, and so on until you find something.

:)
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

No since i fixed the problem

it was on line 19. $data = stripslashes($data)

It doesn't connect to the database..

could not not select the database: Access denied for user 'charismaevnts'@'%' to database 'db_name'
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

When you created the database "db_name", how did you do it ?
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

what do you mean??..its godaddy's connection..

the orignal connection was this:

01.<?php
02.//Connect To Database
03.$hostname='hostname';
04.$username='user';
05.$password='pass';
06.$dbname='dbname';
07.$usertable='usertable ';
08.$yourfield = 'login';
09.
10.mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
11.mysql_select_db($dbname);
12.
13.
14.//select database
15 mysql_select_db(db_name) or die ('could not not select the database: ' . mysql_error());

32.
33.$query = 'SELECT * FROM $usertable';
34.$result = mysql_query($query);
35.if($result) {
36. while($row = mysql_fetch_array($result)){
37. $name = $row['$yourfield'];
38. echo 'Name: '.$name;
39. }
40.}
41.?>
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

minus line 15
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Databases don't just automatically exist for you to connect to. You had to have created it first. Since it's Godaddy you likely created the database through their MySQL control panel.

I want to know how you did it so I can be sure you've done it, and, so you'll go back and look over what you did once more and possibly discover something you missed. :)
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

And i still keep getting this error "$errors[] = 'The email address and/or password entered do not match those on " When I type in my email and password I get this error.. when I click submit

this is my database structure
Code: [ Select ]
CREATE TABLE `login` (
 `id` int(1) NOT NULL auto_increment,
 `email` varchar(40) NOT NULL default '',
 `password` varchar(40) NOT NULL default '',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

This is my database connection:

<?php
//Connect To Database
$hostname='host';
$username='user';
$password='pass';
$dbname='charismaevnts';
$usertable='charismaevnts ';
$yourfield = 'login';

$dbc = @mysql_connect($hostname,$username, $password) or die('Unable to connect to database! Please try again later.');
if(!mysql_select_db($dbname)) die('Database missing.');

// function used to escape data
function escape_data($data) {
  global $dbc;
  
  if (ini_get('magic_quotes_gpc')) {
    $data = stripslashes($data);
  }
  
  return mysql_real_escape_string($data, $dbc);
}

$sql = "SELECT * FROM {$usertable}";
$result = mysql_query($sql, $dbc);
if($result) {
  while($row = mysql_fetch_array($result)){
    echo 'Name: '.$row[$yourfield];
  }
}
?>
  1. CREATE TABLE `login` (
  2.  `id` int(1) NOT NULL auto_increment,
  3.  `email` varchar(40) NOT NULL default '',
  4.  `password` varchar(40) NOT NULL default '',
  5.  PRIMARY KEY (`id`)
  6. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
  7. This is my database connection:
  8. <?php
  9. //Connect To Database
  10. $hostname='host';
  11. $username='user';
  12. $password='pass';
  13. $dbname='charismaevnts';
  14. $usertable='charismaevnts ';
  15. $yourfield = 'login';
  16. $dbc = @mysql_connect($hostname,$username, $password) or die('Unable to connect to database! Please try again later.');
  17. if(!mysql_select_db($dbname)) die('Database missing.');
  18. // function used to escape data
  19. function escape_data($data) {
  20.   global $dbc;
  21.   
  22.   if (ini_get('magic_quotes_gpc')) {
  23.     $data = stripslashes($data);
  24.   }
  25.   
  26.   return mysql_real_escape_string($data, $dbc);
  27. }
  28. $sql = "SELECT * FROM {$usertable}";
  29. $result = mysql_query($sql, $dbc);
  30. if($result) {
  31.   while($row = mysql_fetch_array($result)){
  32.     echo 'Name: '.$row[$yourfield];
  33.   }
  34. }
  35. ?>


This is my login page..
Code: [ Select ]
<?php
session_start();

// execute on form submit
if (isset($_POST['submitted'])) {
  $email = '';
  $errors = array();
  $password = '';
  
  // verify input
  if (empty($_POST['email'])) {
    $errors[] = 'You forgot to enter your email address.';
  }
  if (empty($_POST['password'])) {
    $errors[] = 'You forgot to enter your password.';
  }
  
  // validate data against database
  if (empty($errors)) {
    // database connection, handle: $dbc
    require_once ("configproject3.php");
    
    $email = escape_data($_POST['email']);
    $password = sha1($_POST['password']);
    
    // build query
    $sql = "SELECT id FROM login WHERE email = '".$email."' AND password = '".$password."' LIMIT 1;";
    $result = @mysql_query($sql, $dbc);
    
    // close database connection
    @mysql_close($dbc);
    
    // validate dataset
    if (@mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      
      // write user data to session
      $_SESSION['id'] = $row['id'];
      $_SESSION['email'] = $email;
      $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
      
      // set header
      header('Location: '.dirname($_SERVER['PHP_SELF']).'/loggedin3.php');
      exit;
    }
    
    $errors[] = 'The email address and/or password entered do not match those on file.';
  }
}

include ("header2.php");

// handle errors
if (!empty($errors)) { // Print any error messages.
  echo '<h1 id="mainhead">Error!</h1>
      <p class="error">
        The following error(s) occurred:<br />';
  foreach ($errors as $msg) { // Print each error.
    echo " - {$msg}<br />\n";
  }
  echo '  </p>
       <p>Please try again.</p>';
}
?>
<h2><p class="names">Login</p></h2>
<form action="login2.php" method="post">
  <table>
    <tr>
      <th>
        <p class="names">Email Address:</p>
      </th>
      <td>
        <input type="text" name="email" size="20" maxlength="40" />
      </td>
    </tr>
    <tr>
      <th>
        <p class="names">Password:</p>
      </th>
      <td>
        <input type="password" name="password" size="20" maxlength="20" />
      </td>
    </tr>
  </table>
  <p>
    <input type="submit" name="submit" value="Login" />
  </p>
  <input type="hidden" name="submitted" value="TRUE" />
</form>
<div class="footer">
<?php include ("footer.php"); ?>
</div>
  1. <?php
  2. session_start();
  3. // execute on form submit
  4. if (isset($_POST['submitted'])) {
  5.   $email = '';
  6.   $errors = array();
  7.   $password = '';
  8.   
  9.   // verify input
  10.   if (empty($_POST['email'])) {
  11.     $errors[] = 'You forgot to enter your email address.';
  12.   }
  13.   if (empty($_POST['password'])) {
  14.     $errors[] = 'You forgot to enter your password.';
  15.   }
  16.   
  17.   // validate data against database
  18.   if (empty($errors)) {
  19.     // database connection, handle: $dbc
  20.     require_once ("configproject3.php");
  21.     
  22.     $email = escape_data($_POST['email']);
  23.     $password = sha1($_POST['password']);
  24.     
  25.     // build query
  26.     $sql = "SELECT id FROM login WHERE email = '".$email."' AND password = '".$password."' LIMIT 1;";
  27.     $result = @mysql_query($sql, $dbc);
  28.     
  29.     // close database connection
  30.     @mysql_close($dbc);
  31.     
  32.     // validate dataset
  33.     if (@mysql_num_rows($result)) {
  34.       $row = mysql_fetch_assoc($result);
  35.       
  36.       // write user data to session
  37.       $_SESSION['id'] = $row['id'];
  38.       $_SESSION['email'] = $email;
  39.       $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
  40.       
  41.       // set header
  42.       header('Location: '.dirname($_SERVER['PHP_SELF']).'/loggedin3.php');
  43.       exit;
  44.     }
  45.     
  46.     $errors[] = 'The email address and/or password entered do not match those on file.';
  47.   }
  48. }
  49. include ("header2.php");
  50. // handle errors
  51. if (!empty($errors)) { // Print any error messages.
  52.   echo '<h1 id="mainhead">Error!</h1>
  53.       <p class="error">
  54.         The following error(s) occurred:<br />';
  55.   foreach ($errors as $msg) { // Print each error.
  56.     echo " - {$msg}<br />\n";
  57.   }
  58.   echo '  </p>
  59.        <p>Please try again.</p>';
  60. }
  61. ?>
  62. <h2><p class="names">Login</p></h2>
  63. <form action="login2.php" method="post">
  64.   <table>
  65.     <tr>
  66.       <th>
  67.         <p class="names">Email Address:</p>
  68.       </th>
  69.       <td>
  70.         <input type="text" name="email" size="20" maxlength="40" />
  71.       </td>
  72.     </tr>
  73.     <tr>
  74.       <th>
  75.         <p class="names">Password:</p>
  76.       </th>
  77.       <td>
  78.         <input type="password" name="password" size="20" maxlength="20" />
  79.       </td>
  80.     </tr>
  81.   </table>
  82.   <p>
  83.     <input type="submit" name="submit" value="Login" />
  84.   </p>
  85.   <input type="hidden" name="submitted" value="TRUE" />
  86. </form>
  87. <div class="footer">
  88. <?php include ("footer.php"); ?>
  89. </div>
Moderator Remark: Use [code][/code] to encapsulate code segments
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

Here's what my database looks like..
Moderator Remark: Removed screenshot. It displayed a password in plain text
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

With Godaddy, MySQL uses the same name for databases as it does usernames. So, if you create a database called "my_database", the username to connect to that database will also be "my_database".

In everything you've posted so far, the $username and $dbname strings are different. I imagine you might have changed them for security sake, but if those two strings are not the same in your actual code, that's a problem.
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

$username='charismaevnts';
$password='pass';
$dbname='charismaevnts';

Thats whats in my database..


Where do you see in the code???
  • kej0283
  • Novice
  • Novice
  • kej0283
  • Posts: 18

Post 3+ Months Ago

This is competle off topic, I'm running a events calendar that's database driven.. they way it show up now.. it shows the date Time Event name.. and address..but how do you add the instructor'name so, that the name will show individal.. for example..

say you have

Dating Mastery progrom as the event.. instrutor is chris
Dating Program as the event.. instrutor is miles

How do you make them show up like that for each event???

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: Liamw411 and 53 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.