a little help

Thread Solved

Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

a little help

 
0
  #1
May 7th, 2008
I'm trying to get this script to work and it is only giving me a blank white page, can someone take a look at it and see if you can tell whats wrong with it.
  1. <?php
  2. // Show simple format of the records so person can choose the reference name/number
  3. // this is then passed to the next page, for all details
  4.  
  5. $host = "localhost";
  6. $user = "xxx";
  7. $pass = "xxx";
  8. $db = "phonebook";
  9.  
  10. //Connecting to MYSQL
  11. $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  12.  
  13. //Select the database we want to use
  14. mysql_select_db($db) or die ("Could not find database");
  15.  
  16. // Get all records in all columns from table and put it in $result.
  17. $query = "SELECT * FROM people";
  18.  
  19. // execute query
  20. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  21.  
  22. // Get all records in all columns from table and put it in $result.
  23. $result=mysql_query("select * from people");
  24.  
  25. /*Split records in $result by table rows and put them in $row.
  26. Make it looping by while statement. */
  27. while($row=mysql_fetch_assoc($result)){
  28.  
  29. // Output
  30. echo "ID : $row['id'] <br/>";
  31. echo "First Name : $row['fname'] <br/>";
  32. echo "Last Name : $row['lname'] <br/>";
  33. echo "Phone Number : $row['phone_num'] <br/>";
  34. echo "Extension : $row['ext'] <br/>";
  35. echo "Title : $row['title'] <br/>";
  36. echo "Department : $row['dept'] <br/>";
  37. echo "Fax Number : $row['fax'] <hr>";
  38.  
  39. // Add a link with a parameter(id) and it's value.
  40. echo '<a href="phoneupdate1.php?id='.$row['id'].'">Update</a>';
  41.  
  42. }
  43.  
  44. mysql_close();
  45. ?>
I must be missing something simple but I just can't find it. This is a form to update a record in a MySQL database.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: a little help

 
0
  #2
May 7th, 2008
add this before the while():
  1. if(mysql_num_rows($result)<1) {
  2. echo 'No Results';
  3. } else {
  4. // while statement here
  5. }

This will determine if the query is returning a result
Last edited by Will Gresham; May 7th, 2008 at 5:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: a little help

 
0
  #3
May 7th, 2008
No, still same result. Here is how I put it in:
  1. if(mysql_num_rows($result)<1) {
  2. echo 'No Results';
  3. } else {
  4. // while statement here
  5. while($row=mysql_fetch_assoc($result)){
  6.  
  7. echo "ID : $row['id'] <br/>";
  8. echo "First Name : $row['fname'] <br/>";
  9. echo "Last Name : $row['lname'] <br/>";
  10. echo "Phone Number : $row['phone_num'] <br/>";
  11. echo "Extension : $row['ext'] <br/>";
  12. echo "Title : $row['title'] <br/>";
  13. echo "Department : $row['dept'] <br/>";
  14. echo "Fax Number : $row['fax'] <hr>";
  15.  
  16. // Add a link with a parameter(id) and it's value.
  17. echo '<a href="phoneupdate1.php?id='.$row['id'].'">Update</a>';
  18. }
  19.  
  20. mysql_close();
  21. ?>
I thought it looked like it needed one more closing bracket (}) after the echo but that didn't improve anything either.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: a little help

 
0
  #4
May 7th, 2008
add error_reporting(E_ALL); to the top of the page to override any php.ini config which may be blocking errors from displaying.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: a little help

 
0
  #5
May 7th, 2008
Still just a blank white screen. Hmm perplexing.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: a little help

 
0
  #6
May 7th, 2008
Do other PHP pages work?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: WCSO-IT is an unknown quantity at this point 
Solved Threads: 1
WCSO-IT WCSO-IT is offline Offline
Newbie Poster

Re: a little help

 
0
  #7
May 7th, 2008
It looks like you are missing one more } in your code. It seems like the one you put after the last echo closes your new while statement, but your else statement is still open.

Also, in your original code you had 2 statements to execute queries.. was the 2nd for testing purposes?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: a little help

 
0
  #8
May 8th, 2008
Yes, all of my other scripts are running just fine.

