Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
updating and displaying the contents of DB table, not updating to html table
Old 09-14-2009, 06:45 PM updating and displaying the contents of DB table, not updating to html table
Webmaster Talker

Posts: 611
Trades: 0
hi, i cant get my html table to display the new contents of my database table when i update it. i dont think my script is updating the database table either. any help or advice greatly appreciated. thank you. derek

here is the db connection script

Code:
 <?php 

////////////////////////////////////////
/// connect to database

////////////////////////////////////////
/// include file 

$host        = "localhost";
$database     = "php_lessons";
$username     = "root";
$password     = "";

mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

mysql_select_db($database);
 
 
 ?>
here are the entire contents of the php script. at the top it inserts data into the table, or is supposed to, below that it is supposed to output the new contents of the table., and below that is the form i use to update the contents of the table.

Code:
 <?php 
 
include("connections.php");
 
 
////////////////////////////////////////
////////////////////////////////////////

/// query db and loop through rows example

$field2 = $_POST['data2'];
$field3    = $_POST['data3'];
$field4    = $_POST['data4'];
$field5    = $_POST['data5'];
$field6    = $_POST['data6'];

if($field2 && $field3 && $field4){

    mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')");

}else{




}

//////////////////////////////////////////////////////////output data into a table///////////////////////////////



$query     = mysql_query("SELECT * FROM table1");

    $table = "<table width=\"40%\" border=\"1\">
                  <tr>
                        <td>heading1</td>
                        <td>heading2</td>
                        <td>heading3</td>
                        <td>heading4</td>
                        <td>heading5</td>
                        <td>heading6</td>
                  </tr>";

while($row = mysql_fetch_array($query)){

    $table .= "    <tr>
                    <td>".$row['record_id']."</td>
                    <td>".$row['field2']."</td>
                    <td>".$row['field3']."</td>
                    <td>".$row['field4']."</td>
                    <td>".$row['field5']."</td>
                    <td>".$row['field6']."</td>
                </tr>";

}

$table .= "</table>";

echo $table;
?>


<!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=iso-8859-1" />
<title>Insert Statement</title>
</head>

<body>




<form id="form1" name="form1" method="post" action="lesson8_test.php">
  <table width="31%" border="0">
    <tr>
      <td width="32%">Data2</td>
      <td width="68%"><input name="data2" type="text" id="data2" /></td>
    </tr>
    <tr>
      <td>Data3</td>
      <td><input name="data3" type="text" id="data3" /></td>
    </tr>
    <tr>
      <td>Data4</td>
      <td><input name="data4" type="text" id="data4" /></td>
    </tr>
    <tr>
      <td>Data5</td>
      <td><input name="data5" type="text" id="data5" /></td>
    </tr>
    <tr>
      <td>Data6</td>
      <td><input name="data6" type="text" id="data6" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>

</body>
</html>
silverglade is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-14-2009, 08:29 PM Re: updating and displaying the contents of DB table, not updating to html table
Webmaster Talker

Posts: 611
Trades: 0
nevermind i got it to work by changing the action name to lesson8test.php, and it worked, not sure why, here is the working code.

Code:
 <?php 
 
include("connections.php");
 
 
////////////////////////////////////////
////////////////////////////////////////

/// query db and loop through rows example

$field2 = $_POST['data2'];
$field3    = $_POST['data3'];
$field4    = $_POST['data4'];
$field5    = $_POST['data5'];
$field6    = $_POST['data6'];

if($field2 && $field3 && $field4){

    mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')");

}else{


echo "error";

}

//////////////////////////////////////////////////////////output data into a table///////////////////////////////



$query     = mysql_query("SELECT * FROM table1");

    $table = "<table width=\"40%\" border=\"1\">
                  <tr>
                        <td>heading1</td>
                        <td>heading2</td>
                        <td>heading3</td>
                        <td>heading4</td>
                        <td>heading5</td>
                        <td>heading6</td>
                  </tr>";

while($row = mysql_fetch_array($query)){

    $table .= "    <tr>
                    <td>".$row['record_id']."</td>
                    <td>".$row['field2']."</td>
                    <td>".$row['field3']."</td>
                    <td>".$row['field4']."</td>
                    <td>".$row['field5']."</td>
                    <td>".$row['field6']."</td>
                </tr>";

}

$table .= "</table>";

echo $table;
?>


<!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=iso-8859-1" />
<title>Insert Statement</title>
</head>

<body>




<form id="form1" name="form1" method="post" action="lesson8test.php">
  <table width="31%" border="0">
    <tr>
      <td width="32%">Data2</td>
      <td width="68%"><input name="data2" type="text" id="data2" /></td>
    </tr>
    <tr>
      <td>Data3</td>
      <td><input name="data3" type="text" id="data3" /></td>
    </tr>
    <tr>
      <td>Data4</td>
      <td><input name="data4" type="text" id="data4" /></td>
    </tr>
    <tr>
      <td>Data5</td>
      <td><input name="data5" type="text" id="data5" /></td>
    </tr>
    <tr>
      <td>Data6</td>
      <td><input name="data6" type="text" id="data6" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>

</body>
</html>
silverglade is offline
Reply With Quote
View Public Profile
 
Old 09-15-2009, 03:05 AM Re: updating and displaying the contents of DB table, not updating to html table
Experienced Talker

Posts: 46
Trades: 0
Here's a tip for you.

Next time you are not sure if you query ran through properly do this after your mysql_query($query);

echo mysql_error();

This will let you know what went wrong. It makes it much easier to debug .
__________________

Please login or register to view this content. Registration is FREE
-> Home of the crasher squirrel

Last edited by jeff_oneil; 09-15-2009 at 03:05 AM.. Reason: typo
jeff_oneil is offline
Reply With Quote
View Public Profile Visit jeff_oneil's homepage!
 
Old 09-15-2009, 06:57 PM Re: updating and displaying the contents of DB table, not updating to html table
Webmaster Talker

Posts: 611
Trades: 0
cool thank you very much! derek
silverglade is offline
Reply With Quote
View Public Profile
 
Old 09-19-2009, 12:13 PM Re: updating and displaying the contents of DB table, not updating to html table
bmcoll3278's Avatar
Super Talker

Posts: 118
Name: Brian Collins
Trades: 0
Quote:
Originally Posted by jeff_oneil View Post
Here's a tip for you.

Next time you are not sure if you query ran through properly do this after your mysql_query($query);

echo mysql_error();

This will let you know what went wrong. It makes it much easier to debug .
Thanks for the great advise on that. I have about 50 scripts on my site that insert data and I added that to all of them.
__________________
I hope to build a site with something for every one

Please login or register to view this content. Registration is FREE
bmcoll3278 is offline
Reply With Quote
View Public Profile Visit bmcoll3278's homepage!
 
Reply     « Reply to updating and displaying the contents of DB table, not updating to html table
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.68602 seconds with 12 queries