2014-12-18 36 views
-4

我剛剛做了一個代碼,它給了我一個從1到57的隨機數,並根據給定的數字,我獲得了一個與該數字相關的值,並從該數組中刪除該數字。從數據庫中刪除隨機條目

下面是每個數字範圍的解釋:

http://i.stack.imgur.com/O1Lby.png

這裏是代碼:

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <title>Test</title> 
 
    <meta charset="UTF-8"> 
 
    
 
</head> 
 
<body> 
 
     <p id="coche"></p> 
 
     <input type="button" value="Click aqui!" onclick="coches()"> 
 
</body> 
 
<script> 
 
var idx = 0; 
 
var randomNums = []; 
 

 
for (var i = 0; i < 57; i++) { 
 
    randomNums.push(i); 
 
} 
 

 
shuffle(randomNums); 
 

 
function shuffle(array) { 
 
    var currentIndex = array.length, 
 
    temporaryValue, randomIndex; 
 

 
    // Mientras queden elementos que barajar... 
 
    while (0 !== currentIndex) { 
 

 
    // Elige un elemento restante... 
 
    randomIndex = Math.floor(Math.random() * currentIndex); 
 
    currentIndex -= 1; 
 

 
    // Y lo intercambias con un elemento actual. 
 
    temporaryValue = array[currentIndex]; 
 
    array[currentIndex] = array[randomIndex]; 
 
    array[randomIndex] = temporaryValue; 
 
    } 
 

 
    return array; 
 
} 
 

 
function coches() { 
 
    if (randomNums[idx] <= 3) { 
 
    document.getElementById("coche").innerHTML = "Audi " + randomNums[idx]; 
 
    } else if (randomNums[idx] <= 9) { 
 
    document.getElementById("coche").innerHTML = "BMW " + randomNums[idx]; 
 
    } else if (randomNums[idx] <= 17) { 
 
    document.getElementById("coche").innerHTML = "Mercedes " + randomNums[idx]; 
 
    } else { 
 
    document.getElementById("coche").innerHTML = "Seat " + randomNums[idx]; 
 
    } 
 

 
    idx++; 
 
} 
 
</script> 
 
</html>

的事情是,現在我需要它讀取數據庫並從數據庫中而不是數組中刪除它。以下是數據庫的示例轉儲。

CREATE DATABASE IF NOT EXISTS `playmo-demo` /*!40100 DEFAULT CHARACTER SET utf8 */; 
 
USE `playmo-demo`; 
 
-- MySQL dump 10.13 Distrib 5.6.19, for osx10.7 (i386) 
 
-- 
 
-- Host: localhost Database: playmo-demo 
 
-- ------------------------------------------------------ 
 
-- Server version \t 5.1.73 
 

 
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
 
/*!40101 SET NAMES utf8 */; 
 
/*!40103 SET @[email protected]@TIME_ZONE */; 
 
/*!40103 SET TIME_ZONE='+00:00' */; 
 
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 
 
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 
 
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 
 
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; 
 

 
-- 
 
-- Table structure for table `premios` 
 
-- 
 

 
DROP TABLE IF EXISTS `premios`; 
 
/*!40101 SET @saved_cs_client  = @@character_set_client */; 
 
/*!40101 SET character_set_client = utf8 */; 
 
CREATE TABLE `premios` (
 
    `id_premios` int(255) NOT NULL, 
 
    `id_paquete` varchar(255) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL, 
 
    `nombre_premio` varchar(255) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL, 
 
    `cantidad` varchar(255) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL, 
 
    `acumulado` varchar(45) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL, 
 
    `rank` varchar(45) CHARACTER SET utf8 COLLATE utf8_spanish_ci DEFAULT NULL, 
 
    PRIMARY KEY (`id_premios`), 
 
    KEY `id_paquete` (`id_paquete`), 
 
    KEY `nombre_premio` (`nombre_premio`), 
 
    CONSTRAINT `premios_ibfk_1` FOREIGN KEY (`nombre_premio`) REFERENCES `banco_imagenes` (`nombre_premio`) 
 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
 
/*!40101 SET character_set_client = @saved_cs_client */; 
 

 
-- 
 
-- Dumping data for table `premios` 
 
-- 
 

 
LOCK TABLES `premios` WRITE; 
 
/*!40000 ALTER TABLE `premios` DISABLE KEYS */; 
 
INSERT INTO `premios` VALUES (1,'1','Copa','8','1','4'),(2,'1','No Premio','3','4','1'),(3,'1','Botella','5','9','2'),(4,'1','Plato','7','16','3'); 
 
/*!40000 ALTER TABLE `premios` ENABLE KEYS */; 
 
UNLOCK TABLES; 
 
/*!40103 SET [email protected]_TIME_ZONE */; 
 

 
/*!40101 SET [email protected]_SQL_MODE */; 
 
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; 
 
/*!40014 SET [email protected]_UNIQUE_CHECKS */; 
 
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 
 
/*!40111 SET [email protected]_SQL_NOTES */; 
 

 
-- Dump completed on 2014-12-18 18:03:29

知道了:

id_premios =是從1到57

nombre_premio =該車名稱中的數字,例如奧迪

+2

您當前的代碼與數據庫無關,嘗試連接到一個並執行預期的行爲。 – Jonast92

回答

0

您需要使用服務器端語言(如PHP)和準備好的MySQLi語句。

<?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 
$dbname = "myDB"; 

// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

    /* Create a prepared statement */ 
if($stmt = $mysqli -> prepare("DELETE from `premios` where id_premios = ?")) { 

/* Bind parameters 
s - string, b - boolean, i - int, etc */ 
$stmt -> bind_param("i", $randomInteger); 

if (mysqli_query($conn, $sql)) { 
    echo "Record deleted successfully"; 
} else { 
    echo "Error deleting record: " . mysqli_error($conn); 
} 

mysqli_close($conn); 
?> 

我沒有測試過這個,但它應該把你放在正確的軌道上。

+0

那麼,如何在每次按下按鈕時執行此代碼?用JavaScript? – Eokizo

+1

您使用AJAX請參閱此處:http://stackoverflow.com/questions/7165395/call-php-function-from-javascript 您需要了解JavaScript,PHP並對AJAX有基本的瞭解才能做到這個。 JavaScript是客戶端(意味着它在用戶的機器上運行),PHP和數據庫是服務器端(意味着它們在服務器上運行)。 JavaScript無法與服務器端交互。 –