Current location: Hot Scripts Forums » Programming Languages » PHP » mysql query loading problem


mysql query loading problem

Reply
  #1 (permalink)  
Old 05-23-04, 03:16 PM
scorpioy scorpioy is offline
Newbie Coder
 
Join Date: May 2004
Location: CA
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
mysql query loading problem

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
Reply With Quote
Reply

Bookmarks


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

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Query problem Wraith PHP 5 03-06-04 05:16 PM
MySQL Expert Required!! Advanced Query Problem bluey_the_punch PHP 1 02-28-04 01:37 PM
PHP:search by MySQL query lowdog PHP 0 02-15-04 10:04 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 10:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.