从数据库中删除随机条目

时间:2014-12-18 17:41:40

标签: javascript mysql sql

我刚刚编写了一个代码,它给出了一个从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>
&#13;
&#13;
&#13;

问题是,现在我需要它来读取数据库并从数据库而不是数组中删除它。以下是数据库的示例转储。

&#13;
&#13;
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	5.1.73

/*!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 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@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 TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!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 */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2014-12-18 18:03:29
&#13;
&#13;
&#13;

知道:

id_premios =数字是1到57

nombre_premio =汽车名称,例如Audi

1 个答案:

答案 0 :(得分: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);
?>

我还没有对此进行测试,但它应该让你走上正轨。