Problem of adding records in database

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

  1. #1
    Hey guys,

    I have created a code to add the data to insert in the database, the rows have been added in the table but it have been empty.

    Here it is the code:

    
    <?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));
        }
      $username = clean($_GET['user']);
        $password = clean($_GET['pass']);
        $firstname = clean($_GET['firstname']);  //variable in the url you posted was firstname, not value.
        if($username == '') {
      $errmsg_arr[] = 'Please Enter your Username';
      $errflag = true;
        }
        if($password == '') {
      $errmsg_arr[] = 'Please Enter your Password';
      $errflag = true;
        }
        if($firstname == '') {
      $errmsg_arr[] = 'Please Enter your First name';
      $errflag = true;
        }
        if($errflag) {
      $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
      echo implode('<br />',$errmsg_arr);
       }
       else {
      $qry="SELECT * FROM members WHERE username='$username' AND passwd='$password' AND firstname='$firstname'";
      $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error());
    
    
        if ($qry) {
      $sql="INSERT INTO members (username, passwd, firstname)
    VALUES
    ('$_GET[$username]','$_GET[$password]','$_GET[$firstname]')";
      $result = mysql_query($sql);
      echo "Records added to the database";
      }
     }
    ?>
    
    PHP:


    I cannot be able to find out where the problem have been cause from.

    Please can you help?

    Anyone post to get it solution will be much appreciate.
     
    mark103, Sep 30, 2010 IP
  2. Spawny

    Spawny Well-Known Member

    Messages:
    252
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #2
    can you please post your form code here..
     
    Spawny, Sep 30, 2010 IP
  3. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    I don't have form, I am only insert the data in the url bar. Hope you can be able to find out why the data have filled out as blank in the database table.
     
    mark103, Sep 30, 2010 IP
  4. BungeeBones

    BungeeBones Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Echo your query into the browser window, then copy and paste into sql window of myPHPAdmin on the server. it will tell you your error better

    $qry="SELECT * FROM members WHERE username='$username' AND passwd='$password' AND firstname='$firstname'";
    echo $qry;
    $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error());


    if ($qry) {
    $sql="INSERT INTO members (username, passwd, firstname)
    VALUES
    ('$_GET[$username]','$_GET[$password]','$_GET[$firstname]')";

    echo $sql;
     
    BungeeBones, Oct 1, 2010 IP