the mssql_affected_rows($qry) doesn't seem to work in php to sql server

could someone look at this code and tell me what is wrong: The data is submitted into the db successfully, but I have an if($qry) mssql_affected_rows($qry) check in my code, that will not run.

go down the section that starts:
if ($_REQUEST["txt_submitValue"]  == 'addChar')
<?php
 
echo "hello addchar world";
trim($txt_AddChar);
 
$userID = $txt_AddChar; //this is the userID
echo "<br>userid is:".$userID;
 
//$txt_submitValue ='search';
if (!isset($_REQUEST["txt_submitValue"]))
	{
				if (!$txt_AddChar)
		{
			echo "nothing entered";
			exit;
		}
	echo "<FORM method = 'post' action=".basename($_SERVER['PHP_SELF'])." NAME=frmAddChar>";
	//echo "<input type=text name=txt_submitValue size=20>";       //hidden submit value
	 
	echo "<p><input type=\"text\" name=\"txt_submitValue\" size=\"20\" value=\"addChar\"></p>"; //hidden submit value
	echo "<p><input type=text name=txt_search size=20 value=$userID></p>";                       //hold userID
	echo "<p>Character First Name:  <input type=text name=txt_charFName size=20 value=></p>";    //hold char fname
	echo "<p>Character Last Name:  <input type=text name=txt_charLName size=20 value=></p>";    //hold char lname
	 
	echo "<input type=image src=modifyUser.jpg style=color: #0000FF; font-weight: bold width=153 height=24 onclick=document.frmAddChar.txt_submitValue.value = 'addChar';document.formname.submit();>";
	                
	echo "</form>";
	} 
	else
	{
		// Debug
echo '<pre>';
var_dump($_POST);
echo '</pre>';
 
if ($_REQUEST["txt_submitValue"]  == 'addChar')
	{
                    
		$userID = trim($txt_search);
		echo "<br><br>userID 2 =:".$userID;
		$charFName = trim($txt_charFName);
		$charLName = trim($txt_charLName);
	                    
		$dbname="northwinds";
		$host="localhost";
		$port="1433";
		$user="admin";
		$password="testing";
	            
	    $connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
	    $qry = mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
	    mssql_select_db($dbname, $connection);
	    $num_results = mssql_affected_rows($qry);
	    
	    if ($qry)
		    {
		    	echo $num_results."new character inserted in to database";
		    }
		    else 
		    {
		    	echo "<br><br>userID 2 =:".$userID;
		    	echo "Nothing entered into db";
		    	exit;
		    }	                        
	}       
}              
?>

Open in new window

LVL 1
justmelatAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nplibConnect With a Mentor Commented:
mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection) or die(mssql_error());

try that and see if it returns an error.
0
 
nplibCommented:
mssql_affected_rows()
returns the number of affected rows from the last query,

you don't put anything inside it, just
mssql_affected_rows()
0
 
nplibCommented:
change all your mssql_affected_rows($qry) to mssql_affected_rows()
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
steelseth12Commented:
The correct function is  mssql_rows_affected NOT mssql_affected_rows
http://www.php.net/manual/en/function.mssql-rows-affected.php
0
 
nplibCommented:
oh and your mssql_select_db should be above your query
$qry = mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
            mssql_select_db($dbname, $connection);


should be
mssql_select_db($dbname, $connection);
mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
           
and you can't put an insert statement into a variable, placing queries in a variable is for returning a result, when performing an update or insert you just run the query.
0
 
nplibCommented:
ah yes, the function is mssql_rows_affected, my bad.

how ever, to accomplish what you want change your code to this.
            $num_results = mssql_affected_rows();
           
            if ($num_results >= 1)
                    {
                        echo $num_results."new character inserted in to database";
                    }
                    else
                    {
                        echo "<br><br>userID 2 =:".$userID;
                        echo "Nothing entered into db";
                        exit;
                    }
0
 
justmelatAuthor Commented:
ok, here is what i have now:  see attached.

it still dies here:$num_results = mssql_affected_rows();
data is intered into db though.
$connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
	    mssql_select_db($dbname, $connection);
	    $qry = mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
	    $num_results = mssql_affected_rows();
 
	    
	    if ($num_results >=1)
		    {
		    	echo $num_results."new character inserted in to database";
		    }
		    else 
		    {
		    	echo "<br><br>userID 2 =:".$userID;
		    	echo "Nothing entered into db";
		    	exit;
		    }

Open in new window

0
 
steelseth12Commented:
$num_results = mssql_rows_affected();

NOT

$num_results = mssql_affected_rows();

http://www.php.net/manual/en/function.mssql-rows-affected.php
0
 
nplibCommented:
mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);

you can't place an INSERT or UPDATE statement in a variable

do it like above.
0
 
nplibCommented:
yes, and change that too.
0
 
justmelatAuthor Commented:
ok, now here is hwat i have:

$num_results returns nothing.  I placed an echo statement after it.  Data still goes to db though.
  $connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
	    mssql_select_db($dbname, $connection);
	    mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
	    //$num_results = mssql_affected_rows();
	    $num_results = mssql_rows_affected();
	echo "this is".$num_results;
	    
	    if ($num_results >=1)
		    {
		    	echo $num_results."new character inserted in to database";
		    }
		    else 
		    {
		    	echo "<br><br>userID 2 =:".$userID;
		    	echo "Nothing entered into db";
		    	exit;
		    }
	                        
	}       

Open in new window

0
 
justmelatAuthor Commented:
nope, no error.  this is so weird.  It's not a big deal, except i really need to catch an error should it occur
0
 
nplibCommented:
try this

$num_results = mssql_rows_affected($connection);
0
 
nizsmoDeveloperCommented:
justmelat:

if this still doesn't work for you, there's a simple alternative which will do it in the actual database, a neat function here.

<?php
function update($query){
    mssql_query($query,$connection);
 
    $rsRows = mssql_query("select @@rowcount as rows", $connection);
    $rows = mssql_fetch_assoc($rsRows);
    return $rows['rows'];
}
 
$connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
mssql_select_db($dbname, $connection);
$query = "INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())";
 
$num_results = update($query);
 
if ($num_results >=1)
{
    echo $num_results."new character inserted in to database";
}
else 
{
    echo "<br><br>userID 2 =:".$userID;
    echo "Nothing entered into db";
    exit;
}
?>

Open in new window

0
 
Muhammad WasifCommented:
You must have to pass connection link to the function as nplib suggested.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.