PHP MySQL query not displaying data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jaysonsch

    PHP MySQL query not displaying data

    Hello!

    I am having some problems with a database query that I am trying to do.
    I am trying to develop a way to search a database for an entry and
    then edit the existing values. Upon submit, the new values are updated
    in all corresponding tables (the function of the pages in question).
    However, on the page that does the DB update, I also want to do some
    checks on the data before performing the update.

    Now, the problem that I am running into is that when I don't update the
    primary key field (keyid) the page is running into the section where it
    displays a message that the keyid already exists. This only happens
    when the keyid on the previous page is not changed.

    Upon troubleshooting the problem I found that the very first SELECT
    statement does not seem to be returning any rows despite the fact that
    the statement, when run on the SQL server, returns exactly one row. If
    any could provide some assistance with this matter, I would be most
    appreciative.

    Regards;

    Jayson Schultz

    Code:
    <?php
    session_start() ;
    header("Cache-control: private"); //IE 6 Fix

    //continue if authenticated
    if ($_SESSION['auth'] == 1)
    {

    //get variables from post
    $keyid = $_POST['keyid'];
    $username = $_POST['username'];
    $corpid = $_POST['corpid'];
    $usergroupname = $_POST['usergroupname'];
    $oldkeyid = $_POST['oldkeyid'];
    $oldcorpid = $_POST['oldcorpid'];
    echo("Keyid: $keyid");

    // Connect to MySQL
    mysql_connect ("address.co m", "user", "pass")
    or die ('I cannot connect to the database because: ' .
    mysql_error());
    //select database on server
    mysql_select_db ("seniorproject ");

    //SQL Statement
    $sql = "SELECT keyid, corpid FROM users
    WHERE keyid='".$keyid "'";

    // Execute the query and put results in $result

    $result = mysql_query( $sql )
    or die ( 'Unable to execute query.' );
    echo mysql_result($r esult,0);

    // Count number of matches and print to screen
    $numrows = mysql_numrows( $result );
    if ($numrows == 1)
    { echo(" SQL Query: $sql");
    $result_ar = mysql_fetch_row ($result);
    $result_ar = mysql_fetch_row ($result);
    $dbkeyid = $result_ar['keyid'];
    $dbcorpid = $result_ar['corpid'];
    echo("<br> CorpID: $corpid <br>");
    echo(" DBCorpid: $dbcorpid <br>");
    echo(" DBKeyID: $dbkeyid <br>");
    if($dbcorpid == $oldcorpid)
    {
    //If this condition is true, then KEYID has not changed. Execute
    code

    // Formulate the query

    $sql = "UPDATE users
    SET corpid = '".$corpid." ', username = '".$username."' ,
    usergroup = '".$usergroupna me."', keyid = '".$keyid."'
    WHERE keyid = '".$oldkeyid."' ";



    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );


    // Formulate the query
    $sql = "UPDATE accesslog
    SET keyid = '".$keyid."'
    WHERE keyid = '".$oldkeyid."' ";


    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );

    // Formulate the query
    $sql = "UPDATE aclusersdoors
    SET user = '".$keyid."'
    WHERE user = '".$oldkeyid."' ";


    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );

    // Formulate the query
    $sql = "UPDATE aclusersdoorgro ups
    SET user = '".$keyid."'
    WHERE user = '".$oldkeyid."' ";


    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );


    //DB Modifcaiton Succeeded
    echo("DB Modification Succeeded");


    } //Terminates if from line 40

    else{
    $error = "New KeyID Already Exists";

    die;

    }//Terminates Else

    }//Termiantes If
    else{

    //No Duplicate DoorID Exists... Can Proceed

    //Check for valid door group
    $sql = "SELECT * FROM usergroups
    WHERE usergroup ='$usergroupnam e'";

    // Execute the query and put results in $result

    $result = mysql_query( $sql )
    or die ( 'Unable to execute query.' );

    // Count number of matches and print to screen
    $numrows = mysql_numrows( $result );

    if ($numrows == 1)
    {
    //DoorGroup is valid. Can Proceed.
    // Formulate the query
    echo("KeyID: $keyid");
    $sql = "UPDATE users
    SET corpid = '".$corpid." ', username = '".$username."' ,
    usergroup = '".$usergroupna me."', keyid = '".$keyid."'
    WHERE keyid = '".$oldkeyid."' ";
    echo("Users: $sql");



    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );


    // Formulate the query
    $sql = "UPDATE accesslog
    SET keyid = '".$keyid."'
    WHERE keyid = '".$oldkeyid."' ";


    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error.");

    // Formulate the query
    $sql = "UPDATE aclusersdoors
    SET user = '".$keyid."'
    WHERE user = '".$oldkeyid."' ";


    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );

    // Formulate the query
    $sql = "UPDATE aclusersdoorgro ups
    SET user = '".$keyid."'
    WHERE user = '".$oldkeyid."' ";


    // Execute the query and put results in $result
    $result = mysql_query( $sql )
    or die ( "General Error." );



    echo("DB Modification Succeeded");

    ");
    }
    else{
    $error = "Invalid Door Group";

    die;
    }
    }
    }
    else{
    echo("
    //Display Restricted Area HTML Page
    ");
    }

    ?>

  • Tim Van Wassenhove

    #2
    Re: PHP MySQL query not displaying data

    On 2005-03-25, jaysonsch <jayson@jaysons chultz.com> wrote:[color=blue]
    > Hello!
    >
    > I am having some problems with a database query that I am trying to do.
    > I am trying to develop a way to search a database for an entry and
    > then edit the existing values. Upon submit, the new values are updated
    > in all corresponding tables (the function of the pages in question).
    > However, on the page that does the DB update, I also want to do some
    > checks on the data before performing the update.
    >[/color]
    [snip code]

    Start scripts with:
    ini_set('error_ reporting', E_ALL);
    ini_set('displa y_errors', TRUE);

    Append mysql_error() to your message in the die() function call.

    Use mysql_real_esca pe_string before inputting it blindly in a query.


    --
    Met vriendelijke groeten,
    Tim Van Wassenhove <http://www.timvw.info>

    Comment

    • Jerry Stuckle

      #3
      Re: PHP MySQL query not displaying data



      jaysonsch wrote:[color=blue]
      > Hello![/color]
      <snip>

      Well, right off hand...

      $sql = "SELECT keyid, corpid FROM users WHERE keyid='".$keyid . "'";
      missing^


      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      Working...