嘿大家我试图运行两个插入语句,最后在php数组中运行更新语句,但似乎没有正常工作。我意识到这个代码有一些问题,例如在将数据插入数据库之前转义html数据并不理想,但是我将在以后修复,我唯一关心的是插入和更新语句。以下是我正在使用的代码:
这是正在构建的作业数据阵列。
if(empty($_POST) === false && empty($errors) === true){
date_default_timezone_set('America/Denver');
$datetime =date("Y-m-d H:i:s");
$submissionId = rand(10000,99999);
$req_data = array(
'itemId' => $i_san,
'itemName' => $_POST['itemName'],
'submissionId' => $submissionId,
'username' => $_SESSION['username'],
'email' => $_SESSION['email'],
'subDate' => $datetime,
'wistiaId' => $_SESSION['wistiaId']
);
add_DCRequests($req_data);//INSERT INTO `DCrequests`
// === Sanatize inputs === //
$text_1_raw = $_POST['textOne'];
$text_1_noQuotes = str_replace('"',"'",$text_1_raw);
//$text_one = utf8_encode("Ñ");
$text_one = htmlentities(trim($text_1_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_2_raw = $_POST['textTwo'];
$text_2_noQuotes = str_replace('"',"'",$text_2_raw);
$text_two = htmlentities(trim($text_2_noQuotes) , ENT_QUOTES , 'UTF-8' );
$text_3_raw = $_POST['textThree'];
$text_3_noQuotes = str_replace('"',"'",$text_3_raw);
$text_three = htmlentities(trim($text_3_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_4_raw = $_POST['textFour'];
$text_4_noQuotes = str_replace('"',"'",$text_4_raw);
$text_four = htmlentities(trim($text_4_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_5_raw = $_POST['textFive'];
$text_5_noQuotes = str_replace('"',"'",$text_5_raw);
$text_five = htmlentities(trim($text_5_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_6_raw = $_POST['textSix'];
$text_6_noQuotes = str_replace('"',"'",$text_6_raw);
$text_six = htmlentities(trim($text_6_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_7_raw = $_POST['textSeven'];
$text_7_noQuotes = str_replace('"',"'",$text_7_raw);
$text_seven = htmlentities(trim($text_7_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_8_raw = $_POST['textEight'];
$text_8_noQuotes = str_replace('"',"'",$text_8_raw);
$text_eight = htmlentities(trim($text_8_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_9_raw = $_POST['textNine'];
$text_9_noQuotes = str_replace('"',"'",$text_9_raw);
$text_nine = htmlentities(trim($text_9_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_10_raw = $_POST['textTen'];
$text_10_noQuotes = str_replace('"',"'",$text_10_raw);
$text_ten = htmlentities(trim($text_10_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_11_raw = $_POST['textEleven'];
$text_11_noQuotes = str_replace('"',"'",$text_11_raw);
$text_eleven = htmlentities(trim($text_11_noQuotes) , ENT_QUOTES , 'UTF-8');
$text_12_raw = $_POST['textTwelve'];
$text_12_noQuotes = str_replace('"',"'",$text_12_raw);
$text_twelve = htmlentities(trim($text_12_noQuotes) , ENT_QUOTES , 'UTF-8');
$aep = escape_data($_POST['aep']);
$output = escape_data($_POST['output']) . "_" . $_POST['subId'];
$output_scrub = preg_replace('/[^A-Za-z0-9\-_]/', "", $output);
$rendStatus = "ready";
//parse out 3 items from POST target (display shape matrix | disp w | disp h)
$item = escape_data($_POST['target']);
$get_target_w_h = explode('|', $item);
$targ = escape_data($get_target_w_h[0]);
$w = escape_data($get_target_w_h[1]);
$h = escape_data($get_target_w_h[2]);
$matrix = $w ."x". $h;
$BGColor = escape_data($_POST['hex']);
$bg_scrub = preg_replace('/[^A-Za-z0-9\-]/', "", $BGColor);
$c1 = escape_data($_POST['hex2']);
$c1_scrub = preg_replace('/[^A-Za-z0-9\-]/', "", $c1);
$c2 = escape_data($_POST['hex3']);
$c2_scrub = preg_replace('/[^A-Za-z0-9\-]/', "", $c2);
$c3 = escape_data($_POST['hex4']);
$c3_scrub = preg_replace('/[^A-Za-z0-9\-]/', "", $c3);
// if user is banner attach value = 2
if($_SESSION['userLevel'] == 5){$attach = 2;}else{$attach = 0;}
$show = escape_data($_POST['hide']);
if($show === '1'){
$show_1_val = '{{on}}';
$show_2_val = '{{off}}';
$show_3_val = '{{off}}';
}elseif($show === '2'){
$show_1_val = '{{off}}';
$show_2_val = '{{on}}';
$show_3_val = '{{off}}';
}elseif($show === '3'){
$show_1_val = '{{off}}';
$show_2_val = '{{off}}';
$show_3_val = '{{on}}';
}
$show1_scrub = preg_replace('/[^a-z\{}]/', "", $show1);
$show2 = escape_data($_POST['HideShowLayer2']);
$show2_scrub = preg_replace('/[^a-z\{}]/', "", $show2);
$show3 = escape_data($_POST['HideShowLayer3']);
$show3_scrub = preg_replace('/[^a-z\{}]/', "", $show3);
$still = escape_data($_POST['HideShowLayer1']);
$stillFrame = preg_replace('/[^0-9\.{}]/', "", $custStillFrame);
if($target_file1 != ""){
$image1 = "https://www.test.com/ce/". escape_data($target_file1);}
else{
$image1 = "";
}
if($target_file2 != ""){
$image2 = "https://www.test.com/ce/". escape_data($target_file2);}
else{
$image2 = "";
}
if($target_file3 != ""){
$image3 = "https://www.test.com/ce/". escape_data($target_file3);}
else{
$image3 = "";
}
$completion_date="";
$DCjobsFileId="";
$itemName = escape_data($_POST['itemName']);
$estimatedTime = $currentRendTotal + $custEstRenderTime;
$mydate = date('m/d/Y');
// === Data to insert into the table === //
$job_data = array(
//'bannerToken' => $bannerToken,
'attach' => $attach,
'full-date' => $mydate,
'aep' => $aep,
'target' => $targ,
'output' => $output_scrub,
'itemName' => $itemName,
'render-status' => $rendStatus,
'est_render_time' => $_POST['renderEst'],
'frameNumber' => $stillFrame,
'CustomerName' => $_SESSION['first_name'],
'CustomerEmail' => $_SESSION['email'],
'CustomerKey' => $_SESSION['wistiaId'],
'submissionDate' => $datetime,
'submissionId' => $_POST['subId'],
'itemId' => $custItemId,
'matrix' => $matrix,
'fileformat' => $_POST['format'],
'BGColor' => $bg_scrub,
'ColorOne' => $c1_scrub,
'ColorTwo' => $c2_scrub,
'ColorThree' => $c3_scrub,
'Text-One' => $text_one,
'Text-Two' => $text_two,
'Text-Three' => $text_three,
'Text-Four' => $text_four,
'Text-Five' => $text_five,
'Text-Six' => $text_six,
'Text-Seven' => $text_seven,
'Text-Eight' => $text_eight,
'Text-Nine' => $text_nine,
'Text-Ten' => $text_ten,
'Text-Eleven' => $text_eleven,
'Text-Twelve' => $text_twelve,
'HideShowOne' => $show_1_val,
'HideShowTwo' => $show_2_val,
'HideShowThree' => $show_3_val,
'ImageUploadOne' => $image1,
'ImageUploadTwo' => $image2,
'ImageUploadThree' => $image3,
'completion_date' => "CRAP",
'DCjobsFileId' => "CRAP"
);
add_jobs($job_data);//"INSERT INTO `DCjobs` and INSERT INTO `DCjobsArchive`
header('Content-Type: text/html; charset=utf-8');
header('Location: https://www.test.com/ce/thanks.php?est='.$estimatedTime);
exit();
}elseif(empty($errors) === false){
$reportErrors = "<br /><br /><br />Oops, the following errors occured: <br />" . $errors . "<br /><br /> Please click <a href=\"customize.php\">here</a> to try again. <br /><br />";
}
?>
这是插入和更新jobdata数组
中的数据的函数function add_jobs($job_data){
global $db_conx;
array_walk($job_data, 'array_sanitize');
$jobfields = '`' . implode('`, `', array_keys($job_data)) . '`';
$jobdata = '\'' . implode('\', \'', $job_data) . '\'';
mysqli_query($db_conx, "INSERT INTO `DCjobs` ($jobfields) VALUES ($jobdata)");
mysqli_query($db_conx, "INSERT INTO `DCjobsArchive` ($jobfields) VALUES ($jobdata)");
$selectmaxdcjobsid="SELECT FileRowID, submissionDate FROM DCjobs WHERE submissionDate=(SELECT MAX(submissionDate) FROM DCjobs)";
mysqli_query($db_conx, $selectmaxdcjobsid);
while($row=mysqli_fetch_assoc($selectmaxdcjobsid)){
$maxdcjobsfileid=$row['FileRowID'];
$maxdcjobsubdate=$row['submissionDate'];
}
$selectarchiveid="select submissionId, submissionDate from DCjobsArchive where submissionDate='$maxdcjobsubdate'";
while($row=mysqli_fetch_assoc($selectarchiveid)){
$archivesubmissionid=$row['submissionId'];
$archivesubmissiondate=$row['submissionDate'];
}
$update="UPDATE DCjobsArchive SET DCjobsFileId='$maxdcjobsfileid' WHERE submissionId='$archivesubmissionid'";
mysqli_query($db_conx, $update);
}
答案 0 :(得分:0)
您似乎正在尝试将ID分配给刚刚插入的行,因此您可以填写外键。您可以使用MySQL内置函数LAST_INSERT_ID
来完成此任务。
function add_jobs($job_data){
global $db_conx;
array_walk($job_data, 'array_sanitize');
mysqli_query($db_conx, "INSERT INTO `DCjobs` ($jobfields) VALUES ($jobdata)");
mysqli_query($db_conx, "INSERT INTO `DCjobsArchive` (DCjobsFileId, $jobfields) VALUES (LAST_INSERT_ID(), $jobdata)");
}