I took a look at the }'s and agree that it looks like there needs to be one more. I played with adding one but its not making any difference. I think i did have an extra $results in there as well and took that out. Here is what I have now. I've made so many changes now that I don't even remember what I started with
In my while statement, some examples have shown to use fetch_row and some have fetch_assoc, I've got fetch_row in there now. :
  1. <?php
  2. error_reporting(E_ALL);
  3.  
  4. $host = "localhost";
  5. $user = "xxx";
  6. $pass = "xxx";
  7. $db = "phonebook";
  8.  
  9. //Connecting to MYSQL
  10. $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  11.  
  12. //Select the database we want to use
  13. mysql_select_db($db) or die ("Could not find database");
  14.  
  15. // Get all records in all columns from table and put it in $result.
  16. $query = "SELECT * FROM people ORDER BY fname ASC";
  17.  
  18. // execute query
  19. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  20.  
  21. // Get all records in all columns from table and put it in $result.
  22.  
  23. if (mysql_num_rows($result) > 0) {
  24.  
  25. while($row=mysql_fetch_row($result)){
  26.  
  27. echo "ID : .$row['id'] <br/>";
  28. echo "First Name : $row['fname'] <br/>";
  29. echo "Last Name : $row['lname'] <br/>";
  30. echo "Phone Number : $row['phone_num'] <br/>";
  31. echo "Extension : $row['ext'] <br/>";
  32. echo "Title : $row['title'] <br/>";
  33. echo "Department : $row['dept'] <br/>";
  34. echo "Fax Number : $row['fax'] <hr>";
  35.  
  36. // Add a link with a parameter(id) and it's value.
  37. echo '<a href="phoneupdate1.php?id='.$row['id'].'">Update</a>';
  38. }
  39. else {
  40. // no
  41. // print status message
  42. echo "No rows found!";
  43. }
  44. }
  45. //mysql_close();
  46. ?>
This is getting frustrating. If I could get it to display something, i could work with it. The phoneupdate1.php file it is sending to displays fine but needs the info from this page.

All i want to do is be able to change data in my MySQL database if anyone has a more simple way of doing it.

Thanks for all your help guys.
Last edited by rickarro; May 8th, 2008 at 10:36 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 525
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: a little help

 
0
  #9
May 8th, 2008
A couple of problems with your current code:

mysql_fetch_row returns a row in a numerical array, you would need to use $row[0], $row[1] for displaying the data, to use what you are, you will need to use mysql_fetch_assoc to call the cells by the column names.

There is a slight problem here:
if (mysql_num_rows($result) > 0) {

while($row=mysql_fetch_row($result)){

echo "ID : .$row['id'] <br/>";
echo "First Name : $row['fname'] <br/>";
echo "Last Name : $row['lname'] <br/>";
echo "Phone Number : $row['phone_num'] <br/>";
echo "Extension : $row['ext'] <br/>";
echo "Title : $row['title'] <br/>";
echo "Department : $row['dept'] <br/>";
echo "Fax Number : $row['fax'] <hr>";

// Add a link with a parameter(id) and it's value.
echo '<a href="phoneupdate1.php?id='.$row['id'].'">Update</a>';
}
else { 
    // no 
    // print status message 
    echo "No rows found!";
	} 
}

You have put it as this:
If {
While {
End While }
Else {}
End If }

It should be:
  1. if (mysql_num_rows($result) > 0) {
  2. while($row=mysql_fetch_row($result)){
  3. echo "ID : .$row['id'] <br/>";
  4. echo "First Name : $row['fname'] <br/>";
  5. echo "Last Name : $row['lname'] <br/>";
  6. echo "Phone Number : $row['phone_num'] <br/>";
  7. echo "Extension : $row['ext'] <br/>";
  8. echo "Title : $row['title'] <br/>";
  9. echo "Department : $row['dept'] <br/>";
  10. echo "Fax Number : $row['fax'] <hr>";
  11. // Add a link with a parameter(id) and it's value.
  12. echo '<a href="phoneupdate1.php?id='.$row['id'].'">Update</a>';
  13. }
  14. } else {
  15. // no
  16. // print status message
  17. echo "No rows found!";
  18. }

I'm afraid I cant see any other problems with the actual code, I suggest you also check the database and make sure that the column names are correct (they are case sensitive) and that there is actually data in the table you are using.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: a little help

 
0
  #10
May 8th, 2008
Ok, you got it there, i changed it to $row and included the row numbers instead of the names and it is now displaying the data I want. Thank you so much.
Here is what I've got now:
  1. <?php
  2. error_reporting(E_ALL);
  3. // Show simple format of the records so person can choose the reference name/number
  4. // this is then passed to the next page, for all details
  5.  
  6. $host = "localhost";
  7. $user = "xxx";
  8. $pass = "xxx";
  9. $db = "phonebook";
  10.  
  11. //Connecting to MYSQL
  12. $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  13.  
  14. //Select the database we want to use
  15. mysql_select_db($db) or die ("Could not find database");
  16.  
  17. // Get all records in all columns from table and put it in $result.
  18. $query = "SELECT * FROM people ORDER BY fname ASC";
  19.  
  20. // execute query
  21. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  22.  
  23. // Get all records in all columns from table and put it in $result.
  24.  
  25. if (mysql_num_rows($result) > 0) {
  26. while($row=mysql_fetch_row($result)){
  27. echo "ID : $row[0] <br/>";
  28. echo "First Name : $row[1] <br/>";
  29. echo "Last Name : $row[2] <br/>";
  30. echo "Phone Number : $row[3] <br/>";
  31. echo "Extension : $row[4] <br/>";
  32. echo "Title : $row[5] <br/>";
  33. echo "Department : $row[6] <br/>";
  34. echo "Fax Number : $row[7] <hr>";
  35. // Add a link with a parameter(id) and it's value.
  36. echo '<a href="phoneupdate1.php?id='.$row[0].'">Update</a>';
  37. }
  38. } else {
  39. // no
  40. // print status message
  41. echo "No rows found!";
  42. }
  43. //mysql_close();
  44. ?>

