By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,222 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,222 IT Pros & Developers. It's quick & easy.

PHP MySQL query not displaying data

P: n/a
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
");
}

?>

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2005-03-25, jaysonsch <ja****@jaysonschultz.com> wrote:
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.

[snip code]

Start scripts with:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

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

Use mysql_real_escape_string before inputting it blindly in a query.
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #2

P: n/a


jaysonsch wrote:
Hello!

<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.
js*******@attglobal.net
==================
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.