php mysqli插入无法正常工作

时间:2017-05-21 03:18:02

标签: php mysql sql insertion

我正在尝试将数据插入mysql但不能正常工作, 没有错误显示在下面是PHP脚本

  int[] data = {0,1,2,4,8,16};
  int[] samples = new int[5];

  Random random = new Random();
  int value;
  int round = 3; // take in the number of rounds.
  int[] wins = {0,0,0,0,0}; // we'll use this to store the player # of wins.
  // Run the game for the specified number of rounds.
  for(int i = 0; i < round; i++){
    // Get 5 random numbers for each player.
    for(int j=0;j<5;j++){
        value = random.nextInt(6);
        samples[j] = data[value];
    }
    // Set the current winner to junk values.
    int max = Integer.MIN_VALUE;
    int winner = 0;
    // Run though the samples for the current round.
    for(int j = 0; j < samples.length; j++){
      // Print test of each players number.
      System.out.println("Player" + (j + 1) + " Score " + samples[j]);
      // Check in order which player won the round.
        if(samples[j] > max){
          max = samples[j];
          winner = j;
        }
    }
    // Increment the number of wins for the winner.
    wins[winner]++;
    System.out.println();
  }
  // Print test of round wins.
  for(int i = 0; i < wins.length; i++){
    System.out.println("Player" + (i+1) + " Wins " + wins[i]);
  }
}

1 个答案:

答案 0 :(得分:0)

error_reporting(E_ALL);//show all errors   

 $mysqli = new mysqli('localhost','root','','realestate');

     if($mysqli->connect_error){
     printf("can not connect database %s\n",$mysqli->connect_error);
     exit();
     }  

 if (isset($_POST['submit'])){
     //make sure $_POST is not null
     var_dump('Posted variables are '. $_POST.' and files are '. $_FILES);//Comment out after debugging
     $a_name=$_POST['a_name'];
     $a_number=$_POST['a_number'];
     $email=$_POST['email'];
     $password=$_POST['password']; 


     $target_dir="uploads/";
     $target_file=$target_dir . basename($_FILES["a_image"]["name"]);    
     $temp_file=$_FILES["a_image"]["name"];//make sure this is not null

     move_uploaded_file($_FILES["a_image"]["tmp_name"], $target_file ); 

    $query="INSERT INTO `agent` (`a_name`,`a_number`,`email`,`password`,`a_image`) 
     VALUES ('$a_name','$a_number','$email','$password','$temp_file')";

    if($mysqli->query($query) == true){//check for success
         echo 'Success';//display message of success
     } else {
        echo $mysqli->error;//display error message
     }

//The above code is prone to SQL injection. Below is using prepared statements:
// prepare and bind
$stmt = $mysqli->prepare("INSERT INTO `agent`(`a_name`,`a_number`,`email`,`password`,`a_image`) VALUES (?, ?, ?,?,?)");
$stmt->bind_param("sssss", $a_name, $a_number, $email, $password, $temp_file);

$stmt->execute();