-1
こんにちは私は同様のボタンをクリックするとPHPとMySQLを使って同様のシステムを作っていますButtonデータベースにもデータを挿入しますが、増分および未定義のエラーは発生しません。誰もこの問題を解決するのを手助けできますか?好きなボタンをクリックするとエラーメッセージが表示されます
There is my Like button code :
<?php
//// work with like box
$get_likes = mysqli_query($con,"SELECT * FROM `likes`");
if (mysqli_num_rows($get_likes)===1) {
$get = mysqli_fetch_assoc($get_likes);
// $uid = $get['uid'];
$total_likes = $get['total_likes'];
//echo $uid;
$total_likes = $total_likes + 1;
//echo $total_likes++;
}
if (isset($_POST['likebutton_'])) {
$like = mysqli_query($con,"UPDATE `likes` SET `total_likes` = '$total_likes'") or die(mysqli_error($con));
//$insert_Data = mysqli_query($con,"INSERT INTO `likes` (`uid`) VALUES('$username')") or die(mysqli_error($ocn));
header("Location:home.php");
}
else
{
echo "Error";
}
?>
this code work fine without insert Data
There is My liked with Data Insertd Code
<?php
////work with like box
$get_likes = mysqli_query($con,"SELECT * FROM `likes`");
if (mysqli_num_rows($get_likes)===1) {
$get = mysqli_fetch_assoc($get_likes);
// $uid = $get['uid'];
$total_likes = $get['total_likes'];
//echo $uid;
$total_likes = $total_likes + 1;
//echo $total_likes++;
}
if (isset($_POST['likebutton_'])) {
$like = mysqli_query($con,"UPDATE `likes` SET `total_likes` = '$total_likes'") or die(mysqli_error($con));
$insert_Data = mysqli_query($con,"INSERT INTO `likes` (`uid`) VALUES('$username')") or die(mysqli_error($ocn));
header("Location:home.php");
}
else
{
echo "Error";
}
?>
this is output i want to display my font-end page <?php echo $total_likes ;?> but it occur error
The error is Undefined Variable
I also try $total_likes="";
as global but still not work
結果を表示したいフォーム、データベース、フロントエンドページのコードを追加できますか? – Richard
あなたは 'likes'を取得する必要はありません。mysqlは現在の行+ 1を更新します。あなたはこのコードでSQLインジェクションを開いています。また、すべてのレコードを更新しないように、IDを渡すべきではありませんか? – chris85
あなたが私の推測に遭遇している問題は、 'mysqli_num_rows($ get_likes)'が '1'と等しくないことです。カウントが1の場合のみ '$ total_likes'を割り当てます。 – chris85