Page 1 of 2
Problems from output results from database for updating
Posted: Wed May 16, 2007 8:46 am
by ghadacr
feyd | Please use 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%"> </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
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]
why not try
Posted: Wed May 16, 2007 9:01 am
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];
Posted: Wed May 16, 2007 9:02 am
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();
}
Re: why not try
Posted: Wed May 16, 2007 9:05 am
by ghadacr
Thanks for the reply
Right There is an output on the screen but no results from the database
Posted: Wed May 16, 2007 9:18 am
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
Posted: Wed May 16, 2007 9:55 am
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
Posted: Wed May 16, 2007 11:59 am
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%"> </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>
Posted: Wed May 16, 2007 12:06 pm
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.
Posted: Wed May 16, 2007 2:58 pm
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
Posted: Wed May 16, 2007 4:13 pm
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%"> </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>
Posted: Wed May 16, 2007 4:55 pm
by ghadacr
Thanks alot mate, that worked really apperciate it. Keep up the good work

Posted: Wed May 16, 2007 4:56 pm
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.
Posted: Wed May 16, 2007 5:05 pm
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..
Posted: Wed May 16, 2007 5:22 pm
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%"> </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>
Posted: Wed May 16, 2007 5:30 pm
by ghadacr
Your a legend, and the award goes to
Everah, the crowd goes wild. Thanks
I hope this helps other peeps
