Set up: MySQL 4.1.7, PHP 5.0.2.2, Apache 2.0.52, localhost, Windows98 SE
Objective:
- Page 1 - Using PHP to view data (ID, name, contact) from a MySql table (bands)
- Link on Page 1 opens Page 2 in new window
- Page 2 - Using PHP and form to add another name and contact to bands
Problem:
- Data added via consol persists and displays properly on both Page 1 and Page 2
- Data added via Page 2 appears on Page 2 as having been added with a properly incremented ID
- Refresh Page 1 does not show data added via Page 2
- Open Page 2 to add more data again appears to work as intended but data previously entered does not show up, nor does it appear via consol.
- ID is incremented in second visit to Page 2, but previous ID is skipped:
ID1 name contact from consol input persists
ID2 name contact from consol input persists
ID3 name contact from Page 2 input appears in Page 2, then does not persist
ID4 name contact from Page 2 input appears in Page 2, then does not persist and ID3 and related name and contact have disappeared.
Page 1 Code:
Page 2 code:Code:<?php $dbcnx = mysqli_init(); mysqli_options($dbcnx, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0"); mysqli_options($dbcnx, MYSQLI_OPT_CONNECT_TIMEOUT, 5); mysqli_real_connect($dbcnx, 'localhost', 'root', 'psswrd', 'dbname'); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } echo "<table cellpadding=2 border=1 align=center>"; echo "<tr>"; echo "<th>ID #</th>"; echo "<th>NAME</th>"; echo "<th>CONTACT</th>"; echo "</tr>"; $query = "SELECT * FROM bands"; $result = mysqli_query($dbcnx, $query) or die ("Error in query: $query. " . mysqli_error()); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_row($result)) { echo "<tr>"; echo "<td>" . $row[0] . "</td>"; echo "<td>" . $row[1] . "</td>"; echo "<td>" . $row[2] . "</td>"; echo "</tr>"; } } else { echo "No rows found!"; } mysqli_free_result($result); mysqli_close($dbcnx); echo "<tr>"; echo "<td colspan=3><a href='#' class='bodylink' onclick=\"window.open('addband.php','addband','height=400,width=700,top=10,left=10,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no,resizable=yes')\">Add a new band</a></td>"; echo "</tr>"; echo "</table>"; ?>
Question 1 : What's wrong?Code:<?php if (!isset($_POST['submit'])) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Band Name: <input type="text" name="name"><br> Contact: <input type="text" name="contact"><br> <input type="submit" name="submit" value="Add band"> </form> <?php } else { $dbcnx = mysqli_init(); mysqli_options($dbcnx, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0"); mysqli_options($dbcnx, MYSQLI_OPT_CONNECT_TIMEOUT, 5); mysqli_real_connect($dbcnx, 'localhost', 'root', 'psswrd', 'dbname'); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $name = $_POST['name']; $name = mysqli_real_escape_string($dbcnx, $name); $contact = $_POST['contact']; $contact = mysqli_real_escape_string($dbcnx, $contact); echo $name . " - - " . $contact; $query = "INSERT INTO bands (name, contact) VALUES ('$name','$contact')"; $result = mysqli_query($dbcnx, $query) or die ("Error in query: $query. " . mysqli_error()); echo "New record inserted with ID " . mysqli_insert_id($dbcnx); echo "<table cellpadding=2 border=1 align=center>"; echo "<tr>"; echo "<th>ID #</th>"; echo "<th>NAME</th>"; echo "<th>CONTACT</th>"; echo "</tr>"; $query = "SELECT * FROM bands"; $result = mysqli_query($dbcnx, $query) or die ("Error in query: $query. " . mysqli_error()); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_row($result)) { echo "<tr>"; echo "<td>" . $row[0] . "</td>"; echo "<td>" . $row[1] . "</td>"; echo "<td>" . $row[2] . "</td>"; echo "</tr>"; } } else { echo "No rows found!"; } echo "<tr>"; echo "<td colspan=3><a href='#' class='bodylink' onclick='self.close()'>CLOSE</a></td>"; echo "</tr>"; echo "</table>"; } mysqli_free_result($result); mysqli_close($dbcnx); ?>
Question 2 : Is the issue with my scripting or could it be with a configuration (my.ini, php.ini)?







for Rudy.

Bookmarks