camdenite Posted February 7, 2009 Share Posted February 7, 2009 Hi all, I've been working on this blog creation script but it won't submit. I keep getting "PHP Warning: mysql_close(): no MySQL-Link resource supplied" and it points to the line with "mysql_close(); // Close the database connection." but I have "require_once ('../mysql_connect.php');" Baffled. Please help. Thankyou. Colin. <?php // Check if the form has been submitted. if (isset($_POST['submitted'])) { require_once ('../mysql_connect1.php'); // Connect to the db. $errors = array(); // Initialise error array. // Check for a page name. if (empty($_POST['header'])) { $errors[] = 'You forgot to enter a Blog Header.'; } else { $header = escape_data($_POST['heading']); } // Check for a page title. if (empty($_POST['first_para'])) { $errors[] = 'You forgot to enter a First Paragraph.'; } else { $first_para = escape_data($_POST['first_para']); } // Check for Page Content. if (empty($_POST['entry'])) { $errors[] = 'You forgot to enter an Entry.'; } else { $entry = escape_data($_POST['entry']); } if (empty($errors)) { // If everything's OK. // Create the page in the database. /* Check for an existing page with the same name. $query = "SELECT blog_id FROM blogb WHERE heading='$heading'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { */ // Make the query. $query = "INSERT INTO blogb (blog_id, date1, date2, heading, category_id, first_para, entry, more, image1, image2, image3, image4, pub2 ) VALUES ('$blog_id', '$date1', '$date2', '$heading', '$category_id', '$first_para', '$entry', '$more', '$image1', '$image2', '$image3', '$image4', '$pub2')"; $result = @mysql_query ($query); // Run the query. if ($result) { // If it ran OK. // Redirect the page_created.php page. // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } // Add the page. $url .= '/blog_created.php'; header("Location: $url"); exit(); } else { // If it did not run ok. $errors[] = 'The page could not be registered due to a system error. We appologise for any inconvenience.'; // Public message. $errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message. } } else { // Page already exists. $errors[] = 'This page already exists.'; } /* } // End of empty if (empty(errors)) IF. */ } else { // Form has not been submitted. $errors = NULL; } // End of main submit conditional. // Begin the page now. $page_title = 'Blog Creator'; if (!empty($errors)) { // Print any error messages. echo '<h1>Error!</h1> <p>The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p>'; } // Create the form. echo '<form action="create_blog.php" method="post"> <table width="500px"> <tr> <td>Date: <b>'.$row['date2'].'</b><br /></td></tr> <tr> <td>Heading: <br /> <input type="text" name="heading" size="70" value="'.$row['heading'].'" /> </td> </tr> <tr> <td>Category Code<br /> (enter 2-digit code from list on right):<br /> <input type="text" name="category_id" size="5" value="'.$row['category_id'].'" /></td></tr> <tr> <td valign="top">First Paragraph: <br /> <textarea name="first_para" cols="60" rows="6" />'.$row['first_para'].'</textarea></td> </tr> <tr> <td valign="top">Entry: <br /> <textarea name="entry" cols="60" rows="10" />'.$row['entry'].'</textarea></td> </tr> <tr> <td valign="top">More: <br /> <textarea name="more" cols="60" rows="10" />'.$row['more'].'</textarea></td> </tr> <tr> <td>First Image: <br /> <input type="text" name="image1" size="70" value="'.$row['image1'].'" /> </td> </tr> <tr> <td>Second Image: <br /> <input type="text" name="image2" size="70" value="'.$row['image2'].'" /> </td> </tr> <tr> <td>Third Image: <br /> <input type="text" name="image3" size="70" value="'.$row['image3'].'" /> </td> </tr> <tr> <td> </tr> </tr> <tr> <td> <input type="hidden" name="id" value="submitted" /> <input type="submit" name="submit" value="Save" style="background:#ff0 none;" /> </td> </tr> </table> </form>' ; mysql_close(); // Close the database connection. ?> Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/ Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 Unless you plan on using/managing multiple database connections, you don't even need to have that in there. the script will automatically close any connections when it's done executing. Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/#findComment-756869 Share on other sites More sharing options...
camdenite Posted February 7, 2009 Author Share Posted February 7, 2009 Thanks for that. I thought it was more secure if you close mysql every time you finish the script. I took the line out but the script still doesn't submit. ??? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/#findComment-756873 Share on other sites More sharing options...
PFMaBiSmAd Posted February 7, 2009 Share Posted February 7, 2009 Your require_once() that causes the mysql_connect() is inside of a conditional statement. The error related to the mysql_close() is secondary to your real problem, but indicates that your conditional logic is never TRUE. $_POST['submitted'] does not exist because you don't have a form field that is named 'submitted'. You have a field named "id" that has a value "submitted" and you have a field named "submit" that has a value "Save". You code would need to test either $_POST['id'] or $_POST['submit'] Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/#findComment-756880 Share on other sites More sharing options...
camdenite Posted February 10, 2009 Author Share Posted February 10, 2009 Hi again, I am completely lost as to why this is not creating new info on to the db. The script submits as if I don't fill out the form I get the errors. If I do I just get a blank page an it doesn't update the db... Nothing in the error log. I have been through this script but I am lost as to why it doesn't work and why there are no errors - wierd. Please help. Cheers. <?php require_once ('mysql_connect.php'); // Connect to the db. // Check if the form has been submitted. if (isset($_POST['submitted'])) { $errors = array(); // Initialise error array. // Check for a page name. if (empty($_POST['heading'])) { $errors[] = 'You forgot to enter a Blog Header.'; } else { $header = escape_data($_POST['heading']); } // Check for a page title. if (empty($_POST['first_para'])) { $errors[] = 'You forgot to enter a First Paragraph.'; } else { $first_para = escape_data($_POST['first_para']); } // Check for Page Content. if (empty($_POST['entry'])) { $errors[] = 'You forgot to enter an Entry.'; } else { $entry = escape_data($_POST['entry']); } if (empty($errors)) { // If everything's OK. // Create the page in the database. /* Check for an existing page with the same name. $query = "SELECT blog_id FROM blogb WHERE heading='$heading'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { */ // Make the query. $query = "INSERT INTO blogb (blog_id, date1, date2, heading, category_id, first_para, entry, more, image1, image2, image3, image4, pub2 ) VALUES ('$blog_id', '$date1', '$date2', '$heading', '$category_id', '$first_para', '$entry', '$more', '$image1', '$image2', '$image3', '$image4', '$pub2')"; $result = @mysql_query ($query); // Run the query. if ($result) { // If it ran OK. // Redirect the page_created.php page. // Start defining the URL. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Check for a trailing slash. if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) { $url = substr ($url, 0, -1); // Chop off the slash. } // Add the page. $url .= '/blog_created.php'; header("Location: $url"); exit(); } else { // If it did not run ok. $errors[] = 'The page could not be registered due to a system error. We appologise for any inconvenience.'; // Public message. $errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message. } } else { // Page already exists. $errors[] = 'This page already exists.'; } /* } // End of empty if (empty(errors)) IF. */ } else { // Form has not been submitted. $errors = NULL; } // End of main submit conditional. // Begin the page now. $page_title = 'Blog Creator'; if (!empty($errors)) { // Print any error messages. echo '<h1>Error!</h1> <p>The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p>'; } // Create the form. echo '<form action="create_blog.php" method="post"> <table width="500px"> <tr> <td>Date: <b>'.$row['date2'].'</b><br /></td></tr> <tr> <td>Heading: <br /> <input type="text" name="heading" size="70" value="'.$row['heading'].'" /> </td> </tr> <tr> <td>Category Code<br /> (enter 2-digit code from list on right):<br /> <input type="text" name="category_id" size="5" value="'.$row['category_id'].'" /></td></tr> <tr> <td valign="top">First Paragraph: <br /> <textarea name="first_para" cols="60" rows="6" />'.$row['first_para'].'</textarea></td> </tr> <tr> <td valign="top">Entry: <br /> <textarea name="entry" cols="60" rows="10" />'.$row['entry'].'</textarea></td> </tr> <tr> <td valign="top">More: <br /> <textarea name="more" cols="60" rows="10" />'.$row['more'].'</textarea></td> </tr> <tr> <td>First Image: <br /> <input type="text" name="image1" size="70" value="'.$row['image1'].'" /> </td> </tr> <tr> <td>Second Image: <br /> <input type="text" name="image2" size="70" value="'.$row['image2'].'" /> </td> </tr> <tr> <td>Third Image: <br /> <input type="text" name="image3" size="70" value="'.$row['image3'].'" /> </td> </tr> <tr> <td> </tr> </tr> <tr> <td> <input type="submit" name="submitted" value="Submit" style="background:#ff0 none;" /> <input type="hidden" name="submitted" value="TRUE" /> </td> </tr> </table> </form>' ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/#findComment-758969 Share on other sites More sharing options...
premiso Posted February 10, 2009 Share Posted February 10, 2009 Don't use mysql_close. It is not necessary or needed. That should fix that error. (See the man page above for more information on that). Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources. Unless you are using persistent connections, it is not necessary. Also you are not showing us where you are calling mysql_close that would help. Please use [ code] [ /code] tags (no initial space) for posting code! Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/#findComment-758972 Share on other sites More sharing options...
camdenite Posted February 10, 2009 Author Share Posted February 10, 2009 Sorry I'm posting a new error under an old post. I should start a new thread. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/144224-php-warning-mysql_close-no-mysql-link-resource-supplied/#findComment-758978 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.