Передача переменных в подготовленный MySQL - PullRequest
0 голосов
/ 06 июня 2018

Я занимался самообучением в свободное время, используя твою школу и школу w3.До сих пор я был в основном успешным, но столкнулся с кирпичной стеной с определенной проблемой.

Я пытаюсь использовать подготовленный оператор для загрузки данных в базу данных (администратор Xampp myphp).Я приложил свой код ниже и провел ряд тестов на основе поисковых запросов в Интернете.Когда я запускаю код, я не получаю сообщение об ошибке, но ничего не вставляется в мою базу данных.Я вполне уверен, что это связано с передачей переменных в заполнители bind_param ().

Пожалуйста, игнорируйте тот факт, что заголовки не соответствуют типам данных, так как я хочу сначала вставить данные в базу данных.

Спасибо

<?php

include 'dbh.php';

class AddData extends Dbh {

public function submitTableData(){

$dateErr = $starttimeErr = $finishtimeErr = $durationErr = $taskErr = $entityErr = $completeErr = $commentsErr = "";
$date = $starttime = $finishtime = $duration = $task = $entity = $complete = $comments = "";

$query = "INSERT INTO testtable(Date, Starttime, Finishtime, Duration, Task, Entity, Complete, Comments) VALUES (?,?,?,?,?,?,?,?)";

$stmt= $this->connect()->prepare($query);

$stmt->bind_param("ssssssss", $date, $starttime, $finishtime, $duration, $task, $entity, $complete, $comments);

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return  $data;
}

for ($x = 0; $x < 1; $x++) {

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["date"][$x])) {
    $dateErr = "date is required";
  } else {
    $date = test_input($_POST["date"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$date)) {
      $dateErr = "Only letters and white space allowed"; 
    }
  }

    if (empty($_POST["starttime"][$x])) {
    $starttimeErr = "starttime  is required";
  } else {
    $starttime  = test_input($_POST["starttime"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$starttime )) {
      $starttimeErr = "Only letters and white space allowed"; 
    }
  }

      if (empty($_POST["finishtime"][$x])) {
    $finsihtimeErr = "finishtime is required";
  } else {
    $finishtime = test_input($_POST["finishtime"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$finishtime)) {
      $finishtimeErr = "Only letters and white space allowed"; 
    }
  }

      if (empty($_POST["duration"][$x])) {
    $durationErr = "Name is required";
  } else {
    $duration = test_input($_POST["duration"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$duration)) {
      $durationErr = "Only letters and white space allowed"; 
    }
  }

      if (empty($_POST["task"][$x])) {
    $taskErr = "task is required";
  } else {
    $task = test_input($_POST["task"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$task)) {
      $taskErr = "Only letters and white space allowed"; 
    }
  }

      if (empty($_POST["entity"][$x])) {
    $entityErr = "Name is required";
  } else {
    $entity = test_input($_POST["entity"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$entity)) {
      $entityErr = "Only letters and white space allowed"; 
    }
  }

      if (empty($_POST["complete"][$x])) {
    $completeErr = "complete is required";
  } else {
    $complete = test_input($_POST["complete"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$complete)) {
      $completeErr = "Only letters and white space allowed"; 
    }
  }

      if (empty($_POST["comments"][$x])) {
    $commentsErr = "comments is required";
  } else {
    $comments = test_input($_POST["comments"][$x]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$comments)) {
      $commentsErr = "Only letters and white space allowed"; 
    }
  }
  }
   $stmt->execute();
}
 $stmt->close();
 $this->connect()->close();
}
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $t1 = new AddData;
    $t1->submitTableData();
    }

?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...