0

I keep getting this error displayed in a new page

Error: Duplicate entry 'company' for key 'cname'

how can i display this in a new header

Heres my code:

$con=mysqli_connect("localhost","root","password","mydb");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO profile  (cname, cobusiness, cphone,crep,cdescription)
VALUES
('$_POST[cname]','$_POST[cobusiness]','$_POST[cphone]','$_POST[crep]','$_POST[cdescription]')";

if (!mysqli_query($con,$sql)) {
    die('Error: ' . mysqli_error($con));
    header("Location: home.php?error");
} else {
    header("Location: home.php?success");
    exit;
}

mysqli_close($con);

I have added a trigger in mysql to check for duplicate entries and just need to figure out how to prevent this duplicate entry error display in a new page by default.This way i can display it in a header on the current page like i have already done with the other headers in the code.

RileyManda
  • 2,536
  • 25
  • 30
  • Is `cname` unique in your database settings ? – jean-max Jan 04 '17 at 10:17
  • yes it is.I have a primary key as the id and the unique key as cname.The code is in insert.php,and the form is using insert.php as its action reference when form is submitted – RileyManda Jan 04 '17 at 10:18
  • 1
    You want to display this exact error in your `home.php` file ? Or just a generic message ? – jean-max Jan 04 '17 at 10:21
  • yes,the error should display in a header in home.php and not in a new page.its displaying in insert.php instead of the current page,home.php – RileyManda Jan 04 '17 at 10:22
  • If you have error you make `header` with a specific parameter in your URL like `home.php?status=error` and in your `home.php` file you make `$_GET['status']` and check for the value no ? – jean-max Jan 04 '17 at 10:25
  • I tried this: header("Location: home.php?error"); in insert.php,then in home i did the following:
    Duplicate.Try again
    But its still displaying the error in Insert.php as new page
    – RileyManda Jan 04 '17 at 10:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132276/discussion-between-riley-and-jean-maxime-bouloc). – RileyManda Jan 04 '17 at 10:32

1 Answers1

-1

With valuable assistance from Jean-maxime Bouloc This is the solution: Insert.php

$sql = "INSERT INTO profile  (cname, cobusiness, cphone,crep,cdescription)
VALUES
('$_POST[cname]','$_POST[cobusiness]','$_POST[cphone]','$_POST[crep]','$_POST[cdescription]')";

if (!mysqli_query($con,$sql)) {
    header("Location: home.php?error");
} 
else {
    header("Location: home.php?success");
    exit;
}

mysqli_close($con);
Matthew Wilcoxson
  • 3,432
  • 1
  • 43
  • 48
RileyManda
  • 2,536
  • 25
  • 30
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 04 '17 at 14:02
  • how do i secure the form?any ideas? – RileyManda Jan 04 '17 at 14:16