<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>医院门诊挂号系统</title>
<style type="text/css">
#table
{
margin-right: auto;
margin-left: auto;
}
</style>
</head>
<body style="text-align: center">
<?php
$pname=$_POST['pname'];
$psex=$_POST['psex'];
$page=$_POST['page'];
$room=$_POST['room'];
if (empty($pname)||empty($psex)||empty($page))
{
?>
<p style="text-align:center">请输入完整的患者信息!</p>
<form method="post" action="Guahao.php">
<table id="table">
<tr>
<td>姓名:</td><td><input type="text" id="pname" /></td><td>性别:</td><td><input type="text" id="psex" /></td>
</tr>
<tr>
<td>年龄:</td><td><input type="text" id="page" /></td><td>科室:</td><td><input type="text" id="room" /></td>
</tr>
</table>
<input type="reset" value="重置" id="reset" style="width:100px;height:20px" />
<input type="submit" value="挂号" id="submit" style="width:100px;height:20px" />
</form>
<?php
}
else
{
$db = new mysqli('127.0.0.1','clinicadmin','clinicadmin','clinic');
if (mysqli_connect_errno())
{
echo "无法连接数据库,请稍后重试。";
exit;
}
$query = "insert into guahao values
('".$pname."', '".$psex."', '".$page."', '".$room."')";
$result = $db->query($query);
if (!$result)
{
echo"无法登陆!";
exit;
}
echo"<p>挂号成功!</p>";
$db->close();
?>
<a href="guahao.html">继续挂号</a>
<?php
}
?>
</body>
</html>
我用这个名叫Guahao.php的文件接受表单传来的数据,然后往mysql中插入记录,但是就是那句查询语句的返回值每次都是false,我郁闷,试了无数种方法都是不行。跪求高手帮我看看问题在哪啊!
19 个解决方案
#1
if (!$result)
{
echo $db->error;
echo"无法登陆!";
exit;
}
{
echo $db->error;
echo"无法登陆!";
exit;
}
#2
直接输出sql语句看看
#3
贴错误最实际。
#4
哇,这个我会,你把错误提示贴出来瞄哈~~·
#5
if (isset($_POST['pname']) && isset($_POST['psex']) && isset($_POST['page']) && isset($_POST['room'])) {
$pname=$_POST['pname'];
$psex=$_POST['psex'];
$page=$_POST['page'];
$room=$_POST['room'];
if (empty($pname)||empty($psex)||empty($page)) {
//...
#6
输出sql 看sql是否有问题,还有个这个php不严禁,要改成ls那样
#7
错误是这个
Cannot add or update a child row: a foreign key constraint fails (`clinic`.`guahao`, CONSTRAINT `guahao_ibfk_1` FOREIGN KEY (`room`) REFERENCES `room` (`name`) ON DELETE CASCADE)鏃犳硶鐧婚檰锛
我在数据库里建了2张表,room是主表,挂号是字表 room的主键是name,挂号的外键是room 事件是on delete cascade。
我就是QQ加你的那个,哈哈
#8
SQL出了问题,见ls。
php中isset不是input标签在就会返回true嘛?
#9
Cannot add or update a child row: a foreign key constraint fails (`clinic`.`guahao`, CONSTRAINT `guahao_ibfk_1` FOREIGN KEY (`room`) REFERENCES `room` (`name`) ON DELETE CASCADE)鏃犳硶鐧婚檰锛
求指教
#10
不能添加或更新子行:一个外键约束失败
你的sql语句?
你的sql语句?
#11
mysql> create table room
-> (name char(20) not null primary key,
-> doctor char(20) not null) type=innodb;
Query OK, 0 rows affected, 1 warning (0.45 sec) //这是主表
mysql> create table guahao
-> (name char(20) not null primary key,
-> sex char(10) not null,
-> age char(10) not null,
-> room char(20) not null,
-> foreign key(room) references room(name) on delete cascade) type=innodb;
Query OK, 0 rows affected, 1 warning (0.41 sec) //这是子表
我在命令行执行了下面2个插入的语句
mysql> insert into room values
-> ('五官科','李勇'),
-> ('脑科','张良'),
-> ('骨科','王刚'),
-> ('皮肤科','孙娟'),
-> ('内科','王友福');
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> insert into guahao values
-> ('赵一','男','20','五官科'),
-> ('钱二','男','40','脑科'),
-> ('孙三','女','35','骨科'),
-> ('李四','男','30','皮肤科'),
-> ('王八','女','60','内科');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
现在我想通过PHP进行插入,就出现刚才的错误了。我是新手,不太懂,不知道能否在字表插入多条记录对应主表一个外键,就是我想形成多对一的关系
#12
我又在命令行测试了
sql语句如下
mysql> insert into guahao values
-> ('寂寞','男','22','骨科');
Query OK, 1 row affected (0.00 sec)
mysql> select * from guahao;
+--------+-----+-----+-----------+
| name | sex | age | room |
+--------+-----+-----+-----------+
| 王八 | 女 | 60 | 内科 |
| 钱二 | 男 | 40 | 脑科 |
| 赵一 | 男 | 20 | 五官科 |
| 孙三 | 女 | 35 | 骨科 |
| 寂寞 | 男 | 22 | 骨科 |
| 李四 | 男 | 30 | 皮肤科 |
+--------+-----+-----+-----------+
6 rows in set (0.00 sec)
为什么在命令行有用,用PHP插入就是出错?
#13
我又查看了mysql的编码格式
mysql> show variables like 'character%';
+--------------------------+--------------------------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.1.63-osx10.6-x86_64/share/charsets/ |
+--------------------------+--------------------------------------------------------+
8 rows in set (0.00 sec)
我的PHP文件编码格式是utf-8这样会不会有冲突啊?大神,求教啊!
#14
show create table guahao ; 贴出结果。
#15
mysql> show create table guahao;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| guahao | CREATE TABLE `guahao` (
`name` char(20) NOT NULL,
`sex` char(10) NOT NULL,
`age` char(10) NOT NULL,
`room` char(20) NOT NULL,
PRIMARY KEY (`name`),
KEY `room` (`room`),
CONSTRAINT `guahao_ibfk_1` FOREIGN KEY (`room`) REFERENCES `room` (`name`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.35 sec)
#16
你的SQL语句是什么啊?
#17
在PHP中的SQL语句是
$query = "insert into guahao values ('".$pname."', '".$psex."', '".$page."', '".$room."')";
PHP中插入会出错,但是同样的语句在命令行操作就有效。
#18
命令行下执行:
alter table guahao drop foreign key guahao_ibfk_1;
再执行:
alter table guahao add foreign key(room) references `room` (`name`) on delete cascade on update cascade;
这样试试。
alter table guahao drop foreign key guahao_ibfk_1;
再执行:
alter table guahao add foreign key(room) references `room` (`name`) on delete cascade on update cascade;
这样试试。
#19
还是遇到了同样了的问题,不过还是谢谢
#20
#1
if (!$result)
{
echo $db->error;
echo"无法登陆!";
exit;
}
{
echo $db->error;
echo"无法登陆!";
exit;
}
#2
直接输出sql语句看看
#3
贴错误最实际。
#4
哇,这个我会,你把错误提示贴出来瞄哈~~·
#5
if (isset($_POST['pname']) && isset($_POST['psex']) && isset($_POST['page']) && isset($_POST['room'])) {
$pname=$_POST['pname'];
$psex=$_POST['psex'];
$page=$_POST['page'];
$room=$_POST['room'];
if (empty($pname)||empty($psex)||empty($page)) {
//...
#6
输出sql 看sql是否有问题,还有个这个php不严禁,要改成ls那样
#7
错误是这个
Cannot add or update a child row: a foreign key constraint fails (`clinic`.`guahao`, CONSTRAINT `guahao_ibfk_1` FOREIGN KEY (`room`) REFERENCES `room` (`name`) ON DELETE CASCADE)鏃犳硶鐧婚檰锛
我在数据库里建了2张表,room是主表,挂号是字表 room的主键是name,挂号的外键是room 事件是on delete cascade。
我就是QQ加你的那个,哈哈
#8
SQL出了问题,见ls。
php中isset不是input标签在就会返回true嘛?
#9
Cannot add or update a child row: a foreign key constraint fails (`clinic`.`guahao`, CONSTRAINT `guahao_ibfk_1` FOREIGN KEY (`room`) REFERENCES `room` (`name`) ON DELETE CASCADE)鏃犳硶鐧婚檰锛
求指教
#10
不能添加或更新子行:一个外键约束失败
你的sql语句?
你的sql语句?
#11
mysql> create table room
-> (name char(20) not null primary key,
-> doctor char(20) not null) type=innodb;
Query OK, 0 rows affected, 1 warning (0.45 sec) //这是主表
mysql> create table guahao
-> (name char(20) not null primary key,
-> sex char(10) not null,
-> age char(10) not null,
-> room char(20) not null,
-> foreign key(room) references room(name) on delete cascade) type=innodb;
Query OK, 0 rows affected, 1 warning (0.41 sec) //这是子表
我在命令行执行了下面2个插入的语句
mysql> insert into room values
-> ('五官科','李勇'),
-> ('脑科','张良'),
-> ('骨科','王刚'),
-> ('皮肤科','孙娟'),
-> ('内科','王友福');
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> insert into guahao values
-> ('赵一','男','20','五官科'),
-> ('钱二','男','40','脑科'),
-> ('孙三','女','35','骨科'),
-> ('李四','男','30','皮肤科'),
-> ('王八','女','60','内科');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
现在我想通过PHP进行插入,就出现刚才的错误了。我是新手,不太懂,不知道能否在字表插入多条记录对应主表一个外键,就是我想形成多对一的关系
#12
我又在命令行测试了
sql语句如下
mysql> insert into guahao values
-> ('寂寞','男','22','骨科');
Query OK, 1 row affected (0.00 sec)
mysql> select * from guahao;
+--------+-----+-----+-----------+
| name | sex | age | room |
+--------+-----+-----+-----------+
| 王八 | 女 | 60 | 内科 |
| 钱二 | 男 | 40 | 脑科 |
| 赵一 | 男 | 20 | 五官科 |
| 孙三 | 女 | 35 | 骨科 |
| 寂寞 | 男 | 22 | 骨科 |
| 李四 | 男 | 30 | 皮肤科 |
+--------+-----+-----+-----------+
6 rows in set (0.00 sec)
为什么在命令行有用,用PHP插入就是出错?
#13
我又查看了mysql的编码格式
mysql> show variables like 'character%';
+--------------------------+--------------------------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.1.63-osx10.6-x86_64/share/charsets/ |
+--------------------------+--------------------------------------------------------+
8 rows in set (0.00 sec)
我的PHP文件编码格式是utf-8这样会不会有冲突啊?大神,求教啊!
#14
show create table guahao ; 贴出结果。
#15
mysql> show create table guahao;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| guahao | CREATE TABLE `guahao` (
`name` char(20) NOT NULL,
`sex` char(10) NOT NULL,
`age` char(10) NOT NULL,
`room` char(20) NOT NULL,
PRIMARY KEY (`name`),
KEY `room` (`room`),
CONSTRAINT `guahao_ibfk_1` FOREIGN KEY (`room`) REFERENCES `room` (`name`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.35 sec)
#16
你的SQL语句是什么啊?
#17
在PHP中的SQL语句是
$query = "insert into guahao values ('".$pname."', '".$psex."', '".$page."', '".$room."')";
PHP中插入会出错,但是同样的语句在命令行操作就有效。
#18
命令行下执行:
alter table guahao drop foreign key guahao_ibfk_1;
再执行:
alter table guahao add foreign key(room) references `room` (`name`) on delete cascade on update cascade;
这样试试。
alter table guahao drop foreign key guahao_ibfk_1;
再执行:
alter table guahao add foreign key(room) references `room` (`name`) on delete cascade on update cascade;
这样试试。
#19
还是遇到了同样了的问题,不过还是谢谢