2012-08-12 131 views
2

这是一种很复杂的解释,我不知道你们是否能够帮助我,但我会尽我所能。下面是这个过程中我希望用户去通过:::在IMGAJAX PHP连接插入数据库行

  1. 用户点击(ID =“牛仔”或“巨人”)
  2. 的onclick触发功能“teamback”,有“本”传递到它 (到目前为止好,这里是我需要使用ajax连接到数据库并插入信息)
  3. 函数需要采用userid(会话变量),tn和sc,并使用ajax插入它们
    进入数据库

ajax部分代码是我不知道该怎么做的地方,我从来没有使用ajax。我很确定我搞砸了GET部分。问题是它不起作用。它不会在桌子上插入任何东西。因为我的ajax代码在teamback函数和makepick.php文件中混乱了(我认为)。任何帮助设置这将不胜感激!

下面是HTML文件...

<?php 
// this starts the session 
session_start(); 
$id = $_SESSION['userid']; 

//this connects to the database 
$con = mysql_connect("localhost","yourfan3_jeengle","armyjoe30"); 
mysql_select_db("yourfan3_demo", $con); 

//**THIS IS THE VARIABLE THAT MANUALLY SETS THE PICKS POSSIBLE 
$maxcorrectpicks = 16; 

//gets info for user 
$result = mysql_query("SELECT * FROM League_Info WHERE User_ID = '$id'"); 
$result2 = mysql_fetch_array($result); 
$leaguename = $result2['League']; 

