Answer the question
In order to leave comments, you need to log in
Changing multiple records at once in mysql+php?
I am making a tester, there is a table with questions:
CREATE TABLE `questions` (
`id` int(11) NOT NULL,
`question` varchar(255) NOT NULL,
`ans1` varchar(255) NOT NULL,
`ans2` varchar(255) NOT NULL,
`ans3` varchar(255) NOT NULL,
`ans4` varchar(255) NOT NULL,
`ans5` varchar(255) NOT NULL,
`ans` int(11) NOT NULL,
`cat_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?php
header('Content-Type: text/html; charset=utf-8');
if(isset($_POST['item_id'], $_POST['item_question'])){
require("../includes/connection.php"); // Подключение к БД
$item_id = $_POST['item_id'];
$item_question = $_POST['item_question'];
$item_ans1 = $_POST['item_ans1'];
$item_ans2 = $_POST['item_ans2'];
$item_ans3 = $_POST['item_ans3'];
$item_ans4 = $_POST['item_ans4'];
$item_ans5 = $_POST['item_ans5'];
$item_ans = $_POST['item_ans'];
// Объединяем массивы
$allData = array_map(null,$item_id, $item_question, $item_ans1, $item_ans2, $item_ans3, $item_ans4, $item_ans5, $item_ans);
// Генерируем запрос
$query = "INSERT INTO `questions` (`id`,`question`,`ans1`,`ans2`,`ans3`,`ans4`,`ans5`,`ans`) VALUES ";
$params = array();
foreach($allData as $val){
$params[] = '('.(int)$val[0] .','.(string)$val[1].','.(string)$val[2].','.(string)$val[3].','.(string)$val[4].','.(string)$val[5].','.(string)$val[6].','.(string)$val[7].')';
}
$query .= implode(',', $params) . " ON DUPLICATE KEY UPDATE `question`,`ans1`,`ans2`,`ans3`,`ans4`,`ans5`,`ans` = VALUES(`question`,`ans1`,`ans2`,`ans3`,`ans4`,`ans5`,`ans`)";
echo $query; // можно вывести его на экран для проверки
// Выполняем запрос
$res = mysql_query($query, $con);
echo (mysql_affected_rows($con) > 0 ? 'Данные успешно обновлены!' : 'Не произведено ни одной замены'),
'<br><a href="./">Вернутся</a> на страницу редактирования.';
}
?>
No substitutions madeDoes anyone know a solution or another way
Answer the question
In order to leave comments, you need to log in
CREATE TABLE `questions` (
`id` int(11) NOT NULL,
`question` varchar(255) NOT NULL,
`ans1` varchar(255) NOT NULL,
`ans2` varchar(255) NOT NULL,
`ans3` varchar (255) NOT NULL,
`ans4` varchar(255) NOT NULL,
`ans5` varchar(255) NOT NULL,
`ans` int(11) NOT NULL,
`cat_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* выполнение подготовленного запроса */
mysqli_stmt_execute($stmt);
printf("%d строк вставлено.\n", mysqli_stmt_affected_rows($stmt));
/* закрываем запрос */
mysqli_stmt_close($stmt);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question