简体   繁体   中英

Why is the data in my database not updating?

I am trying have a form update a row in my database. I click on one of the options for the first time, both choices get filled with '1'. Every click afterwards does nothing. I've checked a site to see if my syntax is correct and it said there were no errors. I haven't been working with PHP for long, so I don't really know where I would've gone wrong.

I'm sorry if the fix is super simple but as I said, I haven't been doing this for long.

The options are mine and my friend's names, and so I have censored them.

<?php
include_once('db_feat.php');

$vote1 = $_POST['friend'];
$vote2 = $_POST['me'];
$vote = '';

$select = "SELECT * FROM `vote` WHERE 1";
mysqli_query($db_feat, $select);

$currentfriend = $row['friend'];
$currentme = $row['me'];

$finalfriend = '';
$finalme = '';

if($vote == $votefriend) {
    $finalfriend = $currentfriend + 1;
} else {
    $finalfriend = $currentfriend + 0;
}
if($vote == $voteme) {
    $finalme = $currentme + 1; 
} else {
    $finalme = $currentme + 0;
}

$insert = "UPDATE `vote` SET `friend`=$finalfriend,`me`=$finalme WHERE 1";
mysqli_query($db_feat, $insert);

if (!mysqli_query($db_feat,$insert)){
    echo("Error description: " . mysqli_error($db_feat));
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>index.php</title>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="Media/Images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="CSS/divs.css">
    <link rel="stylesheet" type="text/css" href="CSS/styles.css">
    </head>

<body>
    <?php include_once('template_pageTop.php'); ?>
    <div id="pageMiddle">
        <div id="content">
            <div id="poll">
                <form action="index.php" method="post">
                    <input type="image" src="Media/Images/votefriend.png" name="friend" id="friend">
                    <input type="image" src="Media/Images/voteme.png" name="me" id="me">
                </form>
            </div>

            <?php echo $finalfriend; ?></br />
            <?php echo $finalme; ?>
        </div>
    </div>
    <?php include_once('template_pageBottom.php'); ?>
</body>

Here is the code I got from my database.

CREATE TABLE IF NOT EXISTS `vote` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`friend` int(11) NOT NULL,
`me` int(11) NOT NULL,
PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

it should be something like this.

table

initial value

my_name | my_friend_name
1       | 1

After update

my_name | my_friend_name
2       | 2

2 or something else according to your requirements

First of all, looks like the syntax of your queries are wrong. Your WHERE clause should look something like these.

$select = "SELECT * FROM `vote` WHERE friend = 1";
$insert = "UPDATE `vote` SET `friend`= {$final1},`me`= {$final2} WHERE friend = 1";

Something else to consider. In this part of your code, you are doing $var + 0 which doesn't accomplish anything, you are better off using the original value. Also try to use the identical comparison operator (=== instead of ==) so you'll know the values are exactly alike.

if($vote === $votefriend) {
    $finalfriend = $currentfriend + 1;
} else {
    $finalfriend = $currentfriend;
}
if($vote === $voteme) {
    $finalme = $currentme + 1; 
} else {
    $finalme = $currentme;
}

Furthermore, you never want to blindly assign values to variables and try to use them. Always validate/sanitize. You should have some sort of structure like this.

if ($_POST['form']) {
    if ($_POST['friend']) {
        $vote1 = $_POST['friend'];
    } else {
        echo "Friend is empty";
        exit;
    }
    if ($_POST['me']) {
    $vote2 = $_POST['me'];
    } else {
        echo "Me is empty";
        exit;
    }

    //rest of your code//
} else {
    echo "form was not received";
    exit;
}

If your $_POST values are empty, obviously they are not making it from the form to your PHP script possibly because it was not submitted.

I would highly encourage you to form good habits and techniques from the beginning which keeps you from having to go back later and fix bad code (been there).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM