Hi, I'm trying to send some php based on whether or not the submit button is pushed. Everything is working but I am getting this error message. Submit successful! Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/content/M/o/o/MoonBaby/html/codescript/addingscript.php on line 16 Line 16 is the very last line. I put a beginning bracket right after the "if" statement at the beginning to execute the code after the submit button is pushed. The only way it wants to send is if the end bracket is right before the echo at the end. But there is something about this the php doesn't like, thus the error message. Can anyone see what I should do differently to remove the error? If I remove this mysql_close($con) then it works without an error. Is it important to tell the database to close again after the fields are entered...or does it close anyway all on its own? Thank you very much. <?php if ($_POST['submitbutton'] == "Send Request") { $con = mysql_connect("mysql","test","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("llogin", $con); $username = $_POST['username'] ; $sname = $_POST['scriptname'] ; $spurl = $_POST['scripturl'] ; $sql="INSERT INTO addscript (username,scriptname,scripturl) VALUES ('" . $_POST['username']. "', '" . $_POST['scriptname'] ."','" . $_POST['scripturl'] . "')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }} echo "Submit successful!";mysql_close($con) ?>
The brackets are wrong. It always attempts to close the conecction when the page is called, but it ONLY establishes the connection when you hit submit. Out the error line before the last two brackets.
Thank you but not sure what you mean. I changed the end of the above to this instead. if (!mysql_query($sql,$con)){}} echo "Submit successful!";mysql_close($con) ?> Still getting the same error message. Also I notice if I put any name for the submit button. <input type='submit' name='submit' value='Submit'> The form still sends, when it is only supposed to send if the submit button is named 'Send Request' like this <input type='submit' name='Send Request' value='Submit'>
Give this a try. <?php if ($_POST['submitbutton'] == "Send Request") { $con = mysql_connect("mysql","test","password") OR die('Could not connect: ' . mysql_error()); mysql_select_db("llogin", $con) OR die(mysql_error()); $username = mysql_real_escape_string($_POST['username']); $sname = mysql_real_escape_string($_POST['scriptname']); $spurl = mysql_real_escape_string($_POST['scripturl']); $sql="INSERT INTO addscript (username,scriptname,scripturl) VALUES ('" . $username. "', '" . $sname ."','" . $spurl . "')"; @mysql_query($sql, $con) OR die(mysql_error()); echo "Submit successful!"; mysql_close($con); } ?> PHP: I'd suggest writing your code a little bit cleaner since it's easier to read and understand then.
Thank you very much! I probably can't write it clean yet, because I'm not as good at it. It still isn't working with the submit at the top but I'm going to see if I can do what I need without it. One thing though, can you explain to me please why the posts need to be set up as escape strings instead? I know it doesn't make things much safer but I don't understand how it removes strange characters so that it doesn't confuse the database functions.
Jen, Have organizations skills now as your learning is extremely important. If you don't you'll never become a good programmer.