0

我尝试下一个脚本:

// Insert data into mysql 

$qry="INSERT INTO $tbl_name1 (ID, REFERENCE, CODE, NAME) VALUES (UUID(), '$REFERENCE', '$CODE', '$NAME')";
$result=mysql_query($qry);

$qry2="INSERT INTO $tbl_name2 (PRODUCT) VALUES ('$ID')"; <--- Here is a problem
$result=mysql_query($qry2)

我不知道两个如何同时在两个表中插入相同的 UUID。请帮我!我将非常感谢您的支持!

完毕!!!工作脚本:

$q = "SELECT UUID() AS uid";
$res = mysql_query($q) or die('q error: '.mysql_error());
$row = mysql_fetch_assoc($res);

// Insert data into mysql 
$qry="INSERT INTO $tbl_name1 (ID, REFERENCE, CODE, NAME) VALUES ('".$row['uid']."', '$REFERENCE', '$CODE', '$NAME')";
$result=mysql_query($qry) or die('err 034r '.mysql_error());

$qry2="INSERT INTO $tbl_name2 (PRODUCT) VALUES ('".$row['uid']."')";
$result=mysql_query($qry2) or die('gg2345  '.mysql_error());
4

2 回答 2

2

只需SELECT UUID()在发送INSERTs 并将值放入 PHP 中的语句之前执行此操作。像这样的东西(未经测试):

$result = mysql_query("SELECT UUID() AS UUID") or die('SQL error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
$UUID = $row["UUID"];

$qry="INSERT INTO $tbl_name1 (ID, REFERENCE, CODE, NAME) VALUES ('$UUID', '$REFERENCE',   '$CODE', '$NAME')";
$result=mysql_query($qry);

$qry2="INSERT INTO $tbl_name2 (PRODUCT) VALUES ('$UUID ')"; <--- Here is a problem
$result=mysql_query($qry2)

另一种方法是使用用户定义的变量(参见 SQL Fiddle):

SET @UUID = (SELECT UUID() AS UUID);
INSERT INTO test1 VALUES(@UUID, "foo");
INSERT INTO test1 VALUES(@UUID, "bar");
于 2012-08-29T10:08:47.780 回答
1

假设ID您可以在之前添加的表唯一索引$qry2

$ID = mysql_insert_id();
于 2012-08-29T10:07:45.220 回答