目前,我已经达成了一个结论,即HTTP只适用于在线热连接,并且无法通过localhosts或127.0.0.1调用发送,因为每当我的PHP文件创建'http(send)'
文件时,无论如何我所在的目录无法调用更新数据库。
我的意思是
index.php调用带有函数onClick()
的vote.js
vote.js调用带有函数HTTPSend(ERROR)的vote.php
vote.php使用服务器XAMPP从mysql更新数据库
我的第一个选择是使用bootstrap和新数据库来尝试rails 我的第二个选择是聘请开发人员 我的第三个选择是有人会好好看看,我很乐意通过联系woofwarrior@yahoo.com发送视频演示
文件:
/索引:
<tr>
<td style="width: 10%">
<span class="serial" id="<?php echo $images[$i]['imageID']; ?>"><?php echo $images[$i]['amount']; ?></span>
<a href="javascript:create_window(<?php echo " '" . $image_name . "' " . ", " . $image_size[0] . ", " . $image_size[1] ?>)">
<img src="<?php echo $dir . "/" . $images[$i]['name']; ?>" alt=""/>
</a>
</td>
<td style="width: 40%;position: relative">
<h3 class="text-effect" style="display: inline-block">
<?php echo $images[$i]['desc']; ?>
</h3>
<span class="arrow">
<a href="javascript:vote(<?php echo "'" . $images[$i]['name'] . "'" ?>)"><i
class="fa fa-arrow-up"></i></a>
</span>
</td>
<td> </td>
</tr>
/js/Vote.js
//ajax call to send upvote
function vote(name){
var httpRequest;
httpRequest = new XMLHttpRequest();
//console.log('')
if (!httpRequest) {
console.log('Cannot create an XMLHTTP instance');
return false;
}else{
httpRequest.onreadystatechange = alertContents;
httpRequest.open('POST', 'vote.php', true);
var data = "name="+encodeURIComponent(name);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//console.log(data);
httpRequest.send(data);
}
function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
console.log(httpRequest.responseText);
var data = JSON.parse(httpRequest.responseText);
if(data['imageID'] && data['new_amount']){
document.getElementById(data['imageID']).innerHTML = data['new_amount'];
}
} else {
console.log(httpRequest.status+ 'There was a problem with the request.');
}
}
}
}
/Vote.PHP
<?php
session_start();
include('mysql.php');
//check if user logged in and not anonymous voting
if(isset($_SESSION['userID']) && !isset($_POST['action']) && !isset($_POST['votePic'])){
//get image id
$result = mysqli_query($link, "SELECT `imageID` FROM `image` WHERE `name`='".$_POST['name']."';") or die(mysqli_error($link));
$image_id = mysqli_fetch_assoc($result);
//check if user already voted for certain image
$result = mysqli_query($link, "SELECT * FROM `votes` WHERE `userID`=".$_SESSION['userID']." AND `imageID`=".$image_id['imageID'].";") or die(mysqli_error($link));
$row = mysqli_num_rows($result);
if($row == '0'){
mysqli_query($link, "INSERT INTO `votes`(`userID`, `imageID`) VALUES (".$_SESSION['userID'].", ".$image_id['imageID'].");") or die(mysqli_error($link));
$data = update_vote($image_id['imageID']);
echo json_encode($data);
}else{
//already upvoted
echo json_encode('upvoted before');
}
}elseif(isset($_POST['votePic']) && !empty($_POST['votePic'])){
//anonymous vote from main page
$data = update_vote($_POST['votePic']);
echo json_encode($data);
}else{
//user not logged, cant vote
$data = update_vote($_POST['votePic']);
echo json_encode('not logged');
}
function update_vote($image_id){
//get number of votes and update
global $link;
$data = array();
$stmt = mysqli_prepare($link, "SELECT `amount` FROM `votes_amount` WHERE `imageID`=?;");
mysqli_stmt_bind_param($stmt, 'i', $image_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $num);
while (mysqli_stmt_fetch($stmt)) {
$amount['amount'] = $num;
}
mysqli_stmt_close($stmt);
$new_amount = $amount['amount']+1;
$stmt = mysqli_prepare($link, "UPDATE `votes_amount` SET `amount`=".$new_amount." WHERE `imageID`=?;") or die(mysqli_error($link));
mysqli_stmt_bind_param($stmt, 'i', $image_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
//return ajax data
if(isset($_SESSION['userID']) && !isset($_POST['action']) && !isset($_POST['votePic'])){
//insert scores
mysqli_select_db($link, "woofwarr_users");
$result = mysqli_query($link, "SELECT * FROM `scores` WHERE `userID`=".$_SESSION['userID']." ;") or die(mysqli_error($link));
$row = mysqli_num_rows($result);
if($row == '0'){
mysqli_query($link, "INSERT INTO `scores`(`userID`, `scoreAmount`) VALUES (".$_SESSION['userID'].", '1');") or die(mysqli_error($link));
}else{
$result = mysqli_query($link, "SELECT * FROM `scores` WHERE `userID`=".$_SESSION['userID']." ;") or die(mysqli_error($link));
$row = mysqli_fetch_assoc($result);
$new_score = $row['scoreAmount'] +1;
mysqli_query($link, "UPDATE `scores` SET `scoreAmount`=".$new_score." WHERE `userID` = ".$_SESSION['userID'].";") or die(mysqli_error($link));
}
mysqli_select_db($link, 'woofwarr_gallery');
$data = array('new_amount'=>$new_amount, 'imageID'=>$image_id);
}elseif(isset($_POST['action']) && $_POST['action'] == 'anonymous_voting'){
//get another two images
$result = mysqli_query($link, "SELECT * FROM `image` ORDER BY RAND() LIMIT 2;") or die(mysqli_error($link));
//$data = array();
while($row = mysqli_fetch_assoc($result)){
$data[]=$row;
}
}
mysqli_close($link);
return $data;
}
?>