text area loading from mysql database

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

warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

text area loading from mysql database

Post by warzo69 »

can anyone find the error in this code?

Code: Select all

         <select name="select1" id="select1" onChange='empsel()'/> q
<?php
$result = mysql_query("SELECT fullname FROM employee");
while ($row = mysql_fetch_array($result)) {
    echo "<option>".$row['fullname']."</option><br>";
};
?>
            <option>New Employee</option>
          </select>
          <br/>
          <br/>
          <br/>
          <br/>
 
          <textarea name="select2" rows="5" cols="15" id="select2"/>
<?php
$tmpVar = $_POST['select1'];
$result = mysql_query("SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = '$tmpVar' ");
while ($row = mysql_fetch_array($result)) {
    echo "".$row['ssn']."|"."".$row['dob']."|"."".$row['add1']."|"."".$row['add2']."|".
         "".$row['phone']."|"."".$row['empid']."|"."".$row['position']."|"."".$row['hiredate']."|".
         "".$row['termdate']."|"."".$row['status']."|"."".$row['salary']."|"."".$row['driverslicense']."|";
};
 
?>
          </textarea>
The textarea is loading with nothing in it. If I change FROM employee WHERE fullname = '$tmpVar' "); to FROM employee WHERE fullname = 'John Smith' ");
It loads john smith's information from the database into the textarea like it should.

Any ideas?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: text area loading from mysql database

Post by alex.barylski »

Change:

[sql]FROM employee WHERE fullname = '$tmpVar' "; [/sql]

To:

[sql]FROM employee WHERE fullname = "$tmpVar"; [/sql]

And use single quotes for the SQL query itself.
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

tried

Code: Select all

$result = mysql_query('SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = "$tmpVar"; ');
and

Code: Select all

$result = mysql_query('SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = "$tmpVar" ');
both gave me the same result, a blank textarea. If i manually put in a name like 'John Smith' the employee info is loaded from the database, so the syntax has to be correct. i dont have a clue why this isnt working.

Code: Select all

$result = mysql_query('SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = "John Smith" ');
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: text area loading from mysql database

Post by aceconcepts »

Try:

Code: Select all

 
$result = mysql_query("SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = '$tmpVar' ");
 
Change the single quotes to double quotes and vice versa.

Also, you had a semi-colon immediately after WHERE fullname = "$tmpVar";');

The query i have re-written at the top is one correct method.
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

Try:

Line number On/Off | Expand/Contract

1.
2. $result = mysql_query("SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = '$tmpVar' ");
3.



Change the single quotes to double quotes and vice versa.

Also, you had a semi-colon immediately after WHERE fullname = "$tmpVar";');

The query i have re-written at the top is one correct method.

_________________
I have never let my schooling interfere with my education.
- Mark Twain
this is where i started at the beginning of the post. however I did try it again and it yielded an empty textarea.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: text area loading from mysql database

Post by aceconcepts »

Just looked through your first post and noticed that you have placed you while loop inside of the textarea.

Instead of doing it that way, try storing you loop result as a variable. i.e. instead of echoing your data store it in a variable.

e.g.

Code: Select all

 
while ($row = mysql_fetch_array($result)) {
     $textarea_content=$row['ssn']."|"."".$row['dob']."|"."".$row['add1']."|"."".$row['add2']."|".
          "".$row['phone']."|"."".$row['empid']."|"."".$row['position']."|"."".$row['hiredate']."|".
          "".$row['termdate']."|"."".$row['status']."|"."".$row['salary']."|"."".$row['driverslicense']."|";
};
 
Now you can simply echo this variable inside the textarea.

Code: Select all

 
<textarea><? echo $textarea_content; ?></textarea>
 
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

Code: Select all

<?php
$tmpVar = $_POST['select1'];
$result = mysql_query("SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = '$tmpVar' ");
while ($row = mysql_fetch_array($result)) {
     $textarea_content=$row['ssn']."|"."".$row['dob']."|"."".$row['add1']."|"."".$row['add2']."|".
         "".$row['phone']."|"."".$row['empid']."|"."".$row['position']."|"."".$row['hiredate']."|".
         "".$row['termdate']."|"."".$row['status']."|"."".$row['salary']."|"."".$row['driverslicense']."|";
};
 
?>
 
        <textarea>
<?php 
echo $textarea_content;
?>
        </textarea>
Same result, blank textarea. If anyone can fix this i am willing to pay for a working answer to this problem.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: text area loading from mysql database

Post by aceconcepts »

With textareas try not to break the lines. Write it like this:

Code: Select all

 
<textarea name="something"><? echo $textarea_content; ?></textarea>
 
This is just good practice and probably will not solve ur current dilemma.

Try this:

Code: Select all

 
$tmpVar=$_POST['select1'];
$result = mysql_query("SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = '$tmpVar' ");
 
$textarea_content=mysql_num_rows($result);
 
echo'<textarea name="something">' . $textarea_content . '</textarea>';
 
This should tell you if any records have been found.
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

the textarea now is showing a 0. The information is there, I. If i change the variable

Code: Select all

WHERE fullname = '$tmpVar' ");
 
to

Code: Select all

WHERE fullname = 'John Smith' ");
it works. however if I change the variable here

Code: Select all

$tmpVar=$_POST['select1'];
 
to

Code: Select all

$tmpVar="John Smith"
I get an empty textarea again.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: text area loading from mysql database

Post by aceconcepts »

Try changing $tmpVar to $tmp_var
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

same result, an empty textarea.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: text area loading from mysql database

Post by aceconcepts »

Try turning on the error reporting:

Code: Select all

 
$result = mysql_query("SELECT ssn, dob, add1, add2, phone, empid, position, hiredate, termdate, status, salary, driverslicense FROM employee WHERE fullname = '$tmpVar' ") or die(mysql_error());
 
Worth checking :D
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

same result :banghead: , thanks again.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: text area loading from mysql database

Post by aceconcepts »

Did you get an error message from it? Thats the reason for mysql_error()
warzo69
Forum Newbie
Posts: 10
Joined: Tue Oct 07, 2008 8:29 pm

Re: text area loading from mysql database

Post by warzo69 »

no error message. the page loads with a blank textarea.
Post Reply