Problems from output results from database for updating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Problems from output results from database for updating

Post by ghadacr »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi Guys and Girls,

Right the problem with the following code is that it does not do two things 

1) Does not output any results from the database
2) Radio button does output the all the UserID's

Please some help would greatful or some example code would be better...

Thanking in advance

Code: Select all

<?PHP include 'opendb.php'; ?>
<?

$query="SELECT * FROM Users WHERE UserID='$UserID'";
$result=mssql_query($query);
$num=mssql_numrows($result); 
//mysql_close();

$i=0;
while ($i < $num) {
$UserName=mssql_result($result,$i,"UserName");
$UserInitials=mssql_result($result,$i,"UserInitials");
$UserID=mssql_result($result,$i,"UserID");

?>

<form action="updated.php">
  <table width="75%" border="0">
    <tr> 
      <td width="16%"><strong>Username:</strong></td>
      <td width="22%"><strong>User initials</strong></td>
      <td width="62%">&nbsp;</td>
    </tr>
    <tr> 
      <td><input name="UserName" type="hidden" value=<? echo "$UserName"?></td>
      <td><input name="UserInitials" type="hidden" value=<? echo "$UserInitials"?></td>
      <td><input type="radio" name="UserID" value=<? echo "$UserID"?></td>
    </tr>
    <tr> 
      <td colspan="3"><input type="Submit" value="Update"> <input type="reset" name="Reset" value="Reset"></td>
    </tr>
  </table>
</form>

<?
++$i;
} 
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

why not try

Post by N1gel »

Why not try....

Code: Select all

$query="SELECT * FROM Users WHERE UserID='$UserID'";
$result=mssql_query($query);
$num=mssql_numrows($result);
//mysql_close();

while ($row=mysql_fetch_row($result)) 
{
$UserName   = $row["UserName"];
$UserInitials  = $row["UserInitials"];
$UserID         = $row["UserID"];
I normally use this though

Code: Select all

$UserName   = $row[0];
$UserInitials  = $row[1];
$UserID         = $row[2];
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The script doesn't work without opendb.php, therefore do not use include, use require instead.
$UserID is probably supposed to represent the parameter sent by your form. That's depreacted, use $_GET['UserID'] or $_POST['UserID'] instead and have a read of http://de2.php.net/manual/en/security.globals.php
Your script is prone to sql injections, see http://de.php.net/security.database.sql-injection
mssql_query returns false if something went wrong with the query. You should check that condition.

Code: Select all

$result=mssql_query($query);
if ( false===$result ) {
	echo 'sql error: ', mssql_get_last_message(), "<br />\n",
		'query: ', htmlentities($query);
	exit();
}
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Re: why not try

Post by ghadacr »

Thanks for the reply

Right There is an output on the screen but no results from the database
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

I 'M KNOWING GETTING THE FOLLOWING ERROR:


Notice: Undefined variable: myserver in C:\opendb.php on line 9

Notice: Undefined variable: UserID in C:\UpdateUser.php on line 4

Fatal error: Call to undefined function mssql_numrows() in C:\UpdateUser.php on line 6
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
$a = 1234;

echo $a; <- ok, variable has been set/defined somewhere
echo $x; <- Notice: Undefined variable: x in ....
?>
ghadacr wrote:Fatal error: Call to undefined function mssql_numrows()
Take a look at the list of functions the mssql extension provides at http://de2.php.net/mssql
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Hi Again,

Right I'm started to get somewhere, i edited the code(slightly) as follows: but the problem is know that its outputting the table header twice, any suggestions, and its not outputting all the records from the database.

php:

Code: Select all

<?PHP include 'opendb.php'; ?> 
<?PHP 

$query="SELECT * FROM Users"; 
$result=mssql_query($query); 
$num=mssql_num_fields ($result); 
//mysql_close(); 



?> 

<form action="updated.php"> 


  <table width="75%" border="0"><?PHP $i=0; 
        while ($i < $num) { 

        $UserInitials=mssql_result($result,$i,"UserInitials"); 
        $UserID=mssql_result($result,$i,"UserID"); 
        $UserName=mssql_result($result,$i,"UserName"); ?> 
    <tr> 
      <td width="16%"><strong>Username:</strong></td> 
      <td width="22%"><strong>User initials</strong></td> 
      <td width="62%">&nbsp;</td> 
    </tr> 
    <tr> 

        
      <td><?php echo "$UserName";?></td> 
      <td><?php echo "$UserInitials";?></td> 
      <td><input type=radio name=UserID value=<?php echo "$UserID"; ?><?php 
++$i; 
} 
?> </td> 
    </tr> 
    <tr> 
      <td colspan="3"><input type="Submit" value="Update"> <input type="reset" name="Reset" value="Reset"></td> 
    </tr> 
  </table> 
</form>
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

mssql_num_fields() returns the number of fields in each row in the result, not the number of rows. This is why not all the results are displaying. Perhaps you should read the manual.

Your header is printing multiple times because you put it inside the loop that outputs the results.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Ok, please could you suggest a place where to put it because, i have tried every where and i am just getting different other results. Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why not just do this?

Code: Select all

<?php
require_once 'opendb.php';

$sql = "SELECT * FROM Users";
if (($result = mssql_query($sql)) === false)
{
    die('Could not execute the query ' . $sql);
} 
?>

<form action="updated.php">
  <table width="75%" border="0">
    <tr>
      <td width="16%"><strong>Username:</strong></td>
      <td width="22%"><strong>User initials</strong></td>
      <td width="62%">&nbsp;</td>
    </tr>
<?php
while ($row = mssql_fetch_array($result))
{
    echo '<tr>';
    echo '<td>' . $row['UserName'] . '</td>';
    echo '<td>' . $row['UserInitials'] . '</td>';
    echo '<td><a href="' . basename(__FILE__) . '?userid=' . $row['UserID'] . '">Click to do something with this user</a></td>';
    echo '</tr>';

}
?> 
  </table>
</form>
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Thanks alot mate, that worked really apperciate it. Keep up the good work :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I removed the form elements and completely forgot to remove the <form> tag. Anyway, you get the idea of what you are supposed to do. Glad I was able to help.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Maybe i'm being a pest but could you give me the version you got with the radio buttons, because i'm configuring the script to be used with radio buttons and it does not seem to work. My bad. Sorry. Much apperciated..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, this one time only. After that, you get to try to make it work with the example code given to you.

Code: Select all

<?php
require_once 'opendb.php';

$sql = "SELECT * FROM Users";
if (($result = mssql_query($sql)) === false)
{
    die('Could not execute the query ' . $sql);
}
?>

<form action="updated.php">
  <table width="75%" border="0">
    <tr>
      <td width="16%"><strong>Username:</strong></td>
      <td width="22%"><strong>User initials</strong></td>
      <td width="62%">&nbsp;</td>
    </tr>
<?php
while ($row = mssql_fetch_array($result))
{
    echo '<tr>';
    echo '<td>' . $row['UserName'] . '</td>';
    echo '<td>' . $row['UserInitials'] . '</td>';
    echo '<td><input type="radio" name="UserID" value="' . $row['UserID'] . '" />Click to do something with this user</a></td>';
    echo '</tr>';

}
?>
    <tr>
      <td colspan="3">
        <input type="Submit" value="Update">
        <input type="reset" name="Reset" value="Reset">
      </td>
    </tr>
  </table>
</form>
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Your a legend, and the award goes to Everah, the crowd goes wild. Thanks

I hope this helps other peeps :?:
Post Reply