Jump to content

[SOLVED] Why is blank data being inserted into my database?


nscherneck

Recommended Posts

<form action="" method="get">
<p>Name: <input name="Name" type="text"></p>
<p>Rate: <input name="Rate" type="text"></p>
<input name="" type="submit" onClick="<?php 
$put = "INSERT INTO Employees VALUES ('','$Name','$Rate')";
mysql_query($put, $connection); ?>">
</form>

 

this form inserts data into my mysql database, but everytime the page is refreshed blank data gets inserted.  my thought is that the PHP code would only execute "onClick" of the button.  am i wrong?

Link to comment
Share on other sites

Hi..

    use following code for this:

<!-- ASSUME THIS FILENAME IS "test.php"--->
<form action="" method="post">
<p>Name: <input name="Name" type="text"></p>
<p>Rate: <input name="Rate" type="text"></p>
<input name="submit" type="submit" value="submit">
</form>

<?php 
if(issset($_POST['submit']) && $_POST['submit'] == 'submit')
$put = "INSERT INTO Employees VALUES ('','$Name','$Rate')";
mysql_query($put, $connection); 
header("location:test.php");
exit;
?>

 

Regards,

vijay

Link to comment
Share on other sites

Get method appends the values what ur submitting in the form to the URL so its not secure as user can append any data(google SQL injection) there if u r not doing proper validation apart from that if u use get method the end user can easily bookmark that one. Post method doesn't show the values in URL.

why I suggested post method is u r using those variables in insert statement.

Just imagine the user has bookmarked the page and each time he visited it insert duplicate values if u r not taken proper care.

Link to comment
Share on other sites

the reason you were getting empty entries is because the PHP code was being executed EVERY TIME.  it is always processed BEFORE the HTML.  it cannot be used as a client-side language (note that an onclick event is a client event, not a server event).  vijay's code should amend that, but i figured i'd explain why it was inserting empty rows.

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.