<?php
@$code = $_POST['code'];
@$name = $_POST['name'];
@$city = $_POST['city'];
$connect = mysql_connect("localhost", "root", "", "parth") or die("connection failed");
$sql_query = "INSERT INTO customer_master(customer_code,customer_name,customer_city) values(" . $code . ",'" . $name . "','" . $city . "');";
$result = mysql_query($sql, $connect);
if ($result) {
echo "database connected";
} else {
echo "data not inserted";
}
mysql_close($connect);
?>
上面提到的代码是在数据库中插入数据但未插入数据的代码,没有显示任何错误,所以任何人都可以帮我解决问题。
答案 0 :(得分:0)
你在这里犯了错误“。$ code。”,错过了单引号而你正在使用现在已经折旧的mysql所以请使用mysqli,如下所示:
<?php
@$code=$_POST['code'];
@$name=$_POST['name'];
@$city=$_POST['city'];
$connect=mysqli_connect("localhost","root","","parth") or die("connection failed");
$sql_query="INSERT INTO customer_master(customer_code,customer_name,customer_city) values('".$code."','".$name."','".$city."');";
$result=mysqli_query($sql,$connect);
if($result)
{
echo "database connected";
}
else
{
echo "data not inserted";
}
mysqli_close($connect);
?>
答案 1 :(得分:0)
我从未遇到过&#34; @&#34;在PHP中的变量声明之前,这对我来说是新的,但是像lrvin和其他人指出的那样,你在SQL中缺少单引号。
我不是连接的粉丝,我在尝试采用连接路径时犯了几个错误(这只是一个偏好)。你可以按照lrvin的回答,或者你也可以添加以下修改:
不要使用连接
包含mysql_error函数。这将返回一条错误消息,该消息将直接指向您的错误。
$sql_query="INSERT INTO customer_master(customer_code,customer_name,customer_city) values('$code', '$name', '$city')";
$result=mysql_query($sql_query, $connect) or die(mysql_error($connect));
你应该考虑使用mysqli而不是mysql。 mysql已被弃用。
答案 2 :(得分:0)
'
之前和之后的单引号" . $code . "
。同样删除额外分号;
在插入查询结束时。
请参阅代码:
@$code = $_POST['code'];
@$name = $_POST['name'];
@$city = $_POST['city'];
$connect = mysql_connect("localhost", "root", "", "parth") or die("connection failed");
$sql_query = "INSERT INTO customer_master(customer_code,customer_name,customer_city) values('" . $code . "','" . $name . "','" . $city . "')";
$result = mysql_query($sql, $connect);
if ($result) {
echo "database connected";
} else {
echo "data not inserted";
}
mysql_close($connect);
答案 3 :(得分:-1)
改变这个:
$sql_query="INSERT INTO customer_master(customer_code,customer_name,customer_city) values(".$code.",'".$name."','".$city."');";
$result=mysql_query($sql,$connect);
分为:
$sql_query="INSERT INTO customer_master(customer_code,customer_name,customer_city) values('".$code."','".$name."','".$city."')";
$result=mysql_query($sql_query,$connect);