Jump to content

[SOLVED] Mysql query, two times?


mwkemo

Recommended Posts

Hello to all,

 

I'm new to php coding and i like it alot. I need help with some noob stuff.

 

1. Having problem with mysql query after submiting HTML form. It's allways execuding two times, that way i have two identical data in table which i do not wont.

 

HTML code:

<form name="kluboviForm" method="post" action="kluboviAction.php">
<input name="predsjednik" type="text"/>
<input name="email" type="text"/>
<input name="kontakt" type="text"/>
<input name="sjedisteKluba" type="text"/>
<input name="imeKluba" type="text"/>
<input type="Submit"/>
</form>

 

PHP code (kluboviAction.php):

<?php
mysql_connect(localhost,root,'');
mysql_select_db(ljestvica) or die( "Unable to select database");
mysql_query("INSERT INTO klubovi VALUES ('','a','b','c','d','e')");
mysql_close();
?>

 

After execuding the submit button on form i get this error in google chrome:

 

Error 101 (net::ERR_CONNECTION_RESET): Unknown error.

 

But the query is execudet and table is filed with two identical informations.

 

2. Is there a way to put php function inside action form? I wont to execude php function after someone submits the form. I wont to delete kluboviAction.php file and use function to put the information in to database without the need to use second file. If there is anothere way to do that please let me know.

 

3. How to redirect URL after execuding submit button to not show message above?

 

Any help would be appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/178122-solved-mysql-query-two-times/
Share on other sites

As far as being executed twice, I'm not sure why that's happening, but you could use PHP_SELF and post the form to itself, like:

 

<?php
if(isset($_POST['email'])
{
mysql_connect(localhost,root,'');
mysql_select_db(ljestvica) or die( "Unable to select database");
mysql_query("INSERT INTO klubovi VALUES ('','a','b','c','d','e')");
mysql_close();
}
?>
<html>
</body>
<form name="kluboviForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="predsjednik" type="text"/>
<input name="email" type="text"/>
<input name="kontakt" type="text"/>
<input name="sjedisteKluba" type="text"/>
<input name="imeKluba" type="text"/>
<input type="Submit"/>
</form>
</body>
</html>

Thanks for fast replays,

 

I'm using html code inside php so i need to use echo "<html code>", everything is working perfectly after removing "<?php echo $_SERVER['PHP_SELF']; ?>" from action. Even double quotes are fixed, dont know how but it works:-)

 

Still having problem with redirect link. After submiting form I'm getting white page with some error message. Is there a way to redirect to index.php after submiting form?

 

If I use more then one php tag inside script, and for ex. if I put variables in the first tag, are those variables still valid for other tags? Like this:

<?php
$username="root";
$password="";
$database="ljestvica";
$host="localhost";
?>

<html>
</html>

<?php
if(isset($_POST['email'])) {
mysql_connect($host,$username,$password);
?>

 

This did not work for me, I need to put variables after "if(isset($_POST['email']))", after that script works.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.