samedi 25 juin 2016
Insert on duplicate key update - insert won't post anything into db, update does
I've built a "master table" that fetches data from 4 tables. The purpose of this table is that users can place bets on football games - therefore I'm displaying all available matches plus the bets that are already available (but also the games where no bets are available yet). This "placing bets" function is built as INSERT ON DUPLICATE KEY UPDATE.
When I now try to insert a new bet (aka "Tipp" in the code below) it won't create a new database entry. On the other hand when I update an entry (which I have added directly via phpMyAdmin) via ON DUPLICATE KEY UPDATE everything works fine and existing entries in the table tippsget updated.
Any idea why INSERT won't work? Please let me know when you need to know more about the code.
Master table
<form method="POST" action="savebets.php">
<table style="text-align: left; width: 100%">
<tr>
<th>date</th>
<th>time</th>
<th>home</th>
<th></th>
<th></th>
<th></th>
<th>away</th>
<th colspan="2">tipp</th>
</tr>
<?php
mysqli_select_db($conn,"allerlei");
$names = mysqli_query($conn,"set names utf8");
$results = mysqli_query($conn, "
//SQL query, see below
");
while($pers = mysqli_fetch_assoc($results)) {
?>
<tr>
<td><?php echo $pers["id"] ?><input type="hidden" name="id[]" value="<?php echo $pers["id"]?>"></td>
<td><?php echo $pers["date"] ?></td>
<td><?php echo $pers["time"]?></td>
<td><?php echo $pers["match_id"]?><input type="hidden" name="match_id[]" value="<?php echo $pers["match_id"]?>"></td>
<td><?php echo $pers["home_name"]?></td>
<td><?php echo $pers["goals_home"]?>
</td>
<td>:</td>
<td><?php echo $pers["goals_away"]?></td>
<td><?php echo $pers["away_name"]?></td>
<input type="hidden" name="user_id[]" value="<?php echo $pers["user_id"]?>">
<td><input type="tel" size="1" maxlength="2" name="tipp_home[]" value="<?php echo $pers["tipp_home"]?>"></td>
<td><input type="tel" size="1" maxlength="2" name="tipp_away[]" value="<?php echo $pers["tipp_away"]?>"></td>
<input type="hidden" name="tipp_id[]" value="<?php echo $pers["tipp_id"]?>">
</tr>
<?php
}
?>
</table>
<table>
<tr><td><input type="SUBMIT" name="submitbutton" value="Tipps speichern"></td></tr>
</table>
</form>
action script (savebets.php)
<?php
session_start();
require("connect.php");
$tipp_id = $_POST["tipp_id"];
$match_id = $_POST["match_id"];
$user_id = $_POST["user_id"];
$goals_home = $_POST["tipp_home"];
$goals_away = $_POST["tipp_away"];
$failsCount = 0;
foreach($_POST['id'] as $key => $value) {
if(!$goals_home[$key] && !$goals_away[$key]) {
continue;
}
$result="
INSERT INTO tipps
(tipp_id, match_id, user_id, goals_home, goals_away)
VALUES
('$tipp_id', '$match_id[$key]', '$user_id[$key]', '$goals_home[$key]', '$goals_away[$key]')
ON DUPLICATE KEY UPDATE
match_id = '".$match_id[$key]."',
user_id = '".$user_id[$key]."',
goals_home = '".$goals_home[$key]."',
goals_away = '".$goals_away[$key]."'
";
$query=mysqli_query($conn,$result);
if(!$query) {
$failsCount++;
}
} else {
$notSavedCount++;
}
if($failsCount == 0 && $notSavedCount == 0) {
mysqli_close($conn);
$_SESSION['msg'] = "Succes! Deine Tipps wurden gespeichert.";
header("Location:mastertable.php#bottom");
} elseif($failsCount !=0) {
echo "Fail, an error occured, try again.";
} else {
mysqli_close($conn);
$_SESSION['msg'] = "Succes! Tipps gespeichert. <b>Hinweis</b>: Du hast noch offene Tipps! Bitte fülle alle Felder aus.";
header("Location:mastertable.php#bottom");
}
?>
SQL Query
SELECT
matchschedule.id,
matchschedule.home,
matchschedule.away,
matchschedule.goals_home,
matchschedule.goals_away,
DATE_FORMAT(matchschedule.date, "%d.%m.%Y") AS date,
DATE_FORMAT(matchschedule.time, "%H:%i") AS time,
home.name AS home_name,
away.name AS away_name,
tipps.goals_home AS tipp_home,
tipps.goals_away AS tipp_away,
tipps.punkte_tipp,
tipps.tipp_id,
tipps.match_id,
login.user,
login.id AS user_id
FROM matchschedule LEFT JOIN teams home
ON matchschedule.home=home.id
LEFT JOIN teams away
ON matchschedule.away=away.id
LEFT JOIN tipps
ON matchschedule.id = tipps.match_id
LEFT Join login
ON tipps.user_id = login.id
WHERE user = '".$_SESSION['username']."' OR tipp_id is NULL
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire