Bartosz XxX Bartosz XxX - 2 months ago 35
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.

function undefinedIndex(){
set_error_handler("undefinedIndex", E_NOTICE);
$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;
[color="#FF0000"] $win1=$mysqli->query('SELECT stat_win FROM statistic_tips');
$loss1=$mysqli->query('SELECT stat_false FROM statistic_tips');
UPDATE statistic_tips (stat_win) VALUES '$win1+=1'";
UPDATE statistic_tips (stat_false) VALUES '$loss1+=1'";
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);
while($row = $result->fetch_assoc()){
echo '<form role="form" method="post" action=""
<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 class="form-group">
<input class="form-control" placeholder="Enter team name" name="team1" required>
<div class="form-group">
<input class="form-control" placeholder="Enter team name" name="team2" required>
<button type="submit" class="btn btn-default" name="add_match">Add Match</button>
<button type="reset" class="btn btn-default">Reset</button>


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