This is how it displays with the word "Update" being a link to my update form:

UpdateID : 4
First Name : Edward
Last Name : ScissorHands
Phone Number : xxx-123-456
Extension : 111
Title : Hedgetrimmer
Department : Roads & Grounds
Fax Number :

So far so good, when i click on the update link, it takes me to my update form but does not display the data. Here is what it displays:

Reference:

Last Name :<? echo $row['lname']; ?>
First Name :<? echo $row['fname']; ?>
Phone Number :<? echo $row['phone_num']; ?>
Extension :<? echo $row['ext']; ?>
Title :<? echo $row['title']; ?>
Department :<? echo $row['dept']; ?>
Fax Number :<? echo $row['fax']; ?>

SUBMIT BUTTON

Here is the form it is using:

  1. <?php
  2. $record = $_POST['record'];
  3. echo "Reference: $record<br><BR>";
  4.  
  5. $host = "localhost";
  6. $user = "xxx";
  7. $pass = "xxx";
  8. $db = "phonebook";
  9.  
  10. //Connecting to MYSQL
  11. MySQL_connect("$host","$user","$pass");
  12.  
  13. //Select the database we want to use
  14. mysql_select_db($db) or die("Could not find database");
  15.  
  16. // ***** This part will process when you Click on "Submit" button *****
  17. // Check, if you clicked "Submit" button
  18. if($_POST['Submit']){
  19.  
  20. // Get parameters from form.
  21. $id=$_POST['id'];
  22. $fname=$_POST['fname'];
  23. $lname=$_POST['lname'];
  24. $phone_num=$_POST['phone_num'];
  25. $ext=$_POST['ext'];
  26. $title=$_POST['title'];
  27. $dept=$_POST['dept'];
  28.  
  29. // Do update statement.
  30. mysql_query("update phonebook set First Name='$fname', Last Name='$lname', Phone Number='$phone_num', Extension='$ext', Title='$title', Department='$dept' where id='$id'");
  31.  
  32. // Re-direct this page to select.php.
  33. header("location:phoneupdate.php");
  34. exit;
  35. }
  36. // ************* End update part *************
  37.  
  38. // *** Select data to show on text fields in form. ***
  39.  
  40. // Get id parameter (GET method) from select.php
  41. $id=$_POST['id'];
  42.  
  43. // Get records in all columns from table where column id equal in $id and put it in $result.
  44. $result=mysql_query("select * from people where id='$id'");
  45.  
  46. // Split records in $result by table rows and put them in $row.
  47. $row=mysql_fetch_assoc($result);
  48.  
  49. // Close database connection.
  50. mysql_close();
  51. ?>
  52.  
  53. <!-- END OF PHP CODES AND START HTML TAGS -->
  54.  
  55. <html>
  56. <body>
  57. <!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
  58. <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
  59. <p>Last Name :
  60. <!-- name of this text field is "Last Name" -->
  61. <input name="lname" type="text" id="lname" value="<? echo $row['lname']; ?>"/>
  62. <br />
  63. First Name :
  64. <!-- name of this text field is "fname" -->
  65. <input name="fname" type="text" id="fname" value="<? echo $row['fname']; ?>"/>
  66. <br />
  67. Phone Number :
  68. <!-- name of this text field is "phone_num" -->
  69. <input name="phone_num" type="text" id="phone_num" value="<? echo $row['phone_num']; ?>"/>
  70. </p>
  71. Extension :
  72. <!-- name of this text field is "ext" -->
  73. <input name="ext" type="text" id="ext" value="<? echo $row['ext']; ?>"/>
  74. <br />
  75. Title :
  76. <!-- name of this text field is "title" -->
  77. <input name="title" type="text" id="title" value="<? echo $row['title']; ?>"/>
  78. <br />
  79. Department :
  80. <!-- name of this text field is "dept" -->
  81. <input name="dept" type="text" id="dept" value="<? echo $row['dept']; ?>"/>
  82. <br />
  83. Fax Number :
  84. <!-- name of this text field is "fax" -->
  85. <input name="fax" type="text" id="fax" value="<? echo $row['fax']; ?>"/>
  86. <br />
  87. <p>
  88. <input type="submit" name="Submit" value="Submit" />
  89. </p>
  90. </form>
  91. </body>
  92. </html>

So now when i use this form does
  1. $id=$_POST['id'];
  2. $fname=$_POST['fname'];
  3. $lname=$_POST['lname'];
need to reflect the $row[0] instead of the ['id'] as well? I'm not getting the data from the first form for some reason.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC