同时插入和更新|数据库、PHP、Mysql
很抱歉我的英语不好。同时插入和更新|数据库、PHP、Mysql,php,mysql,database,account,Php,Mysql,Database,Account,很抱歉我的英语不好。 // Add New Entry if (isset($_POST['submit']) && $_POST['submit'] == 'saveEntry') { // User Validations if($_POST['money_income_id'] == '...') { $msgBox = alertBox($catReq, "<i class='fa fa-tim
// Add New Entry
if (isset($_POST['submit']) && $_POST['submit'] == 'saveEntry') {
// User Validations
if($_POST['money_income_id'] == '...') {
$msgBox = alertBox($catReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_description'] == '') {
$msgBox = alertBox($money_income_descriptionReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_accounts_catogerytitles'] == '...') {
$msgBox = alertBox('Failed! Income Account Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_amount'] == '...') {
$msgBox = alertBox('Failed! Amount Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_time'] == '...') {
$msgBox = alertBox('Failed! Time Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_payer_title'] == '...') {
$msgBox = alertBox('Failed! Payer Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_incomecategory_title'] == '...') {
$msgBox = alertBox('Failed! Income Category Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_paymentmethod_title'] == '...') {
$msgBox = alertBox('Failed! Payment Method Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else {
$money_income_account = $_POST['money_accounts_catogerytitles'];
$money_income_description = encodeIt($_POST['money_income_description']);
$money_income_amount = $_POST['money_income_amount'];
$money_income_time = $_POST['money_income_time'];
$money_income_payer = $_POST['money_payer_title'];
$money_income_category = $_POST['money_incomecategory_title'];
$money_income_paymentmethod = $_POST['money_paymentmethod_title'];
$stmt = $mysqli->prepare("
INSERT INTO
money_income(
money_income_account,
id,
money_income_description,
money_income_amount,
money_income_time,
money_income_payer,
money_income_category,
money_income_paymentmethod,
school_income_date,
school_income_ipaddress
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
");
$stmt->bind_param('ssssssssss',
$money_income_account,
$pw_userId,
$money_income_description,
$money_income_amount,
$money_income_time,
$money_income_payer,
$money_income_category,
$money_income_paymentmethod,
$todayDt,
$actIp
);
$stmt = $mysqli->prepare("UPDATE
money_accounts
SET
money_accounts_catogeryamount = ?,
money_accounts_catogerylastupdated = ?
WHERE
money_accounts_catogeryid = ?"
);
$stmt->bind_param('sss',
$money_income_amount,
$todayDt,
$money_income_account
);
$stmt->execute();
$stmt->close();
header('index.php?page=income');
// Add Recent Activity
$activityType = '8';
$activityTitle = $newEntryNavLink.' "'.decodeit($money_income_description).' '.$createdText;
updateActivity($pw_userId,$activityType,$activityTitle);
$msgBox = alertBox('New income entry '."<b>\"".decodeit($money_income_description)."\"</b> ".$newcatMsg2, "<i class='fa fa-check-square'></i>", "alert-success");
// Clear the Form of values
$_POST['money_income_description'] = $_POST['money_income_amount'] = $_POST['money_income_time'] = '';
}
}
?>
如何在插入数据库表的同时更新另一个数据库表?我想插入一个新的“收入”条目,在执行此操作时,我希望插入收入的账户表将使用当前账户余额+新收入账户进行更新。还有更简单的方法吗
这是我的php代码
// Add New Entry
if (isset($_POST['submit']) && $_POST['submit'] == 'saveEntry') {
// User Validations
if($_POST['money_income_id'] == '...') {
$msgBox = alertBox($catReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_description'] == '') {
$msgBox = alertBox($money_income_descriptionReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_accounts_catogerytitles'] == '...') {
$msgBox = alertBox('Failed! Income Account Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_amount'] == '...') {
$msgBox = alertBox('Failed! Amount Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_time'] == '...') {
$msgBox = alertBox('Failed! Time Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_payer_title'] == '...') {
$msgBox = alertBox('Failed! Payer Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_incomecategory_title'] == '...') {
$msgBox = alertBox('Failed! Income Category Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_paymentmethod_title'] == '...') {
$msgBox = alertBox('Failed! Payment Method Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else {
$money_income_account = $_POST['money_accounts_catogerytitles'];
$money_income_description = encodeIt($_POST['money_income_description']);
$money_income_amount = $_POST['money_income_amount'];
$money_income_time = $_POST['money_income_time'];
$money_income_payer = $_POST['money_payer_title'];
$money_income_category = $_POST['money_incomecategory_title'];
$money_income_paymentmethod = $_POST['money_paymentmethod_title'];
$stmt = $mysqli->prepare("
INSERT INTO
money_income(
money_income_account,
id,
money_income_description,
money_income_amount,
money_income_time,
money_income_payer,
money_income_category,
money_income_paymentmethod,
school_income_date,
school_income_ipaddress
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
");
$stmt->bind_param('ssssssssss',
$money_income_account,
$pw_userId,
$money_income_description,
$money_income_amount,
$money_income_time,
$money_income_payer,
$money_income_category,
$money_income_paymentmethod,
$todayDt,
$actIp
);
$stmt = $mysqli->prepare("UPDATE
money_accounts
SET
money_accounts_catogeryamount = ?,
money_accounts_catogerylastupdated = ?
WHERE
money_accounts_catogeryid = ?"
);
$stmt->bind_param('sss',
$money_income_amount,
$todayDt,
$money_income_account
);
$stmt->execute();
$stmt->close();
header('index.php?page=income');
// Add Recent Activity
$activityType = '8';
$activityTitle = $newEntryNavLink.' "'.decodeit($money_income_description).' '.$createdText;
updateActivity($pw_userId,$activityType,$activityTitle);
$msgBox = alertBox('New income entry '."<b>\"".decodeit($money_income_description)."\"</b> ".$newcatMsg2, "<i class='fa fa-check-square'></i>", "alert-success");
// Clear the Form of values
$_POST['money_income_description'] = $_POST['money_income_amount'] = $_POST['money_income_time'] = '';
}
}
?>
//添加新条目
如果(isset($\u POST['submit'])和&$\u POST['submit']=='saveEntry'){
//用户验证
如果($_POST['money\u income\u id']=='…')){
$msgBox=alertBox($catReq,“,”警报危险“);
}否则如果($_POST['money\u income\u description']=''){
$msgBox=alertBox($money\u income\u descriptionReq,“,”警报危险“);
}如果($\u POST['money\u accounts\u catogerytitles']=='…'){
$msgBox=alertBox('失败!需要收入帐户','',“警报危险”);
}如果($_POST['money\u income\u amount']=='{
$msgBox=alertBox('Failed!Amount Required','','alert danger');
}如果($_POST['money\u income\u time']=='{
$msgBox=alertBox('失败!需要时间','',“警报危险”);
}如果($_POST['money\u payer\u title']=='…'){
$msgBox=alertBox('失败!需要付款人','',“警报危险”);
}如果($_POST['money\u incomecategory\u title']=='…')){
$msgBox=alertBox('失败!需要收入类别','',“警告危险”);
}如果($\u POST['money\u paymentmethod\u title']=='…')){
$msgBox=alertBox('失败!需要付款方式','',“警告危险”);
}否则{
$money\u income\u account=$\u POST['money\u accounts\u catogerytitles';
$money\u income\u description=encodeIt($\u POST['money\u income\u description']);
$money\u income\u amount=$\u POST['money\u income\u amount'];
$money\u income\u time=$\u POST['money\u income\u time'];
$money\u income\u payer=$\u POST['money\u payer\u title'];
$money\u income\u category=$\u POST['money\u incomecategory\u title';
$money\u income\u paymentmethod=$\u POST['money\u paymentmethod\u title';
$stmt=$mysqli->prepare(“
插入
货币收入(
货币收入账户,
身份证件
金钱、收入、描述,
货币、收入、金额,
金钱、收入、时间,
金钱、收入、付款人,
货币(收入)(类别),,
货币收入支付方法,
学校(收入)(日期),
学校收入地址
)价值观(
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
");
$stmt->bind_参数('ssss',
$money\u income\u账户,
$pw_userId,
$money\u income\u description,
$money\u income\u amount,
$money\u income\u time,
$money\u income\u payer,
$money\u income\u类别,
$money\u income\u paymentmethod,
$todayDt,
$actIp
);
$stmt=$mysqli->prepare(“更新
货币账户
设置
货币账户金额=?,
货币\账户\ catogerylastupdated=?
哪里
货币账户卡托格里德=?”
);
$stmt->bind_参数('sss',
$money\u income\u amount,
$todayDt,
$money\u income\u账户
);
$stmt->execute();
$stmt->close();
标题('index.php?page=income');
//添加最近的活动
$activityType='8';
$activityTitle=$newEntryNavLink.“.”。解码($money\u income\u description)。'.$createdText;
updateActivity($pw_userId、$activityType、$activityTitle);
$msgBox=alertBox(“新收入条目”。“\”。解码($money\u income\u description)。“\”.$newcatMsg2,“,“警报成功”);
//明确价值观的形式
$\u POST['money\u income\u description']=$\u POST['money\u income\u amount']=$\u POST['money\u income\u time']='';
}
}
?>
这是我的html代码
// Add New Entry
if (isset($_POST['submit']) && $_POST['submit'] == 'saveEntry') {
// User Validations
if($_POST['money_income_id'] == '...') {
$msgBox = alertBox($catReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_description'] == '') {
$msgBox = alertBox($money_income_descriptionReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_accounts_catogerytitles'] == '...') {
$msgBox = alertBox('Failed! Income Account Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_amount'] == '...') {
$msgBox = alertBox('Failed! Amount Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_income_time'] == '...') {
$msgBox = alertBox('Failed! Time Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_payer_title'] == '...') {
$msgBox = alertBox('Failed! Payer Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_incomecategory_title'] == '...') {
$msgBox = alertBox('Failed! Income Category Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else if($_POST['money_paymentmethod_title'] == '...') {
$msgBox = alertBox('Failed! Payment Method Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
} else {
$money_income_account = $_POST['money_accounts_catogerytitles'];
$money_income_description = encodeIt($_POST['money_income_description']);
$money_income_amount = $_POST['money_income_amount'];
$money_income_time = $_POST['money_income_time'];
$money_income_payer = $_POST['money_payer_title'];
$money_income_category = $_POST['money_incomecategory_title'];
$money_income_paymentmethod = $_POST['money_paymentmethod_title'];
$stmt = $mysqli->prepare("
INSERT INTO
money_income(
money_income_account,
id,
money_income_description,
money_income_amount,
money_income_time,
money_income_payer,
money_income_category,
money_income_paymentmethod,
school_income_date,
school_income_ipaddress
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
");
$stmt->bind_param('ssssssssss',
$money_income_account,
$pw_userId,
$money_income_description,
$money_income_amount,
$money_income_time,
$money_income_payer,
$money_income_category,
$money_income_paymentmethod,
$todayDt,
$actIp
);
$stmt = $mysqli->prepare("UPDATE
money_accounts
SET
money_accounts_catogeryamount = ?,
money_accounts_catogerylastupdated = ?
WHERE
money_accounts_catogeryid = ?"
);
$stmt->bind_param('sss',
$money_income_amount,
$todayDt,
$money_income_account
);
$stmt->execute();
$stmt->close();
header('index.php?page=income');
// Add Recent Activity
$activityType = '8';
$activityTitle = $newEntryNavLink.' "'.decodeit($money_income_description).' '.$createdText;
updateActivity($pw_userId,$activityType,$activityTitle);
$msgBox = alertBox('New income entry '."<b>\"".decodeit($money_income_description)."\"</b> ".$newcatMsg2, "<i class='fa fa-check-square'></i>", "alert-success");
// Clear the Form of values
$_POST['money_income_description'] = $_POST['money_income_amount'] = $_POST['money_income_time'] = '';
}
}
?>
<div class="modal fade slide-up disable-scroll" id="addNewAppModal" tabindex="-1" role="dialog" aria-labelledby="addNewAppModal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content-wrapper">
<div class="modal-content">
<div class="modal-header clearfix ">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i>
</button>
<h4 class="p-b-5"><span class="semi-bold">New</span> Income</h4>
</div>
<div class="modal-body">
<p class="small-text text-center">Create a new income using this form, make sure you fill them all</p>
<form action="" method="post">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="money_income_description">Description</label>
<input type="text" class="form-control" name="money_income_description" required="required" value="<?php echo isset($_POST['money_income_description']) ? $_POST['money_income_description'] : ''; ?>" />
<span class="help-block"><?php echo $money_income_descriptionHelp; ?></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="money_income_amount">Amount</label>
<input type="number" class="form-control" name="money_income_amount" required="required" value="<?php echo isset($_POST['money_income_amount']) ? $_POST['money_income_amount'] : ''; ?>" />
<span class="help-block"><?php echo $money_income_descriptionHelp; ?></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="money_income_time">Time</label>
<input id="date" type="text" name="money_income_time" required="required" class="form-control input-sm" value="<?php echo isset($_POST['money_income_time']) ? $_POST['money_income_time'] : ''; ?>" id="startDate">
<span class="help-block"><?php echo $money_income_descriptionHelp; ?></span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-2">
<div class="form-group">
<label for="money_accounts_catogerytitles">Select Account</label>
<select class="cs-select cs-skin-slide" required="required" data-init-plugin="cs-select" name="money_accounts_catogerytitles" id="money_accounts_catogerytitles">
<option value="...">Select</option>
<?php
$sqlStmt = "SELECT
money_accounts_catogeryid,
money_accounts_catogerytitles,
UNIX_TIMESTAMP(money_accounts_catogerydate) AS income3Date
FROM
money_accounts
WHERE
id = ".$pw_userId."
ORDER BY income3Date DESC";
$results = mysqli_query($mysqli, $sqlStmt) or die('-2'.mysqli_error());
?>
<?php while ($row = mysqli_fetch_assoc($results)) { ?>
<option value="<?php echo $row['money_accounts_catogerytitles']; ?>"><?php echo decodeit($row['money_accounts_catogerytitles']); ?></option>
<?php } ?>
</select>
</div>
</div>
新收入
使用此表格创建新收入,请确保填写所有收入
描述
由于您使用的是同一个变量($stmt
),第二个prepare
将取代第一个变量。请尝试使用$stmt1
和$stmt2
。如果您需要同时运行两个查询或不运行,请查看…或触发器\/请查看mysql触发器:是的,这是t