Bartosz XxX Bartosz XxX - 11 months ago 115
MySQL Question

Incrementing rows in the database

I have a form that adds / edits an event.
Namely, I wanted to extend it with the function of adding value automatically (just like increment and ++).
I have statistic_tips in the database containing two lines: stat_win and stat_false
Only during editing (because in principle the editing will usually consist in changing one record) after making changes, I would like to automatically increase the stat_win or stat_false line depending on the selected option (win, loss from results).
How do you achieve this, my dear?
Prompt? Possibly I would like to hear better solutions with a pleasant desire.
I apologize for a mistaken question, but I am a beginner
In red I marked the fragment, where I tried to figure it out, but unfortunately it failed.

<?php
function undefinedIndex(){
}
set_error_handler("undefinedIndex", E_NOTICE);
if(isset($_POST['add_tip'])){
$team1=$_POST['team1'];
$team2=$_POST['team2'];
$league=$_POST['league'];
$datentime=$_POST['datentime'];
$draw=$_POST['draw'];
$results=$_POST['results'];
$sql_insert="INSERT INTO premium_tips(datentime, league, team1, team2, draw, results) VALUES ('$datentime','$league','$team1','$team2','$draw','$results')";
if ($conn->query($sql_insert) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(isset($_POST['update_tip'])){
$team1=$_POST['team1u'];
$team2=$_POST['team2u'];
$league=$_POST['leagueu'];
$datentime=$_POST['datentimeu'];
$draw=$_POST['drawu'];
$results=$_POST['resultsu'];
$id=$_POST['id'];
[color="#FF0000"] $win1=$mysqli->query('SELECT stat_win FROM statistic_tips');
$loss1=$mysqli->query('SELECT stat_false FROM statistic_tips');
if($results=="win"){
$sql_insert="
UPDATE statistic_tips (stat_win) VALUES '$win1+=1'";
}
if($results=="loss"){
$sql_insert="
UPDATE statistic_tips (stat_false) VALUES '$loss1+=1'";
}[/color]
$sql_insert="
UPDATE premium_tips SET datentime='$datentime', league='$league',team1='$team1', team2='$team2',draw='$draw', results='$results' WHERE id='$id'";
if ($conn->query($sql_insert) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>


And here is the file responsible for the editing form

$sql_get = "SELECT * FROM matches WHERE match_id='$q'";
$result = $conn->query($sql_get);
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
echo '<form role="form" method="post" action=""
onreset="resetAction()">
<div class="form-group">
<label>Match info</label>

<input class="form-control" placeholder="Date of match" type="datetime-local" name="datentime" required><br>
<input class="form-control" placeholder="League" name="league" required><br>
<input class="form-control" placeholder="Stadium" name="stadium" required>
</div>
<div class="form-group">
<label>Home</label>
<input class="form-control" placeholder="Enter team name" name="team1" required>
</div>
<div class="form-group">
<label>Away</label>
<input class="form-control" placeholder="Enter team name" name="team2" required>
</div>
<button type="submit" class="btn btn-default" name="add_match">Add Match</button>
<button type="reset" class="btn btn-default">Reset</button>

</form>';


In fact, I mean automatic statistics on the WON / LOSS chart
At the beginning, when adding, each type has $ result = actual and when editing it is changed to either $ result = win or $ result = loss
I do not know if he combines well with these statistics.

Answer Source
UPDATE statistic_tips (stat_win) VALUES '$win1+=1'

This isn't valid SQL syntax.

What you're looking for is:

UPDATE statistic_tips SET stat_win = stat_win + 1 WHERE id = ?

Note that you don't need to select the previous value from the database beforehand -- the UPDATE query will increment it in place.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download