-1

I have this table where it handles all the user feedbacks and I have this id column on that table. I'm not really sure what's the purpose of it. Also I didn't see any id in the code.

PS: I just got the code from the internet.

enter image description here

<?php
require_once ('database.php');

if (isset($_POST['send'])) {

$full_name = $_POST['full_name'];
$email = $_POST['email'];
$website = ($_POST['website']);
$message = ($_POST['message']);

{
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insert_query = "INSERT INTO tbl_feedback (full_name, email, website, message)
VALUES (?, ?, ?, ?)";

$insert = $database->prepare($insert_query);
$insert->execute(array($full_name, $email, $website, $message));

echo "<script>alert('Successfully sent!'); window.location='feedback.php'</script>";
}
}
?>
Spoody
  • 2,852
  • 1
  • 26
  • 36
Andre
  • 11
  • 2
  • 3
    please read up on primary keys.. it is probably set on auto increment. Check the table structure – Suraj Rao Jan 15 '18 at 12:40
  • It's common to use auto increment primary keys, they are generated by the DB if you don't put a specific value into it via the INSERT statements. – Nigel Ren Jan 15 '18 at 12:42
  • 1
    I think you accepted the wrong answer... @Gordon Linoff's answer is more related to your question. – Spoody Jan 15 '18 at 12:58

2 Answers2

3

The id column uniquely identifies each row. It is -- no doubt -- the primary key of the table.

SQL tables often have numeric primary keys. They are very convenient for foreign key references. They also, incidentally, show the order of insertion of various rows. Having such primary keys is a good practice in general.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks but can I remove it if I don't really need it? – Andre Jan 15 '18 at 12:41
  • I know it's a good practice tho, I just wanted to know if I can remove it – Andre Jan 15 '18 at 12:41
  • The techical term for it is *surrogate key*. – Philipp Maurer Jan 15 '18 at 12:42
  • 1
    You can but you shouldn't. If you delete this key, you must choose another primary key for the table : email for example – Alex83690 Jan 15 '18 at 12:42
  • 1
    @Andre No, don't remove it if you didn't write the application, other tables may use it as a foreign key and by removing it you will remove the relation between those tables. – Spoody Jan 15 '18 at 12:43
  • @Alex83690 By the way, I have another table in the same database with an email in it. So if I remove the id and make email my primary key. Is that considered a relationship? – Andre Jan 15 '18 at 12:45
  • @MehdiBounya No the program I downloaded comes with only 1 table :) – Andre Jan 15 '18 at 12:45
  • In your second table, you must mark the email as a foreign key. See https://www.w3schools.com/sql/sql_foreignkey.asp – Alex83690 Jan 15 '18 at 12:46
  • @Alex83690 Thanks! That's what I needed. – Andre Jan 15 '18 at 12:47
  • Nice to help you. But you really should not delete the id column with auto increment. See https://stackoverflow.com/questions/1997358/pros-and-cons-of-autoincrement-keys-on-every-table – Alex83690 Jan 15 '18 at 12:49
-1

id of the table may be assigned to AUTO INCREMENT & it is a primary key of the table .

If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.

-reference

so no need to provide ID manually . whenever user inserts data id is automatically generated (i.e +1 of last inserted value's ID)

but in this line you must specify column name also with respect to value to be inserted when you aren't inserting value into all column

$insert_query = "INSERT INTO tbl_feedback (full_name, email, website, message)
VALUES (?, ?, ?, ?)";
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22