我是php脚本的新手..我想知道我的php脚本中是否有任何错误,我想从收入表中获取income_id(收入表中的主键)并将其插入费用表(作为外键) ...我可以将所有数据添加到费用表中,除了income_id ..
<?php
//Importing our db connection script
require_once('dbConnect.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$id = $_POST['id'];
$income_id = $_POST['income_id'];
$category = $_POST['category'];
$amount = $_POST['amount'];
$date = date('Y-m-d');
$sql = "SELECT income_id from `income` where id='".$id."'";
$result = mysqli_query($con, $sql);
$rows = mysqli_fetch_array($result);
//Creating an sql query
$sql = "INSERT INTO expenses (income_id,category,amount,date) VALUES ('$rows[income_id]','$category','$amount','$date')";
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Added Successfully';
}else{
echo 'Could Not Add';
}
//Closing the database
mysqli_close($con);
}
&#13;
答案 0 :(得分:0)
我建议加入两个查询:
INSERT INTO expenses (income_id, category, amount, date)
VALUES ((SELECT income_id FROM `income` WHERE id='$id'), '$category', '$amount', '$date')