我需要我的表单发布INSERT或UPDATE数据,具体取决于列是否已存在。 INSERT查询工作正常,但UPDATE没有。我想这可能是因为最初的sql查询($ query)失败了,很可能是因为它没有找到$ Unique_Ref POST数据。如果是这种情况,如何从$ invoice_data数组中提取此单个值?
这是表单页面代码:
if (isset($_GET['success']) && empty($_GET['success'])) {echo 'Record saved'; } else {if (empty($_POST) === false && empty($errors) === true) {$invoice_data = array(
'Unique_Ref' => mysqli_real_escape_string($conn, $_POST['Unique_Ref']),
'Supplier_Name' => mysqli_real_escape_string($conn, $_POST['Supplier_Name']),
'Supplier_Invoice_Ref' => mysqli_real_escape_string($conn, $_POST['Supplier_Invoice_Ref']),
'Office' => mysqli_real_escape_string($conn, $_POST['Office']),
'Loss_Surplus_Amount' => mysqli_real_escape_string($conn, $_POST['Loss_Surplus_Amount']),
'Loss_Surplus_Currency' => mysqli_real_escape_string($conn, $_POST['Loss_Surplus_Currency']),
'Outcome' => mysqli_real_escape_string($conn, $_POST['Outcome']));
save_invoice($invoice_data);header('Location: invoices.php?success'); exit();
} else if (empty($errors) === false) {echo output_errors($errors);} ?> *html form....*
这是save_invoice()函数:
function save_invoice($invoice_data) {
global $conn;
array_walk($invoice_data, 'array_sanitize');
$fields = '`' . implode('`, `', array_keys($invoice_data)) . '`';
$data = '\'' . implode('\', \'', $invoice_data) . '\'';
$query = mysqli_query($conn, "SELECT * FROM `invoicelog` WHERE `Unique_Ref` = '$Unique_Ref'");
$result = mysqli_num_rows($query);
if($result > 0) {
mysqli_query($conn, "UPDATE `invoicelog` SET $fields = $data WHERE `Unique_Ref` = '$Unique_Ref'");
} else {
mysqli_query($conn, "INSERT INTO `invoicelog` ($fields) VALUES ($data)");
}
非常欢迎任何建议。正如我所提到的,最终的INSERT查询工作正常,这是UPDATE查询无效。
答案 0 :(得分:3)
你必须配对
$fields = '`' . implode('`, `', array_keys($invoice_data)) . '`';
$data = '\'' . implode('\', \'', $invoice_data) . '\'';
修改强>
我已经编辑了这样的代码:
$fields = array_keys($invoice_data)[$i];
$data = $invoice_data;
实现下面的目标,它对我有用..嗯..
结束修改
类似的东西:
'fields[0] = $data[0], ...' //and so on, place it in a loop. or whatever you prefer.. :)
因为更新它时(field1 = value1, field2 = value)
<强> EDIT2 强>
实际测试代码:
结束修改
也许这样的事情可以吗?
$set_arr = array();
for ($i = 0; $i < count($data); $i++)
{
$set_arr[] = "`".array_keys($invoice_data)[$i]/*$fields*/."` = '".$data[$i]."' ";
}
$setString = implode(',', $set_arr);
/*
Result ]> `Unique_Ref` = '1' ,`Supplier_Name` = '2' ,`Supplier_Invoice_Ref` = '3' ,`Office` = '4' ,`Loss_Surplus_Amount` = '5' ,`Loss_Surplus_Currency` = '6' ,`Outcome` = '7'
]> from my test
*/
然后你的更新语句将是这样的:
"UPDATE `invoicelog` SET ($setString) `Unique_Ref` = '$Unique_Ref'
希望我能帮助你......我离开这里,快乐的编码干杯!
答案 1 :(得分:1)
您无法在单个表达式中为多个字段设置多个值,例如SET $fields = $data
。您应该分别为每个字段设置每个值:SET field1='value1', field2='value2', ...
答案 2 :(得分:0)
确保为字符串值插入引号('):
mysqli_query($conn, "UPDATE `invoicelog` SET $fields = '$data' WHERE `Unique_Ref` = '$Unique_Ref'");
答案 3 :(得分:0)
// mysqli_query($ conn,“UPDATE invoicelog
SET $ fields = $ data WHERE Unique_Ref
='$ Unique_Ref'”);
使用以上行更新以下代码。
$str = '';
foreach($invoice_data as $field=>$val ){
$str = $str.",".$field."=".$val;
}
$str = substr($str,1);
mysqli_query($conn, "UPDATE `invoicelog` SET $str WHERE Unique_Ref = '$Unique_Ref'");