【问题标题】:Database column doesn't get updated when button being pressed按下按钮时数据库列未更新
【发布时间】:2016-04-02 14:26:52
【问题描述】:

我的 php 文件中有 2 个函数。 1 从我的数据库中生成随机图像。 1 对图片进行投票(酷或不酷)。这是一个小项目,所以我的数据库中没有很多行。最多大约 10-15 个。

我的随机图像功能:

public function randomImage(){
$con = new Database();
$con->connect();
$sql = 'SELECT * FROM fotos order by RAND() LIMIT 1';
$sth = $con->myconn->query($sql);
$result = mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode($result['foto'] ).'"/>';
}

我的投票功能(酷或不酷):

public function voteImage($fotoid){
$con = new Database();
$con->connect();
$sql = 'UPDATE fotos SET cool = cool + 1 WHERE fotoid=?';
$stmt = $con->myconn->prepare($sql);
$stmt->bind_param('i', $fotoid);

    $stmt->execute();

    $stmt->close();  
}

我在另一个 php 文件中的代码:

<?php 
$image = new Image();
$image->randomImage();
?>
<form action="opdracht2.php" method="post" name="cool">
<input type="submit" value="Cool" name="cool" />
<input type="submit" value="Niet Cool" name="ncool" />
<input type="hidden" name="fotoid" value="<?php echo $result['fotoid']; ?>" />
</form>


<?php 


if(isset($_POST['cool']))
{
 $voteimage = new Image();
 $voteimage->voteImage($_POST['fotoid']);
}

我不确定我做错了什么。没有错误消息。当按下按钮很酷时,页面刷新但没有任何反应。它只是加载页面,但在数据库中,酷列不会更新 1。当我测试数据库中的查询时,它会更新。所以我认为我在 PHP 代码中做错了什么。

【问题讨论】:

  • $result['fotoid']; 设置在哪里?
  • 你是什么意思@Mihai
  • 我认为value="&lt;?php echo $result['fotoid']; 在您的 HTML 中将为空。尝试将其硬编码以用于调试目的
  • @Mihai 这不是`$result = mysqli_fetch_array($sth); `为此?
  • 超出函数范围

标签: php mysqli pdo


【解决方案1】:

请查找更新后的代码

public function randomImage(){
$con = new Database();
$con->connect();
$sql = 'SELECT * FROM fotos order by RAND() LIMIT 1';
$sth = $con->myconn->query($sql);
$result = mysqli_fetch_array($sth);
return $result;
}

您在另一个 php 文件中的代码:

<?php 
$image = new Image();
$result= $image->randomImage();
echo '<img src="data:image/jpeg;base64,'.base64_encode($result['foto'] ).'"/>';

?>
<form action="opdracht2.php" method="post" name="cool">
<input type="submit" value="Cool" name="cool" />
<input type="submit" value="Niet Cool" name="ncool" />
<input type="hidden" name="fotoid" value="<?php echo $result['fotoid']; ?>" />
</form>


<?php 


if(isset($_POST['cool']))
{
 $voteimage = new Image();
 $voteimage->voteImage($_POST['fotoid']);
}

你的投票功能(酷或不酷):

public function voteImage($fotoid){
$con = new Database();
$con->connect();
$sql = 'UPDATE fotos SET cool = cool + 1 WHERE fotoid=?';
$stmt = $con->myconn->prepare($sql);
$stmt->bind_param('i', $fotoid);

    $stmt->execute();

    $stmt->close();  
}

除此之外,我认为一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多