Not able to insert stuff from an input field into a DB.

Discussion in 'PHP' started by qwikad.com, May 28, 2017.

  1. #1
    How do I insert the email_alert from the input into the DB? I tried

    $email_alert = $_POST['email_alert'];

    
    
    $job_alert = $job_name;
    $area_alert = $city_name;
    
    $conn = mysqli_connect($host, $user, $pass,$dbname);  
    if(!$conn){  
      die('Could not connect: '.mysqli_connect_error());  
    }  
    echo 'Connected successfully<br/>';  
    $alerts = "INSERT INTO jobalerts (email_alert, job_alert, area_alert) VALUES ('$email_alert', '$job_alert', '$area_alert')";  
    if(mysqli_query($conn, $alerts)){  
     echo "Record inserted successfully";  
    }else{  
    echo "Could not insert record: ". mysqli_error($conn);  
    }  
    mysqli_close($conn);  
    
    
    Code (markup):
    
    <form method="post" name="job_alerts" action="/index.php">  
      Email Address <span class="error">* </span>: <br><input type="text" name="email_alert" value=""> <input type="submit" name="submit" value="Submit">  
    </form>
    
    Code (markup):
     
    qwikad.com, May 28, 2017 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,893
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #2
    first up, promise me you'll sanitise all those variables
    second shouldn't you be doing variable replacement with mysqli?

    but if you're going to send a complete string do it like this

    $alerts = "INSERT INTO `jobalerts` (`email_alert`, `job_alert`, `area_alert`) VALUES ('{$email_alert}', '{$job_alert}', '{$area_alert}')";
    PHP:
    and finally

    do an echo on $alerts and see what's actually in there. If the email isn't, then step back through the code until you can see the value and then work out how it's getting wiped.
     
    sarahk, May 28, 2017 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #3
    I got it working. That action="/index.php" should be the URL whether the submit form is on.
     
    qwikad.com, May 29, 2017 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #4
    I've got another question. How can I avoid inserting duplicate entries?

    I know in my DB I can set the email_alert as Primary, but that would prevent from entering the same email again. I want the same email to be entered as long as job_alert or area_alert are different from what has been entered.

    
    $alerts = "INSERT INTO jobalerts (email_alert, job_alert, area_alert) VALUES ('$email_alert', '$job_alert', '$area_alert')";
    
    Code (markup):
    I tried INSERT IGNORE, but that didn't do anything.
     
    qwikad.com, May 29, 2017 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,893
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #5
    I'd do a count of how many records there are that match - don't be afraid of adding extra database calls.
     
    sarahk, May 29, 2017 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #6
    Using SELECT? How do you count and mach records? I see a few solutions using SELECT and then inserting based on that but as many things as I tried, it didn't work for me. It was either still inserting the same records or stopped inserting anything.
     
    qwikad.com, May 29, 2017 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,893
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #7
    select count(id)
    from `jobalerts`
    where `email_alert` = '{$email_alert}'
    and `job_alert` = '{$job_alert}'
    and `area_alert` = '{$area_alert}'
     
    sarahk, May 29, 2017 IP
  8. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #8
    What if statements do I use to make the insert do what it's supposed to do?
     
    qwikad.com, May 29, 2017 IP
  9. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #9
    http://lmgtfy.com/?q=mysql+insert+if+not+exists+in+another+table (no, not a spam link :)). Srsly, have you even tried to search for this?
     
    Blank ™, May 29, 2017 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,893
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #10
    run the query
    do a var_dump to see what gets returned
    and write your php around that
    it's so long since I did raw mysql...
     
    sarahk, May 29, 2017 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    Do a SELECT _first_ - and if you get a return of 1 (which means the entry exist) don't insert - if you get 0, then insert.
    So, something like this:
    
    SELECT COUNT(*) FROM jobalerts WHERE email_alert = '$email_alert' AND job_name = '$job_name' AND city_name = '$city_name';
    //although you should use prepared statements, not inserting blind variables into a query
    
    PHP:
    Then use the result of that query to decide whether or not to insert a new entry
     
    PoPSiCLe, May 29, 2017 IP
  12. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #12
    I got it working.

    
        $sql2 = "SELECT email_alert, job_alert, area_alert FROM jobalerts WHERE email_alert='$email_alert' AND job_alert='$job_name' AND area_alert='$city_name'";
        $result = $link->query($sql2);
        if ($result->num_rows > 0) {
    }else{
    $sql = "INSERT INTO jobalerts (email_alert, job_alert, area_alert) VALUES ('$email_alert', '$job_name', '$city_name')";
    
    }
    
    Code (markup):
     
    qwikad.com, May 29, 2017 IP
    PoPSiCLe likes this.
  13. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #13
    Pop I am sanitizing them with $email_alert = mysqli_real_escape_string($link, $_REQUEST['email_alert']); etc.
     
    qwikad.com, May 29, 2017 IP
    sarahk likes this.
  14. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #14
    Fair enough - it's still an outdated method, but it works, mostly :) I would still suggest using prepared statements, but your way works too.
     
    PoPSiCLe, May 30, 2017 IP