我目前正在研究一个即将完成的项目, 但还有一个问题需要解决, 这个问题非常关键。
我试图做的是点击某个图像时更改数据库中的值( 激活ID) 。 强 > 。 强 >
我知道我会需要 php 和javascript, 但我的javascript技能不是真的好, 如果有人可以帮助我, 那将是惊人的!
为了帮助你们理解, 数据库中 < 坚固> 结构 : < / 坚固 >
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
CREATE TABLE IF NOT EXISTS `artikel` (
`Titel` varchar(100) CHARACTER SET utf8 NOT NULL,
`ArtikelID` int(255) NOT NULL,
`content` text CHARACTER SET utf8 NOT NULL,
`imagepath` varchar(255) CHARACTER SET utf8 NOT NULL,
`activeID` int(11) NOT NULL,
`thumbpath` varchar(255) CHARACTER SET utf8 NOT NULL,
`shortcontent` text CHARACTER SET utf8 NOT NULL,
`categorie` varchar(10) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`ArtikelID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `artikel` (`Titel`, `ArtikelID`, `content`, `imagepath`, `activeID`, `thumbpath`, `shortcontent`, `categorie`) VALUES
( Zware hinder op E313 door dodelijk ongeval in Geel , 1, Het ongeval gebeurde rond 12 uur. Er had zich een file gevormd doordat de afrit Geel-West is afgesloten wegens werken. , images/bin1.jpg , 0, images/bin1thumb.jpg , Op de E313 richting Antwerpen is vandaag ter hoogte van de afrit Geel-West. , bin ),
( België geeft 12 miljoen voor heropbouw Afghanistan na 2014 , 2, Eind 2014 trekt de NAVO zich terug uit Afghanistan. De militairen dragen dan de verantwoordelijkheid voor de veiligheid definitief over aan de Afghaanse veiligheidstroepen. , images/bin2.jpg , 0, images/bin2thumb.jpg , Na de terugtrekking van de Belgische militairen uit Afghanistan eind 2014 zal België voor maximaal 12 miljoen euro bijdragen aan de heropbouw van het land. , bin ),
( Oppositie hekelt gebrek aan visie en groei in debat begrotingscontrole , 3, De controle was nodig omdat de regering bij de initiële begroting 2012 uitgegaan was van een economische groei van 0,8% van het bbp, terwijl dat cijfer in de economische begroting van februari teruggebracht werd 0,1%. , images/bin3.jpg , 0, images/bin3thumb.jpg , De plenaire Kamer buigt zich vandaag over de begrotingscontrole die het begrotingstekort voor 2012 moet beperken tot 2,8 procent van het bbp, overeenkomstig het stabiliteitsprogramma. Daarvoor was een inspanning van 1,8 miljard euro nodig. , bin )
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
这是数据库使用的 < 坚固 > 连接 I : 坚固 >
<?php
session_start();
$sHost = "localhost";
$sUser = "root";
$sPassword = "";
$sDatabase = "project";
$link = mysqli_connect($sHost, $sUser, $sPassword, $sDatabase) or die("MySQL Error: " . mysqli_error());
?>
这是 php 和 html 代码, 我必须要将内容从数据库中调出, 请注意每个记录 < / strong > 都有一个图像通过路径被调用( 只有您看到的第一个选定语句与此主题相关, 因为我剪掉了 HTML 代码的其余部分, 以缩短主题 ) :
<?php
//session_start();
//include("classes/connection.php");
include_once("classes/connection.php");
$getBinnenland = mysqli_query($link,"SELECT * FROM artikel WHERE categorie = bin ;");
//$getBuitenland = mysqli_query($link,"SELECT * FROM artikel WHERE categorie = bui ;");
//$getSport = mysqli_query($link,"SELECT * FROM artikel WHERE categorie = spo ;");
//$getPolitiek = mysqli_query($link,"SELECT * FROM artikel WHERE categorie = pol ;");
//$getMilieu= mysqli_query($link,"SELECT * FROM artikel WHERE categorie = mil ;");
//$getBizar = mysqli_query($link,"SELECT * FROM artikel WHERE categorie = biz ;");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Digital Press overview</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="wrapper">
<ul id="Binnenland">
<?php
if(mysqli_num_rows($getBinnenland) > 0)
{
while($Binnenland = mysqli_fetch_assoc($getBinnenland))
{
echo "<li><a href= # ><h3>".$Binnenland[ Titel ]."</h3><img src= ". $Binnenland[ thumbpath ] ." alt= thumbnail /></a><p>".$Binnenland[ shortcontent ]. "</p>";
}
}
else
{
echo "<li>Nog geen designs</li>";
}
?>
</ul>
</div>
</body>
<坚固> 现在回答问题: 如果我单击某个图像, 我希望它将该记录的活跃代号修改为 1 而不是 0, 如果我再次单击它, 我希望它重新修改为 0 强 >
I don t have a clue on how to start this, so any help would be most welcome, Thank you in advance to all who will help me with this! Greetings
EDIT
我从下面的答案中应用了代码, 没有错误, 但数据库也不会改变活动ID 。 有人知道我可以做些什么来解决这个问题吗?
点击事件 :
echo "<li><a href= # ><h3>".$Binnenland[ Titel ]."</h3><img onClick = javascript: setActive(); src= ". $Binnenland[ thumbpath ] ." alt= thumbnail /></a><p>".$Binnenland[ shortcontent ]. "</p>";
笔记本:
<script type="text/javascript">
function setActive()
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Return Value. Handle as you wish. Display or ignore.
var x = xmlhttp.responseText;
}
}
xmlhttp.open("POST","setActive.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(p_str);
}
</script>
设定 Action.php 文件 :
<?php
session_start();
$sHost = "localhost";
$sUser = "root";
$sPassword = "";
$sDatabase = "project";
$link = mysqli_connect($sHost, $sUser, $sPassword, $sDatabase) or die("MySQL Error: " . mysqli_error());
$res = mysqli_fetch_row(mysqli_query($link,"select activeID from artikel"));
$active = $res[activeID];
$activeID = ($active == 0) ? 1 : 0;
$res = mysqli_query($link,"update artikel set activeID = $activeID");
if ($res) echo "Success, ActiveID = $activeID";
else echo "Failure, unable to update ActiveID to $activeID";
?>