5

I swear I have poured and poured over every other similar question on this site and others... but I think I'm just missing something. Hopefully someone can point out a silly mistake that my brain is hiding from me. :)

My script inserts values from a form into a table called "notes"

At that point it creates two entries in a table called "relationships" through a function called newRelationship.

The value of the variable "$note_id" is populated via my mysql_insert_id(); and passed into the above function.

When the code is executed, the entry is successfully added to "notes" and the "id" column is given a proper value with auto_increment.

Next, two entries are added to the "relationship" table (as they should).

HOWEVER, my mysql_insert_id() keeps kicking out a value of "0" instead of the new row id.

For the life of me I cant figure out what I'm doing wrong. I even tried creating a new table from scratch with the same results. The kicker is that I use pretty much identical code in other files of my project with no problems. Anyone see what I'm doing wrong?

the code in question

    if ($user->is_loaded())
    {

    if($_POST['project_id']) {
    $project_id = $_post['project_id'];
    $long_note = $_POST['long_note'];
    $created_by = $_POST['created_by'];
    $note_sql="INSERT INTO notes (`long_note`, `added`, `created_by`) VALUES ('$long_note', '$timenow', '$created_by')";
    if (!mysql_query($note_sql,$con))
    {
    die('Error: ' . mysql_error($note_sql));
    }
    else {
    echo "note created Creating relationship ";
    $note_id = mysql_insert_id();

    echo $note_id;
    newRelationship($project_id, "project", $note_id, "note");
    newRelationship($client_id, "client", $note_id, "note");
    echo "note added successfuly";

    }

And my function

function newRelationship($parent_id, $parent_type, $child_id, $child_type)
{

global $sql, $con, $timenow;

$relationship_sql="INSERT INTO `relationships` (`parent_id`, `parent_type`, `child_id`, `child_type`) VALUES ('$parent_id', '$parent_type', '$child_id', '$child_type');";
    if (!mysql_query($relationship_sql,$con))
    {
     die('Error: ' . mysql_error($relationship_sql));
    }
    else {
    echo $parent_type." ".$parent_id." realationship with ".$child_type." ".$child_id." successful ";
}

}

Per @jack's suggestion here is the sql of my notes table

CREATE TABLE `notes` (
  `id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `contact_id` int(10) NOT NULL,
  `client_id` int(10) NOT NULL,
  `status` text NOT NULL,
  `long_note` text NOT NULL,
  `added` int(11) NOT NULL,
  `modified` int(11) NOT NULL,
  `edit_by` int(10) NOT NULL, 
  `short_note` text NOT NULL,
  `created_by` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=295 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
4

1 に答える 1

8

ドキュメントによる

成功時に前のクエリによってAUTO_INCREMENT列に対して生成されたID 、前のクエリがAUTO_INCREMENT値を生成しない場合は0、 MySQL接続が確立されていない場合はFALSE。

0ドキュメントには、最後に実行されたクエリが値を生成しないAUTO_INCREMENT場合にのみ返されると記載されています。これは、のPRIMARYKEY列がで適切に設定されていないこと意味します。のPRIMARYKEY列が実際に設定されていることを再確認することをお勧めします(もう一度確認しても問題ありません!)。notesauto_incrementnotes auto_increment

サンプルコードを表示するとmysql_insert_id()、挿入直後に呼び出すため、結果を歪めるために間に競合するクエリがないようにする必要があります

問題を引き起こす可能性があると私が思う唯一のことは、MySQLリソースをに渡していますが、に渡してmysql_query()いないことmysql_insert_id()です:

if (!mysql_query($note_sql,$con))
...
$note_id = mysql_insert_id();

これにより、リソースに競合が発生する可能性があります。次の宛先に通話を更新してみてください。

$note_id = mysql_insert_id($con);
于 2012-10-08T04:33:24.333 に答える