//checks if league name exists 
$memberslist = mysql_query("SELECT User_ID, Correct_Picks, Points FROM League_Info WHERE League = '$leaguename'"); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <script type="text/javascript" src="js/jquery-1.2.6.min.js"></script> 
     <script type="text/javascript" src="js/jquery-easing-1.3.pack.js"></script> 
     <script type="text/javascript" src="js/jquery-easing-compatibility.1.2.pack.js"></script> 
     <script type="text/javascript" src="js/coda-slider.1.1.1.pack.js"></script> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <link rel="icon" href="http://www.indiana.edu/favicon.ico" /> 
     <title>Your</title> 
     <link rel="stylesheet" type="text/css" href="yourteamstyle.css" /> 

     <script type="text/javascript"> 
     //this is the user id session stored as a javascript variable 
     var userid = "<?=$id?>"; 

     // Popup window code 
     function newPopup(url) { 
      popupWindow = window.open(url,'popUpWindow','height=450,width=600,left=10,top=10,resizable=no,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes') 
     } 
     function bigimg(x) { 
      var myDate = new Date(); // Your timezone! 
      var myEpoch = myDate.getTime()/1000; 
      var deadline = '1344700055.000'; 
       //determines if user was on time..if not on time hover enlarge won't work 
       if(myEpoch < deadline) { 
        x.style.height="65px"; 
        x.style.width="85px"; 
        x.style.opacity="0.5"; 
       } else {} 
     } 
     function defaultimg(x) { 
      x.style.height="60px"; 
      x.style.width="80px"; 
      x.style.opacity="1.0"; 
     } 
     function teamback(x) { 
      var myDate = new Date(); // Your timezone! 
      var myEpoch = myDate.getTime()/1000; 
      var deadline = '1344700055.000'; 
       //determines if user was on time..if not on time submitting won't work 
       if(myEpoch > deadline) { 
        // update the "actualone" image's source to the sending-image's source 
        var tn = x.id; 
        var sc = x.name; 
        document.getElementById("actualone").src = x.src; 
        document.getElementById("curtime").innerHTML = myEpoch; 
        document.getElementById("team").innerHTML = x.id; 
        document.getElementById("scenario").innerHTML = x.name; 

        //this is the ajax part where I am having trouble 
        if (window.XMLHttpRequest) { 
         // code for IE7+, Firefox, Chrome, Opera, Safari 
         xmlhttp=new XMLHttpRequest(); 
        } else { 
         // code for IE6, IE5 
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        xmlhttp.onreadystatechange=function() { 
        } 
        xmlhttp.open("GET","makepick.php?newuserid="+userid, true); 
        xmlhttp.open("GET","makepick.php?newtn="+tn, true); 
        xmlhttp.open("GET","makepick.php?newsc="+sc, true); 
        xmlhttp.send(); 

       } else {}   
     } 
     </script> 

    </head> 
     <body> 
      Your Team<br> 
      <iframe style="background-color:red;" src="http://free.timeanddate.com/countdown/i38ik9yz/n417/cf12/cm0/cu4/ct1/cs1/ca0/co1/cr0/ss0/cac000/cpc000/pct/tc66c/fs100/szw320/szh135/tatTime%20Remaining%20to%20Make%20Picks/tac000/tptTime%20since%20Event%20started%20in/tpc000/iso2012-08-11T13:00:00" frameborder="0" width="236" height="36"></iframe> 

      <br><img id="cowboys" name="One" onmouseover="bigimg(this)" onclick="teamback(this)" onmouseout="defaultimg(this)" src="cowboys.gif"> vs <img id="giants" name="One" onmouseover="bigimg(this)" onclick="teamback(this)" onmouseout="defaultimg(this)" src="giants.gif"><img src="" id="actualone" style="width:85px; height:65px;"><br><br> 
      <div id="curtime">44</div>|||<div id="deadline"></div><br><div id="team">Team</div><div id="scenario">Scenario</div> 


     </body> 
</html> 

这里是makepick.php文件

<?php 
$userid = $_GET["newuserid"]; 
$tn = $_GET["newtn"]; 
$sc = $_GET["newsc"]; 

//this connects to the database 
$con = mysql_connect("localhost","yourfan3_jeengle","armyjoe30"); 
mysql_select_db("yourfan3_demo", $con); 

mysql_query("INSERT INTO Week1_Picks_Test (UserID, '$sc') VALUES ('$userid', '$tn')"); 

?> 
+0

什么问题呢? – 2012-08-12 15:58:14

+0

问题是它不起作用。它不会在桌子上插入任何东西。因为我的ajax代码在teamback函数和makepick.php文件(我认为)中搞砸了。 – 2012-08-12 15:59:35

+0

这不是全部,但是你需要改变'var userid =“”;''var userid =“<? php echo $ id;?>“; – PitaJ 2012-08-12 16:09:47

回答

1

东西,我注意到了蝙蝠的权利: 查询您在AJAX处理中执行单引号(')中的$ sc变量。由于这是一个列名,它会产生一个语法错误。要么删除引号,要么使用反引号(`)。

这就是说,你应该考虑把它传递给数据库,或者甚至更好前清理输入,使用预处理语句与PDO:http://php.net/manual/en/book.pdo.php

2

因为,你是使用jQuery我建议你重写你的teamback()函数的使用jQuery的AJAX方法:

function teamback(x) { 
var myDate = new Date(); // Your timezone! 
var myEpoch = myDate.getTime()/1000; 
var deadline = '1344700055.000'; 
    //determines if user was on time..if not on time submitting won't work 
    if(myEpoch > deadline) { 
     // update the "actualone" image's source to the sending-image's source 
     var tn = x.id; 
     var sc = x.name; 
     document.getElementById("actualone").src = x.src; 
     document.getElementById("curtime").innerHTML = myEpoch; 
     document.getElementById("team").innerHTML = x.id; 
     document.getElementById("scenario").innerHTML = x.name; 

     $.ajax({ 
      type: 'GET', 
      url: 'makepick.php', 
      data: { newuserid: userid, newtn: tn, newsc:sc }, 

      success:function(data){ 
      //do whatever you want to do here on successful submission 
      alert('success'); 
      }, 
      error:function(){ 
      //do whatever you want to do here when an error occurs 
      alert('error'); 
      } 
     }); 
    } else {}   

}

+1

使用JQuery,你也可以使用选择器并将这个函数改为'$(img#cowboys,img#giants).click(function(){...});'' – 2012-08-12 16:17:15