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):
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.
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.
I'd do a count of how many records there are that match - don't be afraid of adding extra database calls.
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.
select count(id) from `jobalerts` where `email_alert` = '{$email_alert}' and `job_alert` = '{$job_alert}' and `area_alert` = '{$area_alert}'
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?
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...
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
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):
Pop I am sanitizing them with $email_alert = mysqli_real_escape_string($link, $_REQUEST['email_alert']); etc.
Fair enough - it's still an outdated method, but it works, mostly I would still suggest using prepared statements, but your way works too.