douchi1945 2018-06-05 22:25
浏览 45

将变量传递给mysqli预处理语句

I have been self teaching myself php in my spare time using you tube and w3 schools. So far I have been mostly successful but have hit a brick wall with a particular issue.

I am trying to use a prepared statement to load data into a database (Xampp myphp admin). I have attached my code below and have done a range of testing based on internet searches. When I run the code I get no error message but nothing inserts into my database. I am fairly certain it is do with passing the variables into the bind_param() placeholders.

Please ignore the fact the headings don't match the data types as I want to get data inserting into the database first.

Thanks

<?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();
    }

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了