Why am I getting an error message?

Discussion in 'PHP' started by Jen-, Dec 29, 2006.

  1. #1
    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)
    ?>
     
    Jen-, Dec 29, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    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.
     
    nico_swd, Dec 29, 2006 IP
  3. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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'>
     
    Jen-, Dec 29, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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.
     
    nico_swd, Dec 30, 2006 IP
  5. Jen-

    Jen- Peon

    Messages:
    151
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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-, Dec 30, 2006 IP
  6. RobPinnacle

    RobPinnacle Active Member

    Messages:
    423
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Jen,

    Have organizations skills now as your learning is extremely important. If you don't you'll never become a good programmer. :)
     
    RobPinnacle, Dec 30, 2006 IP