我查看并测试了几种解决方案,但仍然存在同样的问题..
我有两个表:tb_cadastro(登录)和tb_quest(问卷答案)
CREATE TABLE IF NOT EXISTS `tb_cadastro` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_quest` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`surname` varchar(20) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(35) NOT NULL,
`sex` varchar(4) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_id_quest` (`id_quest`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
ALTER TABLE `tb_cadastro`
ADD CONSTRAINT `fk_id_quest` FOREIGN KEY (`id_quest`) REFERENCES `tb_quest` (`id_quest`) ON UPDATE NO ACTION;
CREATE TABLE IF NOT EXISTS `tb_quest` (
`id_quest` int(11) NOT NULL AUTO_INCREMENT,
`q1` varchar(1) NOT NULL,
`q2` varchar(1) NOT NULL,
`q3` varchar(1) NOT NULL,
`q4` varchar(1) NOT NULL,
`q5` varchar(1) NOT NULL,
`q6` varchar(1) NOT NULL,
`q7` varchar(1) NOT NULL,
`q8` varchar(1) NOT NULL,
PRIMARY KEY (`id_quest`),
UNIQUE KEY `id_quest` (`id_quest`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
这两个表与id_quest
相关问题是tb_cadastro表没有向db添加任何内容,但也没有显示错误.. table tb_quest正常添加数据 我也可以通过mysql
在tb_cadastro中添加数据我得出的结论是问题出现在FK约束中,所以我删除了FK的关系并且一切正常
我尝试了所有我发现的东西, 所以如果你有解决方案,我将不胜感激
这是我的PHP代码
<?php
session_start();
require_once ("conexao.php");
mysql_connect("$hostname", "$username", "$senhabd") or die(mysql_error());
mysql_select_db($banco) or die(mysql_error());
$_SESSION['Usuario'] = $_POST['Usuario'];
$_SESSION['S_usuario'] = $_POST['S_usuario'];
$_SESSION['Email'] = $_POST['Email'];
$_SESSION['C_email'] = $_POST['C_email'];
$_SESSION['Senha'] = $_POST['Senha'];
$_SESSION['C_senha'] = $_POST['C_senha'];
$_SESSION['Sexo'] = $_POST['Sexo'];
$_SESSION['q1'] = $_POST['q1'];
$_SESSION['q2'] = $_POST['q2'];
$_SESSION['q3'] = $_POST['q3'];
$_SESSION['q4'] = $_POST['q4'];
$_SESSION['q5'] = $_POST['q5'];
$_SESSION['q6'] = $_POST['q6'];
$_SESSION['q7'] = $_POST['q7'];
$_SESSION['q8'] = $_POST['q8'];
$insert="INSERT INTO tb_quest (q1,q2,q3,q4,q5,q6,q7,q8) VALUES( '" . $_SESSION['q1'] . "','" . $_SESSION['q2'] . "','" . $_SESSION['q3'] . "','" . $_SESSION['q4'] . "','" . $_SESSION['q5'] . "','" . $_SESSION['q6'] . "','" . $_SESSION['q7'] . "','" . $_SESSION['q8'] . "')"or die (mysql_error());
mysql_query($insert);
$id_quest = mysql_insert_id();
//echo $id_quest;
$insert="INSERT INTO tb_cadastro (id_quest,name,surname,email,password,sex) VALUES( $id_quest,'" . $_SESSION['Usuario'] . "','" . $_SESSION['S_usuario'] . "','" . $_SESSION['Email'] . "','" . $_SESSION['Senha'] . "','" . $_SESSION['Sexo'] . "','" . $_SESSION['Sexo'] . "')" or die (mysql_error());
mysql_query($insert);
?>
答案 0 :(得分:0)
我不做PHP或mysql,但我猜你想在SQL字符串之外引用变量$ id_quest。
$insert="INSERT INTO tb_cadastro (id_quest,name,surname,email,password,sex) VALUES(" . $id_quest . ",'" . $_SESSION['Usuario'] . "','" . $_SESSION['S_usuario'] . "','" . $_SESSION['Email'] . "','" . $_SESSION['Senha'] . "','" . $_SESSION['Sexo'] . "','" . $_SESSION['Sexo'] . "')" or die (mysql_error());