User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 361,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,035 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1006 | Replies: 48 | Solved
Reply
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rickarro rickarro is offline Offline
Junior Poster in Training

a little help

  #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.
<?php
// Show simple format of the records so person can choose the reference name/number
// this is then passed to the next page, for all details

$host = "localhost"; 
$user = "xxx"; 
$pass = "xxx"; 
$db = "phonebook"; 

//Connecting to MYSQL
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

//Select the database we want to use
mysql_select_db($db) or die ("Could not find database");

// Get all records in all columns from table and put it in $result.
$query = "SELECT * FROM people"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from people");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */
while($row=mysql_fetch_assoc($result)){

// Output
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>';

}

mysql_close();
?>
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Location: UK
Posts: 51
Reputation: xan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
xan's Avatar
xan xan is offline Offline
Junior Poster in Training

Re: a little help

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

This will determine if the query is returning a result
Last edited by xan : May 7th, 2008 at 4:47 pm.
Reply With Quote  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rickarro rickarro is offline Offline
Junior Poster in Training

Re: a little help

  #3  
May 7th, 2008
No, still same result. Here is how I put it in:
if(mysql_num_rows($result)<1) {
echo 'No Results';
} else {
// while statement here
while($row=mysql_fetch_assoc($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>';
}

mysql_close();
?>
I thought it looked like it needed one more closing bracket (}) after the echo but that didn't improve anything either.
Reply With Quote  
Join Date: May 2008
Location: UK
Posts: 51
Reputation: xan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
xan's Avatar
xan xan is offline Offline
Junior Poster in Training

Re: a little help

  #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  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rickarro rickarro is offline Offline
Junior Poster in Training

Re: a little help

  #5  
May 7th, 2008
Still just a blank white screen. Hmm perplexing.
Reply With Quote  
Join Date: May 2008
Location: UK
Posts: 51
Reputation: xan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
xan's Avatar
xan xan is offline Offline
Junior Poster in Training

Re: a little help

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

Re: a little help

  #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  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rickarro rickarro is offline Offline
Junior Poster in Training

Re: a little help

  #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. :
<?php
error_reporting(E_ALL);

$host = "localhost"; 
$user = "xxx"; 
$pass = "xxx"; 
$db = "phonebook"; 

//Connecting to MYSQL
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

//Select the database we want to use
mysql_select_db($db) or die ("Could not find database");

// Get all records in all columns from table and put it in $result.
$query = "SELECT * FROM people ORDER BY fname ASC"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// Get all records in all columns from table and put it in $result.

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!";
	} 
} 
//mysql_close();
?>
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 9:36 am.
Reply With Quote  
Join Date: May 2008
Location: UK
Posts: 51
Reputation: xan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
xan's Avatar
xan xan is offline Offline
Junior Poster in Training

Re: a little help

  #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:
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!";
} 

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  
Join Date: Jan 2008
Posts: 76
Reputation: rickarro is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rickarro rickarro is offline Offline
Junior Poster in Training

Re: a little help

  #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:
<?php
error_reporting(E_ALL);
// Show simple format of the records so person can choose the reference name/number
// this is then passed to the next page, for all details

$host = "localhost"; 
$user = "xxx"; 
$pass = "xxx"; 
$db = "phonebook"; 

//Connecting to MYSQL
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

//Select the database we want to use
mysql_select_db($db) or die ("Could not find database");

// Get all records in all columns from table and put it in $result.
$query = "SELECT * FROM people ORDER BY fname ASC"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// Get all records in all columns from table and put it in $result.

if (mysql_num_rows($result) > 0) {
  while($row=mysql_fetch_row($result)){
    echo "ID : $row[0] <br/>";
    echo "First Name : $row[1] <br/>";
    echo "Last Name : $row[2] <br/>";
    echo "Phone Number : $row[3] <br/>";
    echo "Extension : $row[4] <br/>";
    echo "Title : $row[5] <br/>";
    echo "Department : $row[6] <br/>";
    echo "Fax Number : $row[7] <hr>";
    // Add a link with a parameter(id) and it's value.
    echo '<a href="phoneupdate1.php?id='.$row[0].'">Update</a>';
  }
} else { 
  // no 
  // print status message 
  echo "No rows found!";
} 
//mysql_close();
?>

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:

<?php
$record = $_POST['record'];
echo "Reference: $record<br><BR>";

$host = "localhost";
$user = "xxx";
$pass = "xxx";
$db = "phonebook";

//Connecting to MYSQL
MySQL_connect("$host","$user","$pass");

//Select the database we want to use
mysql_select_db($db) or die("Could not find database");

// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){

// Get parameters from form.
$id=$_POST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone_num=$_POST['phone_num'];
$ext=$_POST['ext'];
$title=$_POST['title'];
$dept=$_POST['dept'];

// Do update statement.
mysql_query("update phonebook set First Name='$fname', Last Name='$lname', Phone Number='$phone_num', Extension='$ext', Title='$title', Department='$dept' where id='$id'");

// Re-direct this page to select.php.
header("location:phoneupdate.php");
exit;
}
// ************* End update part *************

// *** Select data to show on text fields in form. ***

// Get id parameter (GET method) from select.php
$id=$_POST['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from people where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);

// Close database connection.
mysql_close();
?>

<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<body>
<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Last Name :
<!-- name of this text field is "Last Name" -->
<input name="lname" type="text" id="lname" value="<? echo $row['lname']; ?>"/>
<br />
First Name :
<!-- name of this text field is "fname" -->
<input name="fname" type="text" id="fname" value="<? echo $row['fname']; ?>"/>
<br />
Phone Number :
<!-- name of this text field is "phone_num" -->
<input name="phone_num" type="text" id="phone_num" value="<? echo $row['phone_num']; ?>"/>
</p>
Extension :
<!-- name of this text field is "ext" -->
<input name="ext" type="text" id="ext" value="<? echo $row['ext']; ?>"/>
<br />
Title :
<!-- name of this text field is "title" -->
<input name="title" type="text" id="title" value="<? echo $row['title']; ?>"/>
<br />
Department :
<!-- name of this text field is "dept" -->
<input name="dept" type="text" id="dept" value="<? echo $row['dept']; ?>"/>
<br />
Fax Number :
<!-- name of this text field is "fax" -->
<input name="fax" type="text" id="fax" value="<? echo $row['fax']; ?>"/>
<br />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

So now when i use this form does
$id=$_POST['id'];
$fname=$_POST['fname'];
$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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC