Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7

    Need help with insert form


    Hi,

    I have the insert form below which works just fine except for the if else condition.
    If the name already exists, i'd like it to echo name already exists.
    I know that I am doing something wrong. Can someone help?
    Thanks much !


    PHP Code:
    if(isset($_REQUEST['add_group']))
    {




    $result_content = mysql_query ("SELECT * FROM $table_group WHERE first_name = '" . $first_name. "' limit 1 ");

    if ( mysql_num_rows($result_content)==0){

    $sql="INSERT INTO $table_group (first_name, last_name)VALUES('$first_name', '$last_name')"; }else { echo  "Name already exists"; }

    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='insert.php'>Back to main page</a>";
    }

    else {
    echo "ERROR";
    }
    }
    // close connection
    mysql_close();


    ?>

    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
    <td><form name="form1" method="post" action="insertTest.php">
      
    <?php

        
    if (isset($data_content["first_name"]) and
                
    $data_content["first_name"] != '' and
                !isset(
    $_POST['first_name'])) {
            
    $first_name $data_content["first_name"];
        } elseif (isset(
    $_POST['first_name'])) {
            
    $first_name $_POST['first_name'];
        } else {
            
    $first_name '';
        } 
        
    $first_name htmlentities(stripslashes($first_name), ENT_QUOTES$character_set);

        if (isset(
    $data_content["last_name"]) and
                
    $data_content["last_name"] != '' and
                !isset(
    $_POST['last_name'])) {
            
    $last_name $data_content["last_name"];
        } elseif (isset(
    $_POST['last_name'])) {
            
    $last_name $_POST['last_name'];
        } else {
            
    $last_name '';
        } 
        
    $last_name htmlentities(stripslashes($last_name), ENT_QUOTES$character_set);
    ?>

    <table width="100%" border="0" cellspacing="1" cellpadding="3">
    <tr>
    <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
    </tr>
    <tr>
    <td width="71">First name</td>
    <td width="6">:</td>
    <td width="301"><input name="first_name" type="text" id="first_name" value="<?php echo $first_name ?>"></td>
    </tr>
    <tr>
    <td>Lastname</td>
    <td>:</td>
    <td><input name="last_name" type="text" id="last_name" value="<?php echo $last_name ?>"></td>
    </tr>

    <tr>
    <td colspan="3" align="center"><input type="submit" name="add_group" value="Submit"></td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,992
    Rep Power
    9397
    Well there's one thing you can fix: edit your post and delete the code that's there, then copy/paste your code back into the textbox, select it all, and then hit the PHP code button. Because right now everything is on one line and that's impossible to read.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7
    Originally Posted by requinix
    Well there's one thing you can fix: edit your post and delete the code that's there, then copy/paste your code back into the textbox, select it all, and then hit the PHP code button. Because right now everything is on one line and that's impossible to read.
    Thanks for that Requinix.

    I was trying to figure out how to now have it all in one line like that!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    PHP Code:
    <?php
      
    if(isset($_REQUEST['add_group'])) {
        
    // Get values from form
        
    $name $_POST['first_name'];
        
    $lastname $_POST['last_name'];
        
    $result_content mysql_query ("SELECT * FROM $table_group WHERE first_name = '" $first_name"' limit 1 ");
        if ( 
    mysql_num_rows($result_content) == 0) {
          
    $sql "INSERT INTO $table_group (first_name, last_name) VALUES ('$first_name', '$last_name')";
        } else {
          echo 
    "Name already exists";
        }
        
    $result mysql_query($sql);
        
    // if successfully insert data into database, displays message "Successful".
        
    if($result) {
          echo 
    "Successful<br />\n
          <a href='insert.php'>Back to main page</a>"
    ;
        } else {
          echo 
    "ERROR";
        }
      }
      
    // close connection
      
    mysql_close();
    ?>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
      <tr>
        <td>
          <form name="form1" method="post" action="insertTest.php">
            <?php
              
    if(isset($data_content["first_name"]) && $data_content["first_name"] != '' && !isset($_POST['first_name'])) {
                
    $first_name $data_content["first_name"];
              } elseif(isset(
    $_POST['first_name'])) {
                
    $first_name $_POST['first_name'];
              } else {
                
    $first_name '';
              }
              
    $first_name htmlentities(stripslashes($first_name), ENT_QUOTES$character_set);
              if(isset(
    $data_content["last_name"]) && $data_content["last_name"] != '' && !isset($_POST['last_name'])) {
                
    $last_name $data_content["last_name"];
              } elseif(isset(
    $_POST['last_name'])) {
                
    $last_name $_POST['last_name'];
              } else {
                
    $last_name '';
              }
              
    $last_name htmlentities(stripslashes($last_name), ENT_QUOTES$character_set);
            
    ?>
            <table width="100%" border="0" cellspacing="1" cellpadding="3">
              <tr>
                <td colspan="3">
                  <strong>Insert Data Into mySQL Database </strong>
                </td>
              </tr>
              <tr>
                <td width="71">First name</td>
                <td width="6">:</td>
                <td width="301"><input name="first_name" type="text" id="first_name" value="<?php echo $first_name ?>"></td>
              </tr>
              <tr>
                <td>Lastname</td>
                <td>:</td>
                <td><input name="last_name" type="text" id="last_name" value="<?php echo $last_name ?>"></td>
              </tr>
              <tr>
                <td colspan="3" align="center"><input type="submit" name="add_group" value="Submit"></td>
              </tr>
            </table>
          </form>
        </td>
      </tr>
    </table>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7
    Thanks Triple,

    But that generated no new results. it is still duplicating inserts.

    I removed this , btw:

    $name = $_POST['first_name'];
    $lastname = $_POST['last_name'];

    It appears to be extraneous codes.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    You are referencing incorrectly. You assign your POST code to $name and $lastname, but reference them as $first_name $last_name
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7
    Originally Posted by Triple_Nothing
    You are referencing incorrectly. You assign your POST code to $name and $lastname, but reference them as $first_name $last_name
    Yup, I saw that. I corrected it and saw that it seemed to not change anything.

    So I removed it all together. They were extraneous.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Echo $result_content and see what that gives you. If you feel its printout is was you want, run that query yourself and see if you get a row back.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7
    I'm just learning PHP still.
    I don't understand what you mean.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    284
    Rep Power
    255
    Originally Posted by Triple_Nothing
    PHP Code:
        $name $_POST['first_name'];
        
    $lastname $_POST['last_name'];
        
    $result_content mysql_query ("SELECT * FROM $table_group WHERE first_name = '" $first_name"' limit 1 ");
        if ( 
    mysql_num_rows($result_content) == 0) {
          
    $sql "INSERT INTO $table_group (first_name, last_name) VALUES ('$first_name', '$last_name')";
        } else {
          echo 
    "Name already exists";
        }
        
    $result mysql_query($sql); 
    That is a mysql injection waiting to happen.
    - The Wise Guy
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Add this to your code, just above the if() command:

    PHP Code:
    echo "Query: SELECT * FROM $table_group WHERE first_name = '" $first_name"' limit 1 "
    Then, copy it from your page and post it here. If you're familiar with PHPMyAdmin, you can run the code there to see if you get any lines.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7
    Well, I added it:

    PHP Code:
    echo "Query: SELECT * FROM $table_group WHERE first_name = '" $first_name"' limit 1 ";  
      
    if(isset(
    $_REQUEST['add_group']))
    {
    ........ 
    Didn't do anything.

    The form continues to insert as usual duplicated entries,
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    This isnt going to change how anything works on your page. It will only print out an echo with the full definition of the line. We want to see if this line is built as you wish it to be. If it is not, then that's your whole problem right there. So, make sure you don't have a redirect, and this line prints out on your page, n post it here.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    51
    Rep Power
    7

    Solved


    Hi Tripple_,
    the first solution you sent was correct. It wasn't working because of conflicts with other scripts running with the include statements. Once, I stripped the codes down to its bare, it worked. Here it is. Thanks for your help.

    PHP Code:
     if(isset($_REQUEST['add_group'])) {

     // Get values from form
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name']; 

    $result_content = mysql_query ("SELECT * FROM $table_group WHERE first_name = '" . $first_name. "' limit 1 ");

         if ( mysql_num_rows($result_content) == 0) {
          $sql = "INSERT INTO $table_group (first_name, last_name) VALUES ('$first_name', '$last_name')";
        } else {
          echo "Name already exists";
        }
        $result = mysql_query($sql);
        // if successfully insert data into database, displays message "Successful".
        if($result) {
          echo "Successful<br />\n
          <a href='insert.php'>Back to main page</a>";
        } else {
          echo "ERROR";
        }
      }
      // close connection
      mysql_close();
    ?>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
      <tr>
        <td>
          <form name="form1" method="post" action="test.php">
            <?php
              
    if(isset($data_content["first_name"]) && $data_content["first_name"] != '' && !isset($_POST['first_name'])) {
                
    $first_name $data_content["first_name"];
              } elseif(isset(
    $_POST['first_name'])) {
                
    $first_name $_POST['first_name'];
              } else {
                
    $first_name '';
              }
              
    $first_name htmlentities(stripslashes($first_name), ENT_QUOTES$character_set);
              if(isset(
    $data_content["last_name"]) && $data_content["last_name"] != '' && !isset($_POST['last_name'])) {
                
    $last_name $data_content["last_name"];
              } elseif(isset(
    $_POST['last_name'])) {
                
    $last_name $_POST['last_name'];
              } else {
                
    $last_name '';
              }
              
    $last_name htmlentities(stripslashes($last_name), ENT_QUOTES$character_set);
            
    ?>
            <table width="100%" border="0" cellspacing="1" cellpadding="3">
              <tr>
                <td colspan="3">
                  <strong>Insert Data Into mySQL Database </strong>
                </td>
              </tr>
              <tr>
                <td width="71">First name</td>
                <td width="6">:</td>
                <td width="301"><input name="first_name" type="text" id="first_name" value="<?php echo $first_name ?>"></td>
              </tr>
              <tr>
                <td>Lastname</td>
                <td>:</td>
                <td><input name="last_name" type="text" id="last_name" value="<?php echo $last_name ?>"></td>
              </tr>
              <tr>
                <td colspan="3" align="center"><input type="submit" name="add_group" value="Submit"></td>
              </tr>
            </table>
          </form>
        </td>
      </tr>
    </table>
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Pakistan
    Posts
    3
    Rep Power
    0

    Cool


    Originally Posted by eropsy
    Hi,

    I have the insert form below which works just fine except for the if else condition.
    If the name already exists, i'd like it to echo name already exists.
    I know that I am doing something wrong. Can someone help?
    Thanks much !


    PHP Code:
    if(isset($_REQUEST['add_group']))
    {




    $result_content = mysql_query ("SELECT * FROM $table_group WHERE first_name = '" . $first_name. "' limit 1 ");

    if ( mysql_num_rows($result_content)==0){

    $sql="INSERT INTO $table_group (first_name, last_name)VALUES('$first_name', '$last_name')"; }else { echo  "Name already exists"; }

    $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='insert.php'>Back to main page</a>";
    }

    else {
    echo "ERROR";
    }
    }
    // close connection
    mysql_close();


    ?>

    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
    <tr>
    <td><form name="form1" method="post" action="insertTest.php">
      
    <?php

        
    if (isset($data_content["first_name"]) and
                
    $data_content["first_name"] != '' and
                !isset(
    $_POST['first_name'])) {
            
    $first_name $data_content["first_name"];
        } elseif (isset(
    $_POST['first_name'])) {
            
    $first_name $_POST['first_name'];
        } else {
            
    $first_name '';
        } 
        
    $first_name htmlentities(stripslashes($first_name), ENT_QUOTES$character_set);

        if (isset(
    $data_content["last_name"]) and
                
    $data_content["last_name"] != '' and
                !isset(
    $_POST['last_name'])) {
            
    $last_name $data_content["last_name"];
        } elseif (isset(
    $_POST['last_name'])) {
            
    $last_name $_POST['last_name'];
        } else {
            
    $last_name '';
        } 
        
    $last_name htmlentities(stripslashes($last_name), ENT_QUOTES$character_set);
    ?>

    <table width="100%" border="0" cellspacing="1" cellpadding="3">
    <tr>
    <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
    </tr>
    <tr>
    <td width="71">First name</td>
    <td width="6">:</td>
    <td width="301"><input name="first_name" type="text" id="first_name" value="<?php echo $first_name ?>"></td>
    </tr>
    <tr>
    <td>Lastname</td>
    <td>:</td>
    <td><input name="last_name" type="text" id="last_name" value="<?php echo $last_name ?>"></td>
    </tr>

    <tr>
    <td colspan="3" align="center"><input type="submit" name="add_group" value="Submit"></td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    You just need to Replace
    PHP Code:
    if ( mysql_num_rows($result_content)==0
    With this
    PHP Code:
    if ( mysql_num_rows($result_content)>0
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo