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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.