MySQL在查询中没有正确使用PHP变量,用字符串/整数替换变量可以正常工作



MySQL没有像它应该的那样使用变量。 它没有从它们中获取任何值,而是递增 MYSQL 表中的自动增量数字,但是该行没有保存。我没有收到任何错误。

我试过这样:

$sql = "INSERT INTO `tbl_bike` (`userID`, `ManuPartNo`, `BikeManufacturer`, `BikeModel`, `BikeType`, `BikeWheel`, `BikeColour`, `BikeSpeed`, `BrakeType`, `FrameGender`, `AgeGroup`, `DistFeatures`) 
VALUES (“.$userID.”, “.$PartNo.”, “.$BikeManufacturer.”, “.$BikeModel.”, “.$BikeType.”, “.$BikeWheel.”, “.$BikeColour.”, “.$BikeSpeed.”, “.$BrakeType.”, “.$FrameGender.”, “.$AgeGroup.”, “.$DistFeatures.”)";

我还尝试将" 替换为 ',删除 .甚至完全删除".对这个问题没有任何帮助。当我使用此查询但删除变量并改为将字符串、int 等放在正确的位置时,查询将完美运行并将结果放入表中。我的变量通常如下:

$PartNo =  $_POST['ManuPartNo’];
$BikeManufacturer =  $_POST['BikeManufacturer’]; 
$BikeModel =  $_POST['BikeModel’]; 
$BikeType =  $_POST['BikeType’]; 
$BikeWheel =  $_POST['BikeWheel’]; 
$BikeColour =  $_POST['BikeColour’]; 
$BikeSpeed =  $_POST['BikeSpeed’]; 
$BrakeType =  $_POST['BrakeType’]; 
$FrameGender =  $_POST['FrameGender’]; 
$AgeGroup =  $_POST['AgeGroup’]; 
$DistFeatures =  $_POST['DistFeatures’]; 

这些变量通常从单独的 PHP/HTML 文件中获取输入,并带有'$_POST['DistFeatures’];'

我尝试从每个末端删除$_POST['DistFeatures’];,只是用普通字符串或 int 值替换值,但仍然没有任何帮助。我完全陷入困境,希望得到任何帮助。

这一切都在 plesk 服务器上运行。

请停止使用已弃用的MySQL。我将使用 PDO 提出答案。您可以使用它来使用 PDO 构建其他查询。

// Establish a connection in db.php (or your connection file)
$dbname = "dbname"; // your database name
$username = "root"; // your database username
$password = ""; // your database password or leave blank if none
$dbhost = "localhost";
$dbport = "10832";
$dsn = "mysql:dbname=$dbname;host=$dbhost";
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
// Include db.php on every page where queries are executed and perform queries the following way
// Take Inputs this way (your method is obsolete and will return "Undefined Index" error)
$userId = (!empty($_SESSION['sessionname']))?$_SESSION['sessionname']:null; // If session is empty it will be set to Null else the session value will be set
$PartNo = (!empty($_POST['ManuPartNo']))?$_POST['ManuPartNo']:null; // If post value is empty it will be set to Null else the posted value will be set
$BikeManufacturer = (!empty($_POST['BikeManufacturer']))?$_POST['BikeManufacturer']:null;
$BikeModel = (!empty($_POST['BikeModel']))?$_POST['BikeModel']:null;
$BikeType = (!empty($_POST['BikeType']))?$_POST['BikeType']:null;
$BikeWheel = (!empty($_POST['BikeWheel']))?$_POST['BikeWheel']:null;
// Query like this
$stmt = $pdo->prepare("INSERT INTO(`userID`, `ManuPartNo`, `BikeManufacturer`, `BikeModel`, `BikeType`)VALUES(:uid, :manuptno, :bkman, :bkmodel, :bktype)");
$stmt-> bindValue(':uid', $userId);
$stmt-> bindValue(':manuptno', $PartNo);
$stmt-> bindValue(':bkman', $BikeManufacturer);
$stmt-> bindValue(':bkmodel', $BikeModel);
$stmt-> bindValue(':bktype', $BikeType);
$stmt-> execute();
if($stmt){
echo "Row inserted";
}else{
echo "Error!";
}

看,就是这么简单。从现在开始使用 PDO。它更安全。要尝试此操作,只需将整个代码复制到空白的PHP文件中并运行它。您的数据库将收到一个条目。请确保在此处更改数据库值。

你应该试试这个

$sql = "INSERT INTO tbl_bike (userID, ManuPartNo, BikeManufacturer, BikeModel, BikeType, BikeWheel, BikeColour, BikeSpeed, BrakeType, FrameGender, AgeGroup, DistFeatures) VALUES ('$userID', '$PartNo', '$BikeManufacturer', '$BikeModel', '$BikeType', '$BikeWheel', '$BikeColour', '$BikeSpeed', '$BrakeType', '$FrameGender', '$AgeGroup', '$DistFeatures')";

如果这不起作用,请在 sql 值中启用 null 属性。因此,您可以找出错误的起源。

最新更新