Jump to content

Problems displaying value from MySQL database in input field


skateme

Recommended Posts

I'm having a lot of trouble displaying a value from a database in one of my input fields. My code is:

 

<?php
session_start();
print "<pre>";print_r($_SESSION);print "</pre>";
?>
<html>
<head>
<title>My Account<title>
</head>
<body>
<?php
$con = mysql_connect("localhost", "admin", "adminpass") or die("Could not connect to database");
$database = mysql_select_db("db") or die("<br />Could not select database");
$username = $_SESSION['username'];
$query=sprintf("select email,location from accountinfo where username='%s'",mysql_real_escape_string($username)); 
$result=mysql_query($query); 
while($row=mysql_fetch_array($result)) 
{ 
$email=$row['email']; 
$location=$row['location']; 
}
?> 
<p>Modify account information:</p>
<form action="modifyaccount.php" method="post">
Username: <input type="text" readonly="readonly" value="<?php echo $username; ?>" /><br />
Password: <input type="password" name="password" id="password" /><br />
Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br />
Location: <input type="text" name="location" id="location" value="<?php echo $location; ?>" /><br />
<input type="submit" value="Modify" />
</form>
</body>
</html>

 

and the error I get is:

 

Parse error: syntax error, unexpected T_VARIABLE in /Library/WebServer/Documents/MDR/myaccount.php on line 27

 

Line 27 is basically the whole while loop.:

 

while($row = mysql_fetch_array($result))  
{  
$email = $row['email'];  
$location = $row['location'];  
}  

 

What am I doing wrong? Thanks in advance!

Link to comment
Share on other sites

is there more than one return?

 

while{

suggests there is more than one return.

If{

would be better if you expect only one return.

 

Also,

 

select email,location from accountinfo where username='%s'

is bad.

 

% is a wildcard, and you have to use like not = but you want a single return, so you need a better query...

 

Finally, just for fun do:

));

$result=mysql_query($query) or die('Croaked: '.mysql_error());

 

and see if the query is valid.

 

Link to comment
Share on other sites

Actually using %s is allowed since the OP is using sprintf().

 

But, as you said, why are using a while loop OP when you would surely only be returning 1 row from the database. Just perform mysql_fetch_array() outside the loop then set the variables.

Link to comment
Share on other sites

Do you mean something like this:

 

$query  = sprintf("select email from userinfo where username='%s' ", mysql_real_escape_string($username)); 
$locquery  = sprintf("select location from userinfo where username='%s' ", mysql_real_escape_string($location)); 
$result = mysql_query($query);
$locresult = mysql_query($locquery);
$email = mysql_fetch_array($result);
$location = mysql_fetch_array($locresult);

while($result=mysql_query($query))
{ 
$email = $row['email']; 
};
while($locresult=mysql_query($locresult))
{
    $location = $row['location']; 
};

 

I get this error when I use the code above:

 

Parse error: syntax error, unexpected T_VARIABLE in /Library/WebServer/Documents/MDR/myaccount.php on line 25

 

Line 25 is the while loop again.

Link to comment
Share on other sites

Try this:

 

$query = sprintf("SELECT email, location FROM userinfo WHERE username='%s'", mysql_real_escape_string($username)); 
$result = mysql_query($query) or die("Error: ".mysql_error());

$data = mysql_fetch_array($result);

$email = $data['email'];
$location = $data['location'];

echo "Email: " . $email . "<br />Location: " . $location;

Link to comment
Share on other sites

Thanks! That worked for the most part. I get an undefined function error:

 

Fatal error: Call to undefined function  sprintf() in /Library/WebServer/Documents/MDR/myaccount.php on line 7

 

My  code:

<?php
session_start(); 
print "<pre>".$_SESSION."</pre>";
$con = mysql_connect("localhost", "admin", "adminpass") or die("Could not connect to database");
$database = mysql_select_db("db") or die("<br />Could not select database");
$username = $_SESSION['username'];
$query  = sprintf("SELECT email, location FROM accountinfo WHERE username = '.$username.'", mysql_real_escape_string($username), $con); 
$result = mysql_query($query); 
$data = mysql_fetch_array($result);

$email = $data['email'];
$location = $data['location'];

?>
<html>
<head>
<title>My Account<title>
</head>
<body>
<p>Modify account information:</p>
<form action="modifyaccount.php" method="post">
Username: <input type="text" readonly="readonly" value="<?php echo $username; ?>" /><br />
Password: <input type="password" name="password" id="password" /><br />
Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br />
Location: <input type="text" name="location" id="location" value="<?php echo $location; ?>" /><br />
<input type="submit" value="Modify" />
</form>



</body>
</html>

Link to comment
Share on other sites

Hm, well try this then:

 

<?php
session_start(); 
echo "<pre>", print_r($_SESSION), "</pre>";

$con = mysql_connect("localhost", "admin", "adminpass") or die("Could not connect to database");
$database = mysql_select_db("db") or die("<br />Could not select database");
$username = $_SESSION['username'];
$query  = "SELECT email, location FROM accountinfo WHERE username = '".mysql_real_escape_string($username)."'";
$result = mysql_query($query) or die(mysql_error());
$data = mysql_fetch_array($result);

$email = $data['email'];
$location = $data['location'];

?>
<html>
<head>
<title>My Account<title>
</head>
<body>
<p>Modify account information:</p>
<form action="modifyaccount.php" method="post">
Username: <input type="text" readonly="readonly" value="<?php echo $username; ?>" /><br />
Password: <input type="password" name="password" id="password" /><br />
Email: <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br />
Location: <input type="text" name="location" id="location" value="<?php echo $location; ?>" /><br />
<input type="submit" value="Modify" />
</form>



</body>
</html>

Link to comment
Share on other sites

I'm still getting another error message on line 7. Line 7 is the $query line:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Library/WebServer/Documents/MDR/myaccount.php on line 7

That error disappears when I remove the quotation marks around SELECT email.., but I get new error saying:

 

Parse error: syntax error, unexpected ',' in /Library/WebServer/Documents/MDR/myaccount.php on line 7

When I remove ",location" from the query, the new error I get is:

 

Parse error: syntax error, unexpected '=' in /Library/WebServer/Documents/MDR/myaccount.php on line 7

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.