Jump to content

[SOLVED] Why is it double inserting


bobahad

Recommended Posts

Hey guys,

Why is this code double inserting in the table... I fill in the form, I click submit and it double inserts.

 

$Link = mysql_connect($host,$user,$password);

@mysql_select_db($DBname) or die( "Unable to select database");

 

$query="INSERT INTO Books (Title, Author , Publisher , ISBN , Year , Price) VALUES ($title,$author,$publisher,$isbn,$year,$price);";

 

if(mysql_db_query($DBname,$query,$Link)){

print("The query was successfully executed!<br>\n");

 

}

else{

print("The query could not be executed!<br>\n");

}

 

 

print($query);

mysql_query($query);

mysql_close($Link);

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/81034-solved-why-is-it-double-inserting/
Share on other sites

Can you guys please check it again, I don't know what I just did wrong.

 

http://welovepluto.com/AddBook.php

 

<BODY>

<h1>Insert Book into Table</h1>

<form name="form1" method="Post" action="AddBook.php">

<b>Title:</b><INPUT TYPE="text" NAME="title"><br>

<b>Author:</b><INPUT TYPE="text" NAME="author"><br>

<b>Publisher:</b><INPUT TYPE="text" NAME="publisher"><br>

<b>ISBN:</b><INPUT TYPE="text" NAME="isbn"><br>

<b>Year:</b><INPUT TYPE="text" NAME="year"><br>

<b>Price:</b><INPUT TYPE="text" NAME="price"><br>

<INPUT TYPE="submit" Value="Insert"><br><INPUT TYPE="reset">

</form>

 

  <?php

//Set Variables for the database access:

 

$host="***";

$user="***";

$password="***";

$DBname="***";

$TableName="Books";

 

$Link = mysql_connect($host,$user,$password);

@mysql_select_db($DBname) or die( "Unable to select database");

 

$query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) VALUES ($title,$author,$publisher,$isbn,$year,$price);";

 

 

 

if(mysql_db_query($DBname,$query,$Link)){

print("The book was added successfully!<br>\n");

 

}

else{

print("The book was not added!<br>\n");

}

 

 

print($query);

 

mysql_close($Link);

?>

 

</BODY>

Do this

 

<BODY>
<h1>Insert Book into Table</h1>
<form name="form1" method="Post" action="AddBook.php">
Title:<INPUT TYPE="text" NAME="title">

Author:<INPUT TYPE="text" NAME="author">

Publisher:<INPUT TYPE="text" NAME="publisher">

ISBN:<INPUT TYPE="text" NAME="isbn">

Year:<INPUT TYPE="text" NAME="year">

Price:<INPUT TYPE="text" NAME="price">

<INPUT TYPE="submit" name="submit" Value="Insert">
<INPUT TYPE="reset">
</form>

<?php
//Set Variables for the database access:

$host="***";
$user="***";
$password="***";
$DBname="***";
$TableName="Books";

$Link = mysql_connect($host,$user,$password);
@mysql_select_db($DBname) or die( "Unable to select database");

if (isset($_POST['submit'])){
    $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) 
    VALUES ($title,$author,$publisher,$isbn,$year,$price);";
   
   if(mysql_db_query($DBname,$query,$Link)){
       print("The book was added successfully!
         \n");
      
    } else{
    
       print("The book was not added!
         \n");
       }
   
   
   print($query);
   
   mysql_close($Link);
}
?>

</BODY>

oh awesome, now it won't show the book added successfully till after I click the button, that's cool :)

Thanks man.

The only problem now is that it only adds numbers even though all the fields are set to varchar(50)... weird.

 

www.welovepluto.com/AddBook.php

Try changing this line

    $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) 
    VALUES ($title,$author,$publisher,$isbn,$year,$price);";

 

To

    $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) 
    VALUES ('$title','$author','$publisher','$isbn','$year','$price')";

 

So it's adding numbers to the database instead of what you typed in?

Yeah the single quotation fixed it, Thanks alot man :)

Much appreciated.

Just for knowledge purpose though the  if (isset($_POST['submit']))

means that run this if statement if a button called 'submit' is clicked ?

Does that mean I can create buttons called Edit, delete and so on and use that same isset ?

 

Thanks again :)

Yeah the single quotation fixed it, Thanks alot man :)

Much appreciated.

Just for knowledge purpose though the  if (isset($_POST['submit']))

means that run this if statement if a button called 'submit' is clicked ?

Does that mean I can create buttons called Edit, delete and so on and use that same isset ?

 

Thanks again :)

 

Yes, that is exactly what it does. You can use the same method for an edit button or whatever else =]

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.