Database error, please help!

Discussion in 'PHP' started by mark103, Sep 26, 2010.

  1. #1
    Hey guys,

    I am using this script to connect to mysql database.

    Here it is the code:

    <?php
        define('DB_HOST', 'localhost');
        define('DB_USER', 'myusername');
        define('DB_PASSWORD', 'mypassword');
        define('DB_DATABASE', 'mydatabase');
    
        session_start();
        $errmsg_arr = array();
        $errflag = false;
    
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
        }
    
        $db = mysql_select_db(DB_DATABASE);
        if(!$db) {
      die("Unable to select database");
        }
    
        function clean($str) {
      $str = @trim($str);
      if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
      }
      return mysql_real_escape_string($str);
      }
        $login = clean($_GET['login']);
        $password = clean($_GET['password']);
        if($login == '') {
      $errmsg_arr[] = 'Login ID missing';
      $errflag = true;
        }
        if($password == '') {
      $errmsg_arr[] = 'PS ID missing';
      $errflag = true;
        }
    
        if($errflag) {
      $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
      session_write_close();
      echo "ERROR";
      exit();
        }
    
      $qry="SELECT level FROM users WHERE login='$login' AND passwd='$password'";
    
        if($result) {
      $result=mysql_query($qry);
        echo $result;
      exit();
        }else {
      die("Query failed");
        }
    ?>
    PHP:

    However, I have received an error which I catch from this line:

    echo "ERROR";
    PHP:
    I have input the correct username, password and the name of the database.


    Do you have any idea why I have received an error, guess if I have missing something?
     
    mark103, Sep 26, 2010 IP
  2. atxsurf

    atxsurf Peon

    Messages:
    2,394
    Likes Received:
    21
    Best Answers:
    1
    Trophy Points:
    0
    #2
    why not to print $errmsg_arr as well not just ERROR?
     
    atxsurf, Sep 26, 2010 IP
  3. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thanks, I have print $errmsg_arr in line 42 and found the problem is coming from line 43:

    Parse error: syntax error, unexpected T_EXIT in /home/myusername/public_html/mysite.com/config.php on line 43


    The error are coming from this line:

    exit();
    PHP:

    Any idea how to correct the error?
     
    mark103, Sep 26, 2010 IP
  4. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    should i remove them or if i have to add on something to get it resolve?

    The exit I call are referring to as none, so should I remove them?
     
    mark103, Sep 26, 2010 IP
  5. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    anyone?????????
     
    mark103, Sep 26, 2010 IP
  6. Media Signal

    Media Signal Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Your code works just fine on my machine - catches if any of the required information is missing and sits silent if everything is ok. PHP misconfiguration ?
     
    Media Signal, Sep 26, 2010 IP
  7. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Well it is not working for me right here. I guess that something is missing out there.

    Here it is the update code:

    
    <?php
        define('DB_HOST', 'localhost');
        define('DB_USER', 'username');
        define('DB_PASSWORD', 'password');
        define('DB_DATABASE', 'mydatabasename');
    
        session_start();
        $errmsg_arr = array();
        $errflag = false;
    
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Failed to connect to server: ' . mysql_error());
    
        mysql_select_db(DB_DATABASE, $link) or  die("Unable to select database");
    
       function clean($str){
      $str = trim($str);
      if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
      }
      $str = strip_tags($str);
      return mysql_real_escape_string($str);
      }
    
        $login = clean($_GET['login']);
        $password = clean($_GET['password']);
        if($login == '') {
      $errmsg_arr[] = 'Login ID missing';
      $errflag = true;
        }
        if($password == '') {
      $errmsg_arr[] = 'PS ID missing';
      $errflag = true;
        }
        if($errflag) {
      $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
      session_write_close();
    
      $qry="SELECT members FROM members WHERE login='$login' AND passwd='$password'";
    
        if($result) {
      $result=mysql_query($qry);
        echo $result;
      exit();
        }else {
      die("Query failed");
    ?>
    
    PHP:
    The problem I have with my updated code are that there are parse error: syntax error, unexpected $end in the last line of the script.

    Here it is the error that it have print out.


    Parse error: syntax error, unexpected $end in /home/myusername/public_html/mysite.com/mysite.php on line 46



    Do you know how to correct this error by an unexpected $end on line 46?


    And also, please could you kindly check out the valid database table that I wish to connect in mysql?


      $qry="SELECT members FROM members WHERE login='$login' AND passwd='$password'";
    PHP:
    The name of the actual table that I wish to connect are called members and there are two columns name that I wish to check on the table are called login and passwd. If you can see anything is invalid then please could you adjust the code of my post and please post it back after when you have correct them.

    Thanks!
     
    mark103, Sep 26, 2010 IP
  8. Media Signal

    Media Signal Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?php
    
    define('DB_HOST', 'localhost');
    define('DB_USER', 'username');
    define('DB_PASSWORD', 'password');
    define('DB_DATABASE', 'mydatabasename');
    
    session_start();
    $errmsg_arr = array();
    $errflag = false;
    
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Failed to connect to server: ' . mysql_error());
    
    mysql_select_db(DB_DATABASE, $link) or  die("Unable to select database");
    
    function clean($str)
    {
    	$str = trim($str);
    	if (get_magic_quotes_gpc()) {
    		$str = stripslashes($str);
    	}
    	$str = strip_tags($str);
    	return mysql_real_escape_string($str);
    }
    
    $login = clean($_GET['login']);
    $password = clean($_GET['password']);
    
    if ($login == '') {
    	$errmsg_arr[] = 'Login ID missing';
    	$errflag = true;
    }
    	
    if ($password == '') {
    	$errmsg_arr[] = 'PS ID missing';
    	$errflag = true;
    }
    
    if($errflag) {
    	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    	session_write_close();
    }
    
    $qry="SELECT * FROM members WHERE login='$login' AND passwd='$password'";
    
    $result = mysql_query($qry);
    
    if (mysql_num_rows($result) == 0) {
    	echo "Invalid login information ..";
    } else {
    	echo "OK - username/password matched!";
    }
    ?>
    PHP:
    Haven't tested the end of this script as I don't have your database but overall it should work now. You forgot to close 2 conditional statements.
     
    Media Signal, Sep 26, 2010 IP
  9. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Thanks for this, I think I am nearly there. I have received an error: Invalid login information

    I have input the valid username, password, database name. What else could be invalid?
     
    mark103, Sep 26, 2010 IP
  10. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #10
    I think I have found a solution right here. I have successfully login into the database by input the username and password in the address bar. Now I needs to write the data in the database table by input the value in the address bar which something would be like this:

    http://www.mysite.com/writemysql.php?user=test&pass=test&firstname=valuedata 
    PHP:
    Here it is code I have created:

    
    <?php
    session_start();
        define('DB_HOST', 'localhost');
        define('DB_USER', 'myusername');
        define('DB_PASSWORD', 'mypassword');
        define('DB_DATABASE', 'mydatabasename');
           
        $errmsg_arr = array();
        $errflag = false;
    
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
        }
    
        $db = mysql_select_db(DB_DATABASE);
        if(!$db) {
      die("Unable to select database");
        }
    
       function clean($var){
    
    return mysql_real_escape_string(strip_tags($var));
        }
      
        $login = clean($_GET['user']);
        $password = clean($_GET['pass']);
        $value = clean($_GET['value']);
        if($login == '') {
      $errmsg_arr[] = 'Login ID missing';
      $errflag = true;
        }
        if($password == '') {
      $errmsg_arr[] = 'PASSWORD ID missing';
      $errflag = true;
        }
        if($value == '') {
      $errmsg_arr[] = 'THE VALUE IS missing';
      $errflag = true;
        }
        if($errflag) {
      $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
      echo implode('<br />',$errmsg_arr);
       }
       else {
      $qry="SELECT * FROM members WHERE login='$login' AND passwd='$password'";
    
      $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error());
    
    	
    if(mysql_num_rows($result) > 0) {
    
      $row = mysql_fetch_row($result);
        echo $row[0];
        }
    else {
    
    
    echo 'Username, Password or value is not found.';
    
    $sql="INSERT INTO members (firstname, lastname)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    
    ?>
    
    Code (markup):
    However, I have received an error:

    Parse error: syntax error, unexpected $end in /home/myusername/public_html/mysite.com/writemysql.php on line 71


    The error are jumping on this line:

    
    ?>
    
    PHP:
    Do you know what the problem is?

    Hope you can help me to get the value update in the database table name members and write the value under the fields id firstname by the same row as for each user.

    Thanks!
     
    mark103, Sep 27, 2010 IP