In my mysql database I get ONLY the records for id 26, 16, 17, 18, 19, 22, 23, 24. Why is that, is there any error in my code? I don;t have a clue, please help:(
<?
$connect = mysql_connect('localhost', 'dbname', 'pass');
if (!$connect) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("dbname") or die(mysql_error());
mysql_query("TRUNCATE TABLE anchors");
$blog_ids = array(
'anchor 1' => '1',
'anchor 2' => '2',
'anchor 3' => '3',
'anchor 2' => '4',
'anchor 2' => '5',
'anchor 4' => '6',
'anchor 5' => '7',
'anchor 6' => '8',
'anchor 7' => '9',
'anchor 8' => '10',
'anchor 9' => '13',
'anchor 10' => '14',
'anchor 11' => '16',
'anchor 12' => '17',
'anchor 13' => '18',
'anchor 14' => '20',
'anchor 15' => '21',
'anchor 16' => '22',
'anchor 17' => '23',
'anchor 18' => '24',
'anchor 19' => '25',
'anchor 20' => '26'
);
foreach($blog_ids as $anchor => $blog_id){
$anchor_url = 'http://www.site.com';
mysql_query("INSERT INTO anchors (blog_id, anchor_url, anchor) VALUES ('$blog_id', '$anchor_url', '$anchor')");
}
mysql_close($connect);
?>
Ty very much, here is the table structure:
CREATE TABLE `anchors` (
`id` int(11) NOT NULL auto_increment,
`blog_id` text,
`anchor_url` text,
`anchor` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `posttitle` (`blog_id`,`a开发者_如何学编程nchor_url`,`anchor`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
Check whether the query actually can be executed.
if (mysql_query("INSERT INTO anchors (blog_id, anchor_url, anchor) VALUES ('$blog_id', '$anchor_url', '$anchor')") === FALSE) {
echo 'failed: ' . mysql_error() . "\n";
}
Furthermore, what is the table structure like? SHOW CREATE TABLE anchors;
will help you. If you have a unique or primary key set, the value really needs to be unique and duplicates fail with an error.
Edit: The problem is not within the script, but within the data. In order to have the data in PHP, the storage needs to be adjusted.
$blog_ids = array(
array(
'anchor' => 'anchor 1',
'blog_id' => '1'
),
array(
'anchor' => 'anchor 2',
'blog_id' => '2'
),
…
);
foreach ($blog_ids as $blog_id_array) {
$blog_id = $blog_id_array['blog_id'];
$anchor = $blog_id_array['anchor'];
$anchor_url = 'http://www.site.com';
if (mysql_query("INSERT INTO anchors (blog_id, anchor_url, anchor) VALUES ('$blog_id', '$anchor_url', '$anchor')") === FALSE) {
echo 'failed: ' . mysql_error() . "\n";
}
}
This should probably be a comment, but it's a bit too long. If you run something like this and get errors, edit your original question to reflect what you've found. If you also post a comment on this answer, I can try to edit this answer to solve your problem.
For INSERT statements, mysql_query()
returns FALSE
on errors. You should check to see if any errors exist. I would edit your code to look more like this:
$result = mysql_query("INSERT INTO anchors (blog_id, anchor_url, anchor) VALUES ('$blog_id', '$anchor_url', '$anchor')");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
In deployment, I wouldn't necessarily die()
, but log errors to some reporting mechanism or parse through mysql_error()
and return a secure and friendly error message that doesn't reveal too much about the underlying database system or queries executed.
精彩评论