1

This works with delete:

  $stmt = $con->prepare("DELETE FROM employees WHERE id = ? LIMIT 1"); 

But this same logic does not work with insert:

$stmt = $con->prepare("INSERT INTO employees (first_name, last_name, position, date, updated) VALUES (?, ?, ?, ?, ?) LIMIT 1") ;

The error is just a basic one, looks like that is returns false: Uncaught Error: Call to a member function bind_param() on boolean

This article's example Using LIMIT 1 in MySQL using INSERT INTO .. SELECT ... LIMIT 1 would be nice with an actual example (if it is even the right way).

The only other article I could find, Use "LIMIT" in a MySQL "INSERT"?, did not seem to have a definite answer.

And yes, putting a Limit 1 would be useful. I realized with my database if you click the save button 2-3 times it will insert the data 2 or 3 times. As a test, I clicked the save button ten times and it entered it in ten tinmes.

Thanks.

  • 1
    It sounds like what you really want is a `UNIQUE KEY`. – Patrick Q Mar 14 '18 at 18:38
  • Not sure what you mean by that but I suppose I could make a loop making sure it is only clicked one. Actually that might be a thing now that I think about it – user9449525 Mar 14 '18 at 18:42
  • You seem to not want the same person/employee inserted multiple times, right? So what you really want is either do a `SELECT` first to see if a matching row already exists, and if so, don't insert, _or_ you want to put a `UNIQUE KEY` on your table and either `INSERT IGNORE` (there are arguments to be made against that) or catch and handle the resulting exception on duplicates. – Patrick Q Mar 14 '18 at 18:45
  • Ok I see. That could be a way thank you for your time and input. So there is no way of doing a limit 1 with insert? I also do not see how this post got me flagged for not posting for 3 days. If it is so easy, why is there so much confusion on the other 2 articles? How come no one can give a direct answer (if is is 'so easy'?) I would like an explanation for further posting. – user9449525 Mar 14 '18 at 19:05
  • 1
    Look at the second answer in the first question that you linked. Specifically the first 4 lines (2 lines of code and 2 lines of text). There is no such thing as a limit on an insert. The number of rows inserted (per command) are the number of rows that you explicitly put _in the query_. If you put one row of data in the insert query, one row will be inserted. If you put more than one row, more than one will be inserted. Each individual query has no inherent knowledge of any previously-run queries. Think of it from the DB side, if I say `INSERT ... LIMIT 1`, you would say "1 _what_?" – Patrick Q Mar 14 '18 at 19:15

0 Answers0