I'm building a webpage that displays a table in mysql database and has a form which allows the user to add more rows to that table. After clicking the submit button, the new row is successfully added into the table by looking at mysql command in the console, but in the webpage, it's not. I have to reload the page, and then it displays the new row, but this causes that new row to be added into the table again by checking in the mysql console. Why would it be like this? I want the new row to be shown in the table once the submit button is click. Does someone have some idea?

Thanks.

    Hi Scorpioy,

    Could you paste your code here so that we can see what might be the problem?

      At the end of your update script, use

      header("Location: mydisplay.php");
      

      That should get around caching, and also prevents the reload button from resubmitting the form data.

        I used header("Location: mydisplay.php"); at the end of update script, but I got the same problem. The server says 'Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\apache\htdocs\myposting.php:106) in c:\program files\apache group\apache\htdocs\myposting.php on line 154'

        Then I tried to put it ahead of line 106, but this time it even doesn't come to this page from the previous page.

        Here is my code:

        <?php

        GLOBAL $button;

        while ( list ($key, $val) = each ($_POST) ) :
        {
        print "$val <br>";

        if($key == "submit_row")
        $button=$val;
        }
        endwhile;

        $db = mysql_connect("localhost", "root") or die("Could not connect to MySQL server!");

        mysql_select_db("BS",$db);
        $result = mysql_query("SELECT courseName, title, edition, quality, price, otherInfo, shijian
        FROM book
        WHERE id = 001",$db);

        echo "<div id=\"content\">";
        echo "<div style=\"border: 1px solid #ccc; padding: 10px; margin: 1em 2em;\"> ";

        echo "<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n";
        echo "<tr>";

        echo "<td><table border=\"2\" cellspacing=\"3\" cellpadding=\"3\" class=\"navbar\">";
        echo "<tr> <h3>Book Selling </h3> </tr>";

        if ($myrow = mysql_fetch_array($result))
        {

        echo "<tr> <td>Action</td> <td>Course Name</td> <td>Book Title</td> <td>Edition</td> <td>Quality</td> <td>price</td> <td>Other Information</td> <td>date posted</td></tr>";

        echo "<form action=\"myPosting.php\" method=\"post\" >";
        do {
        $i = 0;
        $courseN = $myrow["courseName"];
        $title = $myrow["title"]; $edition = $myrow["edition"]; $quality = $myrow["quality"];
        $price = $myrow["price"]; $other = $myrow["otherInfo"]; $time = $myrow["shijian"];

        //echo "<INPUT TYPE=\"checkbox\" NAME=\"$i\" VALUE=\"$i\"> <tr> <td>$courseN</td><td>$title</td><td>$edition</td><td>$quality</td><td>$price</td> <td>$other</td></tr> <BR> ";
        printf("<tr> <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td> <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td>
        <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a> </td><td class=\"navbar\"><a href=\"java script:void(0);\">%d</a></td>
        <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td><td class=\"navbar\"><a href=\"java script:void(0);\">%f</a></td>
        <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td> <td >%s</a></td>
        </tr>\n", "Delete", $courseN, $title, $edition, $quality, $price, $other, $time); //here is line 106

        $i++;
        } while ($myrow = mysql_fetch_array($result));

        // echo ("<input type=\"submit\" name=\"submitDel\" value=\"delete\" onclick = \" confirm(\"Do you want to delete this/these line(s)?\")\" >");

        echo "</form></tr> ";
        } else {
        echo "<h4>No records for book selling</h4>";
        }

        //add a row

        echo "<h4>Add a book:</h4>";
        echo "<p>";
        echo "<form action=\"myPosting.php\" method=\"post\" name=\"form1\" value=\"newBook\">";

        echo "Course Name <input type=\"text\" name=\"course\" size=\"15\" ID=\"Text3\"><br>";
        echo "Book Title <input type=\"text\" name=\"title\" size=\"15\" ID=\"Text4\"><br>";
        echo "Book Edition <input type=\"text\" name=\"edition\" size=\"15\" ID=\"Text5\"><br>";
        echo "Book Quality

        <select name=\"quality\"><option> </option><option>A</option><option>B</option><option>C</option><option>D</option><option>E</option> </select><br>";
        echo "Book Price <input type=\"text\" name=\"price\" size=\"15\" ID=\"Text5\"><br>";

        echo " Other Information: <br>";

        echo "<textarea name = \"otherInfo\" rows = 4 cols = 42></textarea> <br>";
        echo "<input type=\"submit\" name=\"submit_row\" value=\"Add book\" >";

        echo "</form>";
        echo "</table></tr>";

        if($button=="Add book"):
        {
        $courseN = $POST['course'];
        $title = $
        POST['title']; $edition = $POST['edition']; $quality = $POST['quality'];
        $price = $POST['price']; $other = $POST['otherInfo'];

        $sql = "insert into book values (001, '$courseN', '$title', '$edition', '$quality', '$price', '$other', CURDATE())";

        if (! @($sql))
        echo "<p>Error in adding: mysql_error(). </p>";

        header("Location: myPosting.php");

        }
        endif;

        echo "<p><br>";

        // category : furniture
        // category : furniture
        // category : furniture

        echo "<h2></h2>";

        $result2 = mysql_query("SELECT name, quality, price, otherInfo, shijian
        FROM furniture
        WHERE id = 001",$db);

        echo "<div id=\"content\">";
        echo "<div style=\"border: 1px solid #ccc; padding: 10px; margin: 1em 2em;\"> ";

        echo "<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n";
        echo "<tr>";

        echo "<td><table border=\"2\" cellspacing=\"3\" cellpadding=\"3\" class=\"navbar\">";
        echo "<tr> <h3>Furniture Selling </h3> </tr>";

        if ($myrow2 = mysql_fetch_array($result2))
        {

        echo "<tr> <td>Action</td> <td>Item Name</td> <td>Quality</td> <td>price</td> <td>Other Information</td> <td>date posted</td></tr>";

        echo "<form action=\"myPosting.php\" method=\"post\" > ";
        do {
        $i = 0;
        $itemN = $myrow2["name"];
        $quality = $myrow2["quality"];
        $price = $myrow2["price"]; $other = $myrow2["otherInfo"]; $time = $myrow2["shijian"];

        printf("<tr> <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td> </td><td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td>
        <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td><td class=\"navbar\"><a href=\"java script:void(0);\">%f</a></td>
        <td class=\"navbar\"><a href=\"java script:void(0);\">%s</a></td> <td >%s</a></td>
        </tr>\n", "Delete", $itemN,$quality, $price, $other, $time);

        $i++;
        } while ($myrow2 = mysql_fetch_array($result2));

        // echo ("<input type=\"submit\" name=\"submitDel\" value=\"delete\" onclick = \" confirm(\"Do you want to delete this/these line(s)?\")\" >");

        echo "</form></tr> ";
        } else {
        echo "<h4>No records for furniture selling</h4>";
        }

        //add a row

        echo "<h4>Add an item:</h4>";
        echo "<p>";
        echo "<form action=\"myPosting.php\" method=\"post\" name=\"form2\" value=\"newFurniture\">";

        echo "Item Name

        <input type=\"text\" name=\"course\" size=\"15\" ID=\"Text3\"><br>";

        echo "Furniture Quality

        <select name=\"quality\"><option> </option><option>A</option><option>B</option><option>C</option><option>D</option><option>E</option> </select><br>";
        echo "Furniture Price <input type=\"text\" name=\"price\" size=\"15\" ID=\"Text5\"><br>";

        echo " Other Information: <br>";

        echo "<textarea name = \"otherInfo\" rows = 4 cols = 42></textarea> <br>";
        echo "<input type=\"submit\" name=\"submit_row\" value=\"Add furniture\" >";

        echo "</form>";
        echo "</table></tr>";

        echo "</table> ";
        echo "</div></div>";

        if($button=="Add furniture"):
        {
        $itemN = $POST['course'];
        $quality = $
        POST['quality'];
        $price = $POST['price']; $other = $POST['otherInfo'];

        $sql = "insert into furniture values (001, '$itemN', '$quality', '$price', '$other', CURDATE())";

        if (! @($sql))
        echo "<p>Error in adding: mysql_error(). </p>";

        header("Location: myPosting.php");

        }
        endif;

          Hi Scorpioy,

          I can see nothing wrong in your code but I think the problem is that you need to reload your page to see the new additions to the database. You could use the header command except that header doenst work when text has already been sent to the browser.

          Instead I'd just use javascript to reload the page;

          <script Language="JavaScript">
          <!--
          document.location.href ("myPosting.php");
          -->
          </script>
          

          However, to make your life easier why don't you just make two separate files. The first contains your form and display the data from the database. When the form is submitted you use a second file that does the php commands to add the form data to the database.

          In the second file you use just php and don't output anything to the browser, then you can use header("Location: ....) to load the first file.

          Hope this helps

          Oh by the way; when you paste code into this forum, please use the

           and [/ php] tags in your post. This makes for easier reading :)

            That JavaScript has the same effect as before. Then I tried to use 'header("Location: mydisplay.php");' again. I put it in here:

            while ( list ($key, $val) = each ($_POST) ) :
            {
            if($key == "submit_row")
            {
            $button=$val;
            header("Location: myPosting.php");
            }
            }
            endwhile;

            and its working now, hahah.

            The reason that I want to put things within one page is because I think this way would provide a natual feeling to the users. Users can see what they have so far; they can add more items, after clicking submit button, new item is shown in the table immediately, so that users don't need go back and forth to see the changes. But thanks for your suggestion any way.

              Yes sorry I assumed that you would have one file (or set of functions) for updating and one for displaying. In your case the header function is not applicable and Javascript would be a better approach.

                Write a Reply...