Answer the question
In order to leave comments, you need to log in
Explain strange INSERT behavior in mysql_query?
Hello!
I’m working on 1 project here and the following task arose: “Get a string with hash tags separated by commas from the user and write them 1 at a time to the database table. The table contains only 2 columns: ident and hash_tag. Ident is the identifier of what the hash tags refer to, and hash_tag is each individual hash tag ”
I wrote a script:
$idea_hash = "слово1, слово2, слово3";
$idea_count = 3
$mass = explode(',', $idea_hash);
foreach($mass as $n)
{
mysql_query("INSERT INTO hashes (ident, hash_tag) VALUES('$idea_count','$n')");
}
Answer the question
In order to leave comments, you need to log in
Is the table empty? hash_tag is not unique by any chance?
1. For debugging, do
mysql_query("INSERT INTO hashes (ident, hash_tag) VALUES('$idea_count','$n')") or die(mysql_error());
Do you have a unique index on the ident field? And then you write three records with the same $idea_count value - in your case 3.
I hope the given code fragment is just a test, and not a piece from your project? Otherwise, this is one big hole in the security of the site;) Switch to using mysqli and prepared statements. This will dramatically increase the speed of query execution and save the project from a large number of security problems.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question