用php插入数据库没有报错,只是显示插入失败。
什么原因呢?同样我的update也是这种原因。以下是insert的代码:<?php$host="localhost";$user="root";$password="ro...
什么原因呢?同样我的update也是这种原因。
以下是insert的代码:
<?php
$host = "localhost";
$user = "root";
$password = "root";
$db_name = "yaopin";
$conn = mysql_connect($host,$user,$password) or
die ("连接数据库服务器失败。".mysql_error( ));
mysql_select_db("yaopin");
if(@$_POST["num"] != "" && $_POST["name"] != "" && $_POST["zuoyong"] != ""&& $_POST["danjia"] != ""&& $_POST["scrq"] != ""&& $_POST["yxq"] != ""&& $_POST["shuliang"] != "")
{
$number = $_POST["num"];
$name = $_POST["name"];
$effect = $_POST["zuoyong"];
$danjia = $_POST["danjia"];
$scrq= $_POST["scrq"];
$yxq=$_POST["yxq"];
$sl=$_POST["shuliang"];
$str = "insert into yaopinbiao values('$number','$name','$effect','$danjia','$scrq'.'$yxq','$sl')";
$result = mysql_query($str);
if($result > 0 )
{
echo "<script>window.location.href = '../xxx/yaopin.php'</script>";
}
else
{
echo "<script>alert('插入失败!');
window.location.href = '../xxx/insert.php'</script>";
}
}
else
{
echo "<script>alert('药品号,药品名,作用,单价,生产日期,有效期和数量不能为空!');
window.location.href = '../xxx/insert.php'</script>";
}
?> 展开
以下是insert的代码:
<?php
$host = "localhost";
$user = "root";
$password = "root";
$db_name = "yaopin";
$conn = mysql_connect($host,$user,$password) or
die ("连接数据库服务器失败。".mysql_error( ));
mysql_select_db("yaopin");
if(@$_POST["num"] != "" && $_POST["name"] != "" && $_POST["zuoyong"] != ""&& $_POST["danjia"] != ""&& $_POST["scrq"] != ""&& $_POST["yxq"] != ""&& $_POST["shuliang"] != "")
{
$number = $_POST["num"];
$name = $_POST["name"];
$effect = $_POST["zuoyong"];
$danjia = $_POST["danjia"];
$scrq= $_POST["scrq"];
$yxq=$_POST["yxq"];
$sl=$_POST["shuliang"];
$str = "insert into yaopinbiao values('$number','$name','$effect','$danjia','$scrq'.'$yxq','$sl')";
$result = mysql_query($str);
if($result > 0 )
{
echo "<script>window.location.href = '../xxx/yaopin.php'</script>";
}
else
{
echo "<script>alert('插入失败!');
window.location.href = '../xxx/insert.php'</script>";
}
}
else
{
echo "<script>alert('药品号,药品名,作用,单价,生产日期,有效期和数量不能为空!');
window.location.href = '../xxx/insert.php'</script>";
}
?> 展开
3个回答
展开全部
yaopinbiao 这个表的表结构发出来看看,估计问题是与你这个表不止七个字段,而你却只给了七个值,其他没给值的字段又没有设定默认值或没允许为空之类的吧,
追答
如果是数据库没连上肯定会提示 "连接数据库服务器失败。",程序肯定走不下去的了,估计是楼上说的,$number值重复了,因为你是用这个字段来当主键的,表里面主键值是必须为唯一的,重复值是插不进表里面的,你可以这样试试嘛,看看能不能插进去,
insert into yaopinbiao values($number, "$name", "$effect", "$danjia", "$scrq"."$yxq", "$sl");
// 或者这样 加个时间戳试试
insert into yaopinbiao values($number . time(), "$name", "$effect", "$danjia", "$scrq"."$yxq", "$sl");
// 或者是你这里有语法错误吧, $str = "insert into yaopinbiao values('$number','$name','$effect','$danjia','$scrq'.'$yxq','$sl')";
// '$scrq'.'$yxq' 改成 '{$scrq}{$yxq}'
// 试试吧
展开全部
sql 语句错误
insert into 表名('对应的字段名1','xx2','xx3','xx4') values('$number','$name','$effect','$danjia','$scrq'.'$yxq','$sl')
insert into 表名('对应的字段名1','xx2','xx3','xx4') values('$number','$name','$effect','$danjia','$scrq'.'$yxq','$sl')
追问
改过了但还是没有用呢,你看有可能是不是数据库没有连接上呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询