尝试制作一个简单的点击计数器,不起作用。我的垃圾代码怎么了?
addLike.php
<?php
$requestLikes = "SELECT Likes FROM test1";
$Likes = mysql_query($requestLikes);
$insertToLikes = "INSERT INTO test1 (Likes) VALUES (" . $Likes + 1 . ")";
mysql_query($insertToLikes);
$Likes = mysql_query($requestLikes);
?>
HTML
<button action="/DEMO/PHP/addLike.php" method="post">Yeet
</button>0
<?php
echo '<h1>' . $Likes . '</h1>';
?>
www.mySite / DEMO / PHP / addLike.php
Fatal error: Uncaught Error: Call to undefined function mysql_query() in /storage/ssd4/786/4680786/public_html/DEMO/PHP/addLike.php:4 Stack trace: #0 {main} thrown in /storage/ssd4/786/4680786/public_html/DEMO/PHP/addLike.php on line 4
是的,我已经建立了与数据库的连接 编辑:不要介意按钮标记
后的随机“0”答案 0 :(得分:3)
<?php
$conn = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
$requestLikes = "SELECT Likes FROM test1";
$LikesQ = mysqli_query($conn, $requestLikes);
$Likes = mysqli_fetch_array($LikesQ);
$x = $Likes[0] + 1;
$insertToLikes = "INSERT INTO test1 (Likes) VALUES ('$x')";
mysqli_query($conn, $insertToLikes);
$y = mysqli_query($conn, $requestLikes);
$output = mysqli_fetch_array($y);
echo "<h1>$output[0]</h1>";
?>
请使用MYSQLI
$insertToLikes = "INSERT INTO test1 (Likes) VALUES (" . $Likes + 1 . ")";
您很容易受到MySQL注入攻击,总是使用预备语句或mysqli_real_escape_string