我想使用一個值,我從MySQL查詢接收,然後做一個插入,但它不工作。我得到一個語法錯誤,但插入查詢是正確的。多個查詢在一個php文件中使用先前查詢的結果集
選擇查詢會回退一個數量,我們將檢查,然後程序應該執行插入查詢。
<?php
require 'header.php';
$resID = mysql_real_escape_string($_POST['resID']);
$materialen_id = mysql_real_escape_string($_POST['materialen_id']);
$aantal = mysql_real_escape_string($_POST['aantal']);
$effectief_gebruikt = mysql_real_escape_string($_POST['effectief_gebruikt']);
$opmerking = mysql_real_escape_string($_POST['opmerking']);
//$datum_van = date('d-m-Y', $_POST['datum_van']);
//$datum_tot = date('d-m-Y', $_POST['datum_tot']);
$datum_van = $_POST['datum_van'];
$datum_tot = $_POST['datum_tot'];
$sql = "SELECT `aantal_beschikbaar`
FROM `materialen`
WHERE `id` = $materialen_id";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$tot = $row['aantal_beschikbaar'];
echo 'totaal: ' . $tot;
}
$sql2 = "SELECT `aantal` FROM `materialen_per_reservatie`
WHERE `materialen_id` = $materialen_id";
$result2 = mysql_query($sql2) or die(mysql_error());
while ($row = mysql_fetch_array($result2))
{
//$aant = $row['aantal'];
//echo $aant
echo $row['aantal'];
}
$besch = ($tot - $aant);
echo 'beschikbaar: ' . $besch;
/*$sql3 = "SELECT * FROM `materialen_per_reservatie`
WHERE `reservaties_id` = $resID
AND `materialen_id` = $materialen_id";
$result3 = mysql_query($sql3) or die(mysql_error());*/
if($besch > $aantal){
$string2 = "INSERT INTO `materialen_per_reservatie`(`reservaties_id`, `materialen_id`, `aantal`, `effectief_gebruikt`, `opmerking`, `datum_van`, `datum_tot`) VALUES ($resID, $materialen_id, $aantal, $effectief_gebruikt, '$opmerking', '$datum_van', '$datum_tot')";
mysql_query($string2) or die(mysql_error());
}
require 'footer.php';
?>
什麼是錯誤? – 2012-02-09 13:55:04
你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在第3行使用正確的語法 – 2012-02-09 14:10:40
您可以在$ string2執行之前回顯$ string2,然後將其複製並粘貼到mysql中而不會出錯? – FreudianSlip 2012-02-09 13:57:18