PHP在多个查询和表上插入和更新

时间:2016-08-04 03:42:10

标签: php mysql mysqli

在那里我想在一个条件下进行更新和创建,所以当我创建一个新记录时,如果我成功制作新记录,则自动更新我的数据。

这里是我的PHP:

<?php 
require "dbconnection.php";

$a = array();
$a['transidmerchant'] = $_POST['TRANSIDMERCHANT'];
$a['totalamount'] =$_POST['AMOUNT'];
$a['words'] = $_POST['WORDS'];
$a['payment_channel'] = $_POST['PAYMENTCHANNEL'];
$a['session_id'] = $_POST['SESSIONID'];
$a['payment_date_time'] = $_POST['REQUESTDATETIME'];
$a['trxstatus'] = 'Requested';
$query = "INSERT INTO doku (transidmerchant,totalamount,words,payment_channel,session_id,payment_date_time,trxstatus) 
VALUES ('$_POST[TRANSIDMERCHANT]','$_POST[AMOUNT]','$_POST[WORDS]','$_POST[PAYMENTCHANNEL]','$_POST[SESSIONID]','$_POST[REQUESTDATETIME]','Requested')";

$sql = "UPDATE orders SET status='Paid' where id='$_POST[TRANSIDMERCHANT]'";
if(mysqli_query($con,$query)) {
    mysqli_connect($con,$sql);
    echo 1;
}else{
    echo("Error description: " . mysqli_error($con));
}

我的查询:$query$sql

我希望$sql$query成功创建

时更新class CustomTableViewCell: UITableViewCell { @IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var userImageView: UIImageView! @IBOutlet weak var captionLabel: UILabel! @IBOutlet weak var gradientView: UIView! var photo: Photo! { didSet { self.updateUI() } } override func layoutSubviews() { super.layoutSubviews() userImageView.layer.cornerRadius = 0.5 * self.userImageView.frame.width print("\(self.userImageView.frame.width)") // print out 1000.0 !! WHY? userImageView.layer.cornerRadius = 24 // IT WORKS! } private func updateUI() { self.myImageView.image = UIImage(named: photo.name) self.userImageView.image = UIImage(named: photo.user) self.captionLabel.text = photo.caption print("\(self.userImageView.frame.width)") // print out 1000.0 !! Same here } }

2 个答案:

答案 0 :(得分:0)

if (mysqli_query($con, $query) === true) {
    mysqli_query($con, $sql);
    echo 1;
} else {
    echo('Error description: ' . mysqli_error($con));
}

答案 1 :(得分:0)

创建插入然后更新的存储过程。您可能希望执行类似这样的操作,以避免发出检查子查询的常规查询,并转移到创建存储过程。

创建您的程序下面的类似内容并在您的sql对话框中运行它。完成后,运行它:

DELIMITER //
CREATE PROCEDURE Payment
(
a_transidmerchant int,
a_atotalamount float,
a_words varchar(200),
a_payment_channel varchar(200),
a_session_id int,
a_payment_date_time datetime,
etc...
)
BEGIN
insert into doku(field_name1, field_name2, field_name3, field_name4) values(a_field1, a_field2, a_field3, a_field4);
END //
DELIMITER;

现在,在您的php文件中,执行以下操作:

if(isset($_POST[transidmerchantid])) /**** start a post check ****/ 
                                     //before you touch the db
{
 $con = mysqli_connect("localhost","user","pass","database");

 //start defining variables
 $transidmerchantid = $_POST[name];
 $totalamount = $_POST[course];
 $words = $_POST[words];

 //calling stored procedure - call values for parameters in stored procedure
 $sql = "CALL Payment('$transidmerchantid','$totalamount','$words')"; // <----

 //in the order of operation, meaning once you have inserted the data, 
 //you can update the table. you're automatically updating the table row 
 //based on a successful insert, which is after calling the insert row           
 //stored procedure. 

 $result = mysqli_query($con,$sql);
 if($result) //insert successful.
    echo "Record Added Successfully!";
    $sql = "UPDATE orders SET status='Paid' where id='$_POST[TRANSIDMERCHANT]'";
    mysqli_query($con,$query);
 }else{
    echo("Error description: " . mysqli_error($con));
 }else{ 
    echo "Record Not added!"; //insert unsuccessful.
 } 

} /**** end post check ****/