它应该说新记录然后我应该能够在项目表中看到新数据
与数据库文件的连接包含:
$server="localhost";
$login="root";
$pass="";
$database="floweritem";
$con=mysqli_connect($server,$login,$pass,$database);
if(mysqli_connect_errno()){
echo "Failed connect to MYSQL".mysqli_connect_error();
}
表格行动
PHP
<?php
if(isset($_POST['submit'])){
$Name=mysqli_real_escape_string($con,$_POST['Name']);
$Desc=mysqli_real_escape_string($con,$_POST['Desc']);
$Size=mysqli_real_escape_string($con,$_POST['Size']);
$Price=mysqli_real_escape_string($con,$_POST['Price']);
//no problwm with connection to table or database
$sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`)
VALUES($Name,$Desc,$Size,$Price)";
echo $Desc;
if(mysqli_query($con,$sql)){
echo "new record";
}
//always get wrong
//can't find problem
else{echo"Wrong";}
mysqli_close($con);
}
?>
显示$ Desc但未成功在项目表中插入数据
答案 0 :(得分:3)
新的PHP代码应该是这样的
<?php
if(isset($_POST['submit'])){
$Name=mysqli_real_escape_string($con,$_POST['Name']);
$Desc=mysqli_real_escape_string($con,$_POST['Desc']);
$Size=mysqli_real_escape_string($con,$_POST['Size']);
$Price=mysqli_real_escape_string($con,$_POST['Price']);
//no problwm with connection to table or database
$sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`)
VALUES('$Name','$Desc','$Size','$Price')";
echo $Desc;
if(mysqli_query($con,$sql)){
echo "new record";
}
//always get wrong
//can't find problem
else{echo"Wrong";}
mysqli_close($con);
}
?>
答案 1 :(得分:1)
您需要针对值添加单引号。
更正代码:
//no problwm with connection to table or database
$sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`)
VALUES('$Name','$Desc','$Size','$Price')";
echo $Desc;
否则,MySQL会将用户输入的值视为MySQL关键字,从而产生语法错误。
答案 2 :(得分:1)
试试这个:
PHP:
<?php
if(isset($_POST['submit'])){
$Name=mysqli_real_escape_string($con,$_POST['Name']);
$Desc=mysqli_real_escape_string($con,$_POST['Desc']);
$Size=mysqli_real_escape_string($con,$_POST['Size']);
$Price=mysqli_real_escape_string($con,$_POST['Price']);
//no problwm with connection to table or database
$sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`)
VALUES('$Name','$Desc','$Size','$Price')";
echo $Desc;
if(mysqli_query($con,$sql)){
echo "new record";
}
//always get wrong
//can't find problem
else{echo"Wrong";}
mysqli_close($con);
}
?>