English 中文(简体)
PHP/ AJAX: 点击图像时更改数据库中的值( Mysql)
原标题:PHP/AJAX: Change a value in database(mysql) when image clicked

我目前正在研究一个即将完成的项目, 但还有一个问题需要解决, 这个问题非常关键。

我试图做的是点击某个图像时更改数据库中的值( 激活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";
?>           
最佳回答

here is Artikel.php.LATEST POPST 12:40 PST

<?
mysql_connect("xxxxxxxxxxx", "xxxxxxxxxx", "xxxxxxxxx");  
mysql_select_db("xxxxxxxx");  

$res = mysql_query("select * from artikel");  
$fcount = mysql_fetch_assoc(mysql_query("select count(*) as count from artikel"));  

while ($Binnenland =  mysql_fetch_assoc($res)) { 
    $jS =  " .$Binnenland[ArtikelID]. " ; 
    echo "<font size=2 face= Century Gothic ><center><li><b>ARTICLE ID: ".$Binnenland[ ArtikelID ]."</b><br><font size=2><a href= # ><h3>".$Binnenland[ Titel ]."</h3><a href= javascript:setActive(".$jS.") > <img src= $Binnenland[thumbpath]  width=75 style= border: 0px; -moz-border-radius: 8px; border-radius: 8px  alt= thumbnail ></a><p>".$Binnenland[ shortcontent ]. "</p></center>"; 
    echo "<span style= color: black;  id= ".$Binnenland[ArtikelID]." ><center><font face= Century Gothic >ActiveID: ".$Binnenland[activeID]."</font></center><br><br><hr></span>";
} 

?> 

<script type="text/javascript">  

function setActive(ObjID)  {  

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; 

        if (document.getElementById(ObjID).style.color ==  black ) document.getElementById(ObjID).style.color= red ;
                else document.getElementById(ObjID).style.color= black ;

        document.getElementById(ObjID).innerHTML =  <center><font face="Century Gothic">ActiveID:   + x +  </font></center><br><br><hr> ; 
      }  
}  
    var p_str = "a="+ObjID; 
    xmlhttp.open("POST","setActive.php",true);  
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");  
    xmlhttp.send(p_str);  
}  

</script> 

< 强 > 和此处设定了感应.php

<?php              

mysql_connect("xxxxx", "xxxxx", "xxxxxx");  
mysql_select_db("xxxxx"); 
$ArtikelID = $_REQUEST[a]; 

$res = mysql_query("select activeID from artikel where ArtikelID = ".$ArtikelID);  
$row = mysql_fetch_assoc($res); 

$active = $row[activeID];  
$activeID = ($active == 0) ? 1 : 0;   
$res = mysql_query("update artikel set activeID = $activeID  where ArtikelID = ".$ArtikelID);  

if ($res) echo $activeID;  
       else echo "Failure, unable to update ActiveID to $activeID";  
?>
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.