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?
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 (! @mysql_query($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 (! @mysql_query($sql))
echo "<p>Error in adding: mysql_error(). </p>";
header("Location: myPosting.php");
}
endif;
Thanks