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.com", "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($result,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 = '".$usergroupname."', 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 aclusersdoorgroups
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 ='$usergroupname'";
// 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 = '".$usergroupname."', 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 aclusersdoorgroups
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
");
}
?>