Jump to content

Recommended Posts

Please advise. I am still a newbie.

 

I have this form where everytime I open it on my browser, it enters a blank row on mysql. In addition, after i enter data using the form and if i were to refresh the page, it duplicates the data on mysql. I can't figure out what's wrong.

 

Here is the code:

 

<?php @include("connection.php");?>

<?php 
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$boolean = $_POST['boolean'];
$datetime = $_POST['datetime'];
$dropdown = $_POST['dropdown'];
$db_table = "test";
$sql = "INSERT INTO $db_table (FirstName,LastName,boolean,datetime,dropdown) VALUES ('$firstname','$lastname','$boolean','$datetime','$dropdown')";

if($result = mysql_query($sql, $connection)){
echo '<h1>Saved</h1><br>';
}
else {
	echo "Error".mysql_error();
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>

<body>
<?php 
if(isset($_POST['submit'])){
if(isset($_POST['FirstName']) && $_POST['FirstName']!="" && isset($_POST['LastName']) && $_POST['LastName']!="" && isset($_POST['boolean']) && $_POST['boolean']!="" && isset($_POST['datetime']) && $_POST['datetime']!="" && isset($_POST['dropdown']) && $_POST['dropdown']!=""){
 } 
}


?>
     
<form action="<?php $_SERVER['PHP_SELF']; ?>" method='post' >
<fieldset>
<legend>test</legend>
<table width="22%" border="0" cellpadding="3">
    <tr>
      <td width="73%">First Name:</td>
      <td width="27%"><input type='text' size='12' maxlength="25" name="FirstName"></td>
    </tr>   
    <tr>
      <td>Last Name:</td>
      <td><input type="text" size="12" maxlength="25" name="LastName"></td>
    </tr>
    <tr>
      <td>ID:</td>
      <td><input type="text" size="12" maxlength="25" name="id"></td>
    </tr>
    <tr>
      <td>Boolean:</td>
      <td><input type="checkbox" value="1" name="boolean"></td>
    </tr>
    <tr>
      <td>Date-Time:</td>
      <td><input type="datetime" size="12" maxlength="25" name="datetime"></td>
    </tr>
    <tr>
      <td>Drop-Down:</td>
      <td><Select name="dropdown">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option></Select>
      </td>
    </tr>
</table>
</fieldset>
<input type="submit" name="submit" value="Save"><input type="reset" name="Cancel "value="Cancel">
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/240537-empty-row-and-duplicate-data/
Share on other sites

I have this form where everytime I open it on my browser, it enters a blank row on mysql.

This is b/c your first block of code will always be executed.  You need to move the if(isset($_POST['submit'])){ to the top.  It doesn't do anything where you have it now.

I have this form where everytime I open it on my browser, it enters a blank row on mysql.

This is b/c your first block of code will always be executed.  You need to move the if(isset($_POST['submit'])){ to the top.  It doesn't do anything where you have it now.

 

 

Ok. I did that but it still does the same thing. So nothing has changed yet.

<?php if(isset($_POST['submit'])){
if(isset($_POST['FirstName']) && $_POST['FirstName']!="" && isset($_POST['LastName']) && $_POST['LastName']!="" && isset($_POST['boolean']) && $_POST['boolean']!="" && isset($_POST['datetime']) && $_POST['datetime']!="" && isset($_POST['dropdown']) && $_POST['dropdown']!=""){
 }
}
?>

<?php @include("connection.php");?>

<?php 
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$boolean = $_POST['boolean'];
$datetime = $_POST['datetime'];
$dropdown = $_POST['dropdown'];
$db_table = "test";
$sql = "INSERT INTO $db_table (FirstName,LastName,boolean,datetime,dropdown) VALUES ('$firstname','$lastname','$boolean','$datetime','$dropdown')";

if($result = mysql_query($sql, $connection)){
echo '<h1>Saved</h1><br>';
}
else {
	echo "Error".mysql_error();
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>

<body>     
<form action="<?php $_SERVER['PHP_SELF']; ?>" method='post' >
<fieldset>
<legend>test</legend>
<table width="22%" border="0" cellpadding="3">
    <tr>
      <td width="73%">First Name:</td>
      <td width="27%"><input type='text' size='12' maxlength="25" name="FirstName"></td>
    </tr>   
    <tr>
      <td>Last Name:</td>
      <td><input type="text" size="12" maxlength="25" name="LastName"></td>
    </tr>
    <tr>
      <td>ID:</td>
      <td><input type="text" size="12" maxlength="25" name="id"></td>
    </tr>
    <tr>
      <td>Boolean:</td>
      <td><input type="checkbox" value="1" name="boolean"></td>
    </tr>
    <tr>
      <td>Date-Time:</td>
      <td><input type="datetime" size="12" maxlength="25" name="datetime"></td>
    </tr>
    <tr>
      <td>Drop-Down:</td>
      <td><Select name="dropdown">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option></Select>
      </td>
    </tr>
</table>
</fieldset>
<input type="submit" name="submit" value="Save"><input type="reset" name="Cancel "value="Cancel">
</form>
</body>
</html>

I meant something like this:

//When the user hits the "save" button
if(isset($_POST['submit']))
{  
   //Checks to see if any values aren't set or are blank
   if(isset($_POST['FirstName']) && $_POST['FirstName']!="" && isset($_POST['LastName']) && $_POST['LastName']!="" && isset($_POST['boolean']) && $_POST['boolean']!="" && isset($_POST['datetime']) && $_POST['datetime']!="" && isset($_POST['dropdown']) && $_POST['dropdown']!="")
   {
      $firstname = $_POST['FirstName'];
      $lastname = $_POST['LastName'];
      $boolean = $_POST['boolean'];
      $datetime = $_POST['datetime'];
      $dropdown = $_POST['dropdown'];
      $db_table = "test";
      $sql = "INSERT INTO $db_table (FirstName,LastName,boolean,datetime,dropdown) VALUES ('$firstname','$lastname','$boolean','$datetime','$dropdown')";

      if($result = mysql_query($sql, $connection))
      {
         echo 'Saved
';
      }
      else
      {
         echo "Error".mysql_error();
      }	 
   }
}
?>






test


     
</pre>
<form action="<?php%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="'post'">

test

    
      First Name:
      
       
    
      Last Name:
      
    
    
      ID:
      
    
    
      Boolean:
      
    
    
      Date-Time:
      
    
    
      Drop-Down:
      
      One
      Two
      Three
      
    



</form>
<b

 

NOTE: You need to escape your POST variables with - mysql_real_escape_string.

Sorry for misunderstanding and thank you very much. That did it.

 

But now i have realized that i guess i am using the wrong code. Because, it doesn't save the data if i leave any field blank. The form needs to be flexible when I enter data. For example, i would enter the first name and the last name, but not the date. And, it still needs to not to enter blank row when i open it up on the browser. Any help on that?

But now i have realized that i guess i am using the wrong code. Because, it doesn't save the data if i leave any field blank.

Yes.  This IF block will only execute if all of these conditions are true (all input fields are NOT blank and are set):

if(isset($_POST['FirstName']) && $_POST['FirstName']!="" && isset($_POST['LastName']) && $_POST['LastName']!="" && isset($_POST['boolean']) && $_POST['boolean']!="" && isset($_POST['datetime']) && $_POST['datetime']!="" && isset($_POST['dropdown']) && $_POST['dropdown']!="")

If you don't care that they are blank then you can take out the !="" conditions for the desired fields.  You can also implement a more user friendly error checking to tell the user which fields they left blank.

 

And, it still needs to not to enter blank row when i open it up on the browser. Any help on that?

The if(isset($_POST['submit'])) should prevent that from happening, as in, the SQL related code should only execute when the 'Save' button is hit.  Make sure you clear your database from the blank entries to ensure you're not seeing previous entries.

If you don't care that they are blank then you can take out the !="" conditions for the desired fields.

 

 

Thanks a lot! I would have never guessed or at least i would have spent days on this. And I have decided that it wouldn't hurt anything if i take out the whole code below.

 

if(isset($_POST['FirstName']) && $_POST['FirstName']!="" && isset($_POST['LastName']) && $_POST['LastName']!="" && isset($_POST['boolean']) && $_POST['boolean']!="" && isset($_POST['datetime']) && $_POST['datetime']!="" && isset($_POST['dropdown']) && $_POST['dropdown']!="")

 

It feels like it is repetitive. But again, i am not sure what i would be doing by taking the whole thing out.

 

It feels like it is repetitive. But again, i am not sure what i would be doing by taking the whole thing out.

It would mean if the user hits submit without filling in any input fields, the query will still execute and a blank entry will appear in your database, that's up to you.  And again, you should be preventing SQL injections with at least the mysql_real_escape_string method. i.e.

      $firstname = mysql_real_escape_string($_POST['FirstName']);
      $lastname = mysql_real_escape_string($_POST['LastName']);
      $boolean = mysql_real_escape_string($_POST['boolean']);
      $datetime = mysql_real_escape_string($_POST['datetime']);
      $dropdown = mysql_real_escape_string($_POST['dropdown']);

And again, you should be preventing SQL injections with at least the mysql_real_escape_string() method. i.e.

Code: [select]

      $firstname = mysql_real_escape_string($_POST['FirstName']);

      $lastname = mysql_real_escape_string($_POST['LastName']);

      $boolean = mysql_real_escape_string($_POST['boolean']);

      $datetime = mysql_real_escape_string($_POST['datetime']);

      $dropdown = mysql_real_escape_string($_POST['dropdown']);

 

I took that advice of yours. Thanks again. This was great help. I will mark this topic as solved.

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.