/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Transitions insert failiure PHP/MySQL

Hi, I’m not sure if this is a php, or mysql problem.

I’m trying to insert data into two different tables using transitions, but am getting an error I can’t figure out. If I move the $mysqli->commit(); into the foreach loop, I get at least one returned row before the rest fail. The current error message is Array ( [0] => Error: Couldn’t insert into english! ). Any idea what is causing this? Thanks.

[code=php]<?php

$file_array = file(‘../grammar/conjunctions.txt’);
$csv = array_map(‘str_getcsv’, $file_array);

// DB
$mysqli = new mysqli(‘localhost’, ‘root’, ‘******’, ‘angos’);

$mysqli->autocommit(false);
$error = array();

foreach($csv as $value)
{
$angos_query = $mysqli->query(“INSERT INTO angos (angos, grammar) VALUES (‘$value[0]’, ‘con’)”);
$id = $mysqli->insert_id; // grab the currant angos table id
if($angos_query == false)
{
array_push($error, “Error: Couldn’t insert into angos!”);
}

$english_query = $mysqli->query(“INSERT INTO english (angos_id, english) VALUES (‘$id’, ‘$value[1]’)”);
if($english_query == false)
{
array_push($error, “Error: Couldn’t insert into english!”);
}

if(!empty($error))
{
$mysqli->rollback();
}

}

$mysqli->commit();

print_r($error);
// print_r($csv);

?> [/code]

More info SQL:

[CODE]
CREATE TABLE angos
(
id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
angos varchar(255) not null,
grammar varchar(3) not null,
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT unique_input UNIQUE (angos)
) engine=InnoDB;

CREATE TABLE english
(
id int unsigned not null primary key,
angos_id int unsigned,
english varchar(255),
grammar_note varchar(500),
CONSTRAINT fk_angos_source FOREIGN KEY (angos_id) REFERENCES angos(id) ON DELETE CASCADE ON UPDATE CASCADE
) engine=InnoDB;[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 16.2014 — Try adding more info to the debug output. I like to define the query as a string variable so I can include that in the debug, too:
[code=php]
$sqlEnglish = "INSERT INTO english (angos_id, english) VALUES ('$id', '$value[1]')";
$english_query = $mysqli->query($sqlEnglish);
if($english_query == false)
{
error_log("Query failed: ".$mysqli->error().PHP_EOL.$sqlEnglish);
// or you could just add the above to your array_push, below, if you prefer
array_push($error, "Error: Couldn't insert into english!");
}
[/code]
Copy linkTweet thisAlerts:
@lekeauthorJul 17.2014 — Thanks NogDog. Adding $mysqli->error (minus parentheses) error checking to the error array showed me the id of the english table was not incrementing it's own id. This resulted in the error.

So id int unsigned NOT NULL PRIMARY KEY, needed to be id int unsigned NOT NULL [B]AUTO_INCREMENT[/B] PRIMARY KEY, which is strange because I thought making something a primary key meant it was automatically auto-incremented.
Copy linkTweet thisAlerts:
@NogDogJul 17.2014 — Glad you figured it out. Sorry for the parens -- I use PDO these days, which has an errorInfo() method instead of a property. ?

Yeah, you could have a primary key on a text field, or even across multiple fields of different types -- though having it on an auto-incrementing integer field is possibly the most common usage.
×

Success!

Help @leke spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...