469,322 Members | 1,614 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,322 developers. It's quick & easy.

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.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
2 3067
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


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.

Similar topics

reply views Thread by Philip Stoev | last post: by
reply views Thread by Mike Chirico | last post: by
6 posts views Thread by jfizer | last post: by
10 posts views Thread by Lloyd Harold | last post: by
4 posts views Thread by foss | last post: by
4 posts views Thread by bodhiSoma | last post: by
9 posts views Thread by PI | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.