Jump to content


Photo

PHP Warning: mysql_close(): no MySQL-Link resource supplied


  • Please log in to reply
6 replies to this topic

#1 camdenite

camdenite

    Member

  • Members
  • PipPip
  • 28 posts

Posted 07 February 2009 - 11:32 AM

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.

?>

#2 .josh

.josh

    .josh

  • Staff Alumni
  • 14,815 posts

Posted 07 February 2009 - 11:54 AM

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.

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 camdenite

camdenite

    Member

  • Members
  • PipPip
  • 28 posts

Posted 07 February 2009 - 12:04 PM

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  :)

#4 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 07 February 2009 - 12:17 PM

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']


Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#5 camdenite

camdenite

    Member

  • Members
  • PipPip
  • 28 posts

Posted 10 February 2009 - 11:33 AM

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>'
;
?>



#6 premiso

premiso

    Trollololol

  • Members
  • PipPipPip
  • 6,969 posts
  • LocationHairy Beaverville

Posted 10 February 2009 - 11:35 AM

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!
Did I assist you? Feeling like contributing to my gaming addiction?

Assist with my gaming addiction! | Free Flash Games | lxt Article Site

#7 camdenite

camdenite

    Member

  • Members
  • PipPip
  • 28 posts

Posted 10 February 2009 - 11:45 AM

Sorry I'm posting a new error under an old post. I should start a new thread.

Cheers.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com