I have problem updating the mySQL table i have simple loop to display the rows in the table in HTML FORM but when i try to update the row notting happens, no row is updated!
here is the code

<form action="" method="post">
<?php
$sql = "SELECT ID, votemodelName, votemodelImage, modelLink, votes, ip_address FROM vote ORDER BY ID LIMIT 3";
$result = $conn->query($sql);

$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));

if (!$result) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $ID = $row["ID"];
        $votemodelName = $row["votemodelName"];
        $votemodelImage = $row["votemodelImage"];
        $modelLink = $row["modelLink"];
        $votes = $row["votes"];
        $ip_address = $row["ip_address"];
    ?>
<div class="col-xs-4 col-md-4">
    <a href="<?= $modelLink ?>" target="_blank" class="thumbnail">
        <img src="<?= $votemodelImage ?>" class="thumb-vote-image" alt="Amanda">
    </a>
    <h1><button type="submit" name="vote_<?= $ID ?>" class="btn btn-info btn-lg">VOTE</button></h1>
    <h3><?= $votemodelName ?></h3>
</div>

<?php
}
if(isset($_POST["vote_".$ID])) {
    $voteModel = $_POST["vote_".$ID];

    $sql = "UPDATE vote SET votes = votes + 1 WHERE ID='".$voteModel."'";
    $result = $conn->query($sql);

}
}
?>
</form>

Recommended Answers

All 12 Replies

Two thoughts.

  1. Line 34 what is in the result?
  2. Is this the old deprecated mysql calls? I don't see the full code so it's a little light to me.
$conn = mysqli_connect($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

Sorry i have forgoten

Sorry i have forgoten here is the code but its mysqli

$DBServer = 'localhost';
$DBUser   = 'root';
$DBPass   = '';
$DBName   = 'voting';

$conn = mysqli_connect($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

Still wondering what's in $result from line 34. Or are you writing the connect in the last 9 lines of code fails?

Try hard coding a value into Line 33. It looks like the variable $voteModel might not be interpreted.
In line 31,

$voteModel = $_POST["vote_".$ID];

shouldn't that be

$voteModel ="vote_".$_POST[$ID];

?

I think not it gives me error when i click on the last button for vote vote_3
Notice: Undefined offset: 3 in C:\xampp\htdocs\example\includes\featured.php on line 43

line 31 in the code here when i replace it with the @Norman_4 example

I think isset($_POST["vote_".$ID]) is not set in $_POST

check if that variable exists in $_POST

var_dump($_POST);

i get an error now
array(1) { ["vote_3"]=> string(0) "" } Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\example\includes\featured.php:12 Stack trace: #0 C:\xampp\htdocs\example\index.php(31): include() #1 {main} thrown in C:\xampp\htdocs\example\includes\featured.php on line 12

Heres the full code

<form action="" method="post">
<?php
$sql = "SELECT ID, votemodelName, votemodelImage, modelLink, votes, ip_address FROM vote ORDER BY ID LIMIT 3";
$result = $conn->query($sql);

$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));

if (!$result) {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $ID = $row["ID"];
        $votemodelName = $row["votemodelName"];
        $votemodelImage = $row["votemodelImage"];
        $modelLink = $row["modelLink"];
        $votes = $row["votes"];
        $ip_address = $row["ip_address"];
    ?>
<div class="col-xs-4 col-md-4">
<a href="<?= $modelLink ?>" target="_blank" class="thumbnail">
<img src="<?= $votemodelImage ?>" class="thumb-vote-image" alt="Amanda">
</a>
<h1><button type="submit" name="vote_<?= $ID ?>" class="btn btn-info btn-lg">VOTE</button></h1>
<h3><?= $votemodelName ?></h3>
</div>
    <?php
    if(isset($_POST["vote_".$ID])) {
        $voteModel = $_POST["vote_".$ID];
        $sql = "UPDATE vote SET votes = votes + 1 WHERE ID='".$voteModel."'";
        $result = $conn->query($sql);
    }
}
}
?>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.