php向mysql数据库插入问题(附代码)

2025-03-02 04:07:53
推荐回答(5个)
回答1:

应该是$sql = "INSERT INTO `test`.`user` (`username`, `password`) VALUES (\'test\', \'test\');";
if(!$sql)
{
echo("Unable to insert the new user");
exit();
}
中的`test`.`user` 没有这个的数据表,应该是
$sql = "INSERT INTO test (`username`, `password`) VALUES (\'test\', \'test\');";
if(!$sql)
{
echo("Unable to insert the new user");
exit();
}
你看看我这段代码你能不能看懂

//连接数据库
$link = mysql_connect('localhost', 'root', '123456');
//判断连接是否成功
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
//选择数据库
mysql_select_db("xitong");
//判断姓名是否为空
if (!empty($_POST['name']))
{
if (!empty($_POST['xingbie']))
{
if (!empty($_POST['mima']))
{
$name=($_POST['name']);
$xingbie=($_POST['xingbie']);
$mima=($_POST['mima']);
$xinmima=($_POST['xinmima']);
$PostDate=getdate();
$query="select name,xingbie from zhuce where name='$name' and xingbie='$xingbie'";
$result=mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($num_rows>0)
{
echo "您输入的用户名已经有人注册了,请重新输入!";
echo "";
exit;
}
else
{
if ($mima==$xinmima)
{
$sql1="insert into zhuce (name,xingbie,mima,postDate) values('$name','$xingbie','$mima',Now());";
mysql_query($sql1);
mysql_query("commit");
echo "";
exit;
}
else
{
#echo "您没有输入密码,请重新输入!";
echo "";
exit;
}
}
}
else
{
#echo "您没有输入密码,请重新输入!";
echo "";
exit;
}
}
else
{
echo "";
exit;

}
}
else
{
echo "";
exit;

}
?>

回答2:

//首先我想告诉你
$sql = "INSERT INTO `test`.`user` (`username`, `password`) VALUES (\'test\', \'test\');";
这句话仅仅只是定义了一条sql语句,但是并没有使用相应的php提供的方法来执行这条语句,所以要按照以下去书写插入语句,同时删除和修改语句也是如此:

$sql = "INSERT INTO `test`.`user` (`username`, `password`) VALUES ('test', 'test');";
mysql_query($sql);
//使用一个mysql_query();方法来执行sql插入语句
$i=mysql_affected_rows();
if($i>0){echo '成功';}else{echo '失败';}
//之后使用mysql_affected_rows();方法得到插入行数;
然后你判断$i的值,如果大于0就是插入成功,插入test 为什么要加\啊
直接就values('text','text');不行咱们在线聊

回答3:

你只是写了一下SQL语句,并没有执行,当然没有插入任何东西了
而且$sql为非空值,所以每次你都会得到正确的反馈,
在$sql = "INSERT INTO `test`.`user` (`username`, `password`) VALUES (\'test\', \'test\');";
后面,$result = mysql_query($sql,$connect);
然后判断$result是否为真

回答4:

你先搞清楚哪个是数据库,哪个是表!!!!!!!user是数据库 test是表

回答5:

我没记错的话,单引号'不用\吧。。。

$sql = "INSERT INTO `test`.`user` (`username`, `password`) VALUES (\'test\', \'test\');";
改为
$sql = "INSERT INTO `test`.`user` (`username`, `password`) VALUES (\"test\", \"test\");";

看看