Jump to content

Can't pass database value to a variable


bugzy

Recommended Posts

I have a table that has 5 columns

 

player_id

fname

lname

team

email

 

I'm trying to get all the values from that table using this sql command

 

include_once('../database_connection.php');

$player_id = addslashes($_REQUEST['edit_player']);

$sql_query = "Select * from nba_info where player_id = '$player_id'";
$result = MYSQL_QUERY($sql_query);

 

"edit_player" above is came from a different page.

 

 

 

 

They I'm fetching the data using while

 

while($row = mysql_fetch_array($result))
{
	$row['player_id'];
    $row['fname'];
    $row['lname'];
    $row['team'];
    $row['email'];
}

 

Then I'm trying to pass that values to a variable here

 

$id = $row['player_id'];
$fname = $row['fname'];
$fname = $row['lname'];
$lname = $row['team'];
$email = $row['email'];

 

 

When I'm trying to put that variables in a textbox value, they are not showing up

 

Here

 

<table>
	<form name="edit_player_form" method="post" action="">

	<tr>
	<td>Players ID</td>
	<td><input type="text" name="playerid" size="20" value="<?php echo $id; ?>" /></td>
	</tr>

	<tr>
	<td>First Name</td>
	<td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td>
	</tr>

	<tr>
	<td>Last Name</td>
	<td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td>
	</tr>

	<tr>
	<td>Team</td>
	<td><input type="text" name="team" size="20" value="<?php echo $team; ?>" /></td>
	</tr>

	<tr>
	<td>Email</td>
	<td><input type="text" name="email" size="20" value="<?php echo $email; ?>" /></td>
	</tr>


	<tr>
         <td><div align="center">
         <input type="submit" name="Submit" value="Edit This Player">
         </div></td>
        </tr>



	</form>
	</table>

 

 

 

I wonder why it ain't showing up on the textbox value? tried almost everything... Anyone?

Link to comment
Share on other sites

Can you post the whole code without separating it into pieces?

 

 

Hey thanks here

 

 

<?php

include_once('../database_connection.php');


$player_id = addslashes($_REQUEST['edit_player']);



$sql_query = "Select * from nba_info where player_id = '$player_id'";
$result = MYSQL_QUERY($sql_query);


$numberOfRows = MYSQL_NUM_ROWS($result);




while($row = mysql_fetch_array($result))
{
	$row['player_id'];
    $row['fname'];
    $row['lname'];
    $row['team'];
    $row['email'];
}

$id = $row['player_id'];
$fname = $row['fname'];
$fname = $row['lname'];
$lname = $row['team'];
$email = $row['email'];


?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>This Is Where we Edit the Data!</title>
</head>

<body><center><br /><br />
<?php

if($numberOfRows==0)
{

	die('There\'s no Player with that player ID number');
}
?>
	<table>
	<form name="edit_player_form" method="post" action="">

	<tr>
	<td>Players ID</td>
	<td><input type="text" name="playerid" size="20" value="<?php echo $id; ?>" /></td>
	</tr>

	<tr>
	<td>First Name</td>
	<td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td>
	</tr>

	<tr>
	<td>Last Name</td>
	<td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td>
	</tr>

	<tr>
	<td>Team</td>
	<td><input type="text" name="team" size="20" value="<?php echo $team; ?>" /></td>
	</tr>

	<tr>
	<td>Email</td>
	<td><input type="text" name="email" size="20" value="<?php echo $email; ?>" /></td>
	</tr>


	<tr>
         <td><div align="center">
         <input type="submit" name="Submit" value="Edit This Player">
         </div></td>
        </tr>



	</form>
	</table>


<center>
</body>
</html>

Link to comment
Share on other sites

Look at the first example on mysql_query

$result = mysql_query('SELECT * WHERE 1=1');

if (!$result) {

    die('Invalid query: ' . mysql_error());

}

 

 

Add this, do you get an error?

 

 

Hello!

 

I got this

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1=1' at line 1

 

what do you think?

Link to comment
Share on other sites

When using mysql_fetch_array you'll need to set your variables within the array opening and closing brackets.

<?php 
include_once('../database_connection.php');	

$player_id = addslashes($_REQUEST['edit_player']);

$sql_query = "Select * from nba_info where player_id = '$player_id'";
$result = MYSQL_QUERY($sql_query);	
$numberOfRows = MYSQL_NUM_ROWS($result);
while($row = mysql_fetch_array($result))
{
$id = $row['player_id'];
$fname = $row['fname'];
$fname = $row['lname'];
$lname = $row['team'];
$email = $row['email'];
} 
?>

Link to comment
Share on other sites

Well don't use the example query, for one that's not even a valid query, and it's clearly not yours.

 

 

Huh? I don't know what you're talking about.

 

I'm new in php and this is not a project that I'm doing. I'm just practicing base on a sample code from a tutorial video and I encountered this issue and I can't find the solution on it.

 

From an expert like you, I thought the code that you have given would detect why I'm having this issue

Link to comment
Share on other sites

When using mysql_fetch_array you'll need to set your variables within the array opening and closing brackets.

<?php 
include_once('../database_connection.php');	

$player_id = addslashes($_REQUEST['edit_player']);

$sql_query = "Select * from nba_info where player_id = '$player_id'";
$result = MYSQL_QUERY($sql_query);	
$numberOfRows = MYSQL_NUM_ROWS($result);
while($row = mysql_fetch_array($result))
{
$id = $row['player_id'];
$fname = $row['fname'];
$fname = $row['lname'];
$lname = $row['team'];
$email = $row['email'];
} 
?>

 

 

Thanks Drummin

 

This is exactly what I did and it solved the issue :)

 

Thanks again!

Link to comment
Share on other sites

You're probably going to want to use isset() for those variables within the form so you don't have errors when these variables are not set (before passing the form.)

<input type="text" name="playerid" size="20" value="<?php if (isset($id)){ echo "$id";} ?>" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.