查询未插入数据库

时间:2017-07-11 20:16:32

标签: php mysql sql

我正在尝试插入我的数据库。我已经尝试将查询粘贴到MySQL Workbench中并且它可以工作,但是当我运行页面时它不起作用。

echo "<b>DISTRICT</b></br>";
echo "crime volume : " . $crimeVolume. "<br>";
echo "population : " . $population. "<br>";
echo "crime rate : " . $crimeRate=round(($crimeVolume/($population/100000)),2) . " <br>";
echo "crime efficiency / crime clearance efficiency : " . $efficiency=($SolvedCases/$crimeVolume) ." <br>";
echo "use of force : " . $useofForce . " incidents<br>";
echo "drug seizures : " . $numdrug . " (grams/seizures)<br>";
echo "firearm seizures : " . $firearmSeizures=round(($numfire/$stationFire),2) . " <br>";
echo "<br>";
require_once('../../mysqlConnector/mysql_connect.php');
$query2= "INSERT INTO computation (crimeVolume,crimeRate,crimeSolutionEfficiency,useofForce,drugSeizures,firearmsSeizures,stationID) 
VALUES ('{$crimeVolume}','{$crimeRate}','{$efficiency}','{$useofForce}','{$numdrug}','{$firearmSeizures}','0');";
echo $query2;
$result=mysqli_query($dbc,$query2);
echo $result;

1 个答案:

答案 0 :(得分:-1)

请尝试此示例,通过设置数据库连接详细信息,它应该完美无缺。

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }

// Perform query
$result mysqli_query($con,"INSERT INTO computation (crimeVolume,crimeRate,crimeSolutionEfficiency,useofForce,drugSeizures,firearmsSeizures,stationID) 
VALUES ('".$crimeVolume."','".$crimeRate."','".$efficiency."','".$useofForce."','".$numdrug."','".$firearmSeizures."','0')");

// Print result
print_r($result);

// Close connection
mysqli_close($con);
?>

请注意,这不是最终解决方案,您最好使用具有一些过滤和错误检查的正确CLASS。 (为什么不是框架?)。

我希望它有助于你的情况。