function Insert($table, $values, $validation = null) { global $connect; $validation_error = array(); $res = array('id' => null, 'message' => null); $columns = array_keys($values); $placeholders = array_fill(0, count($columns), "?"); $columns_exist = array_filter($columns, function ($column) use ($table) { return tableExists($table, $column); }); $columns_not_exist = array_diff($columns, $columns_exist); foreach ($columns_not_exist as $column) { $res['message'] = 'خطا: ستون ' . $column . ' وجود ندارد ' . $table; unset($values[$column]); } if ($res['message']) { return $res; } $sql = "INSERT INTO $table (" . implode(", ", $columns_exist) . ")VALUES (" . implode(", ", $placeholders) . ")"; $connect->beginTransaction(); $result = $connect->prepare($sql); $i = 1; foreach ($values as $value) { $colType = getColumnType($table, $columns[$i-1]); if (!is_numeric($value) && ($colType == 'int' || $colType == 'float')) { $res['message'] = "خطا: داده غیر مجاز حروف برای عدد." . $table . " - " . $columns[$i-1]; return $res; } if (is_float($value) && $colType == 'int') { $res['message'] = "خطا: داده غیر مجاز اعشاری برای عدد" . $table . " - " . $columns[$i-1]; return $res; } $result->bindValue($i, $value); $i++; } if ($result->execute()) { $id = $connect->lastInsertId(); $connect->commit(); $res['id'] = $id; return $res; } else { $res['message'] = 'خطا در دیتابیس'.$table; return $res; } }

تابع زیر را بهینه کن و آن را دوباره بنویسfunction Insert$table $values $validation = null global $connect; $validation_error = array; $res = arrayid = null message = null; foreach $values

原文地址: http://www.cveoy.top/t/topic/b3XJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录