查询不会插入但没有错误

时间:2019-03-11 09:40:14

标签: php mysql

我要用我的代码完成的工作是,它首先检查代码是否正确,然后必须插入查询。但是不知何故,它不会插入...而且我不知道我一眼看错了什么。

我的PHP:

include 'functions/conn.php';
$Giveaway = "SELECT `content` FROM `sub_codes` WHERE `current_uses` > '0'";
$result1 = mysqli_query($conn, $Giveaway);
$row = mysqli_fetch_array($result1);
    if ($_POST['Code']===$row) {
        $sql3 = "SELECT `user_id` FROM `users` ORDER BY `user_id` DESC LIMIT 1";
        $result3 = mysqli_query($conn, $sql3);
        $row3 = mysqli_fetch_array($result3);
        $_start_date = date('Y-m-d');
        $_end_date = $row2;
        $sql2 = "SELECT `end_date` FROM `sub_codes` WHERE `content` = '".$_POST['Code']."'";
        $result2 = mysqli_query($conn, $sql2);
        $row2 = mysqli_fetch_array($result2);
        $sql = "INSERT INTO `partner_subscriptions` SET `user_id` = '$row3' + 1, `sub_id` = '99', `allowed_users` = '100', `start_date` = '$_start_date', `end_date` = '$row2';";
        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
} else {
    echo "Wrong code";
}

还有我的html:

<br/><div class="form-group">
  <label title="Required">Free description code:</label>
    <input type="Code" name="Code" class="form-control" id="Code"
      required="true"/>
    </div><br/>

这是我到目前为止尝试过的。如果您需要更多代码,请问我。但是我认为这就足够了。我没有任何错误。

编辑

这是我插入页面的剩余代码

<?php

//Controleer of de e-mail nog niet gebruikt is.
if (usedmail($_POST['username'])==true) {
$lastID = saveUser($_POST['fnln'], $_POST['username'], 
password_hash($_POST['password'], PASSWORD_BCRYPT), 0, 0, 1);
$niv = NULL;
if ($_POST['type'] == "3") { // If the partner is an educational 
institution look for niveau
    $niv = NivID($_POST['niv']);
}

$path = saveImage();
Contact($lastID);
Image($lastID);
Social($lastID);
Story($lastID);
Skill($lastID);
$orgID = saveOrganisation($lastID, $_POST['organisation'], $path, $_POST['type'], $_POST['branche'], $niv);
updateUser($orgID, $lastID);
}
else {
header('Location: ../../mailerror');
}
function saveUser($fnln, $userName, $passWord, $orgID, $partID) {
require '../conn.php';
$sql = "INSERT INTO `users` (`user_id`, `fnln`, `username`, `password`, 
`org_id`, `part_id`, `type`, `active`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?);";
$stmt = $conn->prepare($sql);
$_fnln = $fnln;
$_username = $userName;
$_password = $passWord;
$_orgID = $orgID;
$_partID = $partID;
$_type = '1';
$_active = '1';
$stmt->bind_param("sssiiii", $_fnln, $_username, $_password, $_orgID, $_partID, $_type, $_active);
$stmt->execute();
$lastID = $conn->insert_id;
$stmt->close();
return $lastID;

}

function saveImage()
{
$image = $_FILES['frontImage']['tmp_name'][0];
$random = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYS", 10)), 0, 10);
$pic = $random . $_FILES['frontImage']['name'][0];
$url = "../../assets/img/profile/" . $pic;
$uploadURL = uploadImage($image, $url, 70);
return $uploadURL;
}

function uploadImage($src, $dest, $quality)
{
$info = getimagesize($src);

if ($info['mime'] == 'image/jpeg') {
    $image = imagecreatefromjpeg($src);
} elseif ($info['mime'] == 'image/gif') {
    $image = imagecreatefromgif($src);
} elseif ($info['mime'] == 'image/png') {
    $image = imagecreatefrompng($src);
} else {
    die('Unknown image file format. Please upload a jpg, jpeg, gif or a png file.');
}

imagejpeg($image, $dest, $quality);

//  $dest = str_replace("../", "", $dest);
$dest = str_replace(".assets/img/", "", $dest);

return $dest;
}

function NivID($name) {
require '../conn.php';
$content = strtoupper($name);
$sql = "SELECT * FROM niveau WHERE `name`='".$content."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    return $row['id'];
}
else { // Add niv to db if it's unkown
    $sqlNiv = "INSERT INTO `niveau` (`id`, `name`) VALUES (NULL, ?);";
    $stmt = $conn->prepare($sqlNiv);
    $_name = strtoupper($name);
    $stmt->bind_param("s", $_name);
    $stmt->execute();
    $nivid = $conn->insert_id;
    $stmt->close();
    return $nivid;
 }
}

//Add Contactrow
function Contact($id){
require '../conn.php';
$sqlContact = "INSERT INTO `user_contact` (`contact_id`, `user_id`, 
`telnr`, `mobilenr`, `age`, `sex`, `city`) VALUES (NULL, '" . $id . "', 
NULL, NULL, NULL, NULL, NULL);";
if ($conn->query($sqlContact) === TRUE) {
} else {
    echo "Error updating record: " . $conn->error;
 }
}

 //Add Imagerow
function Image($id){
require '../conn.php';
$sqlImage = " INSERT INTO `user_image` (`image_id`, `user_id`, `path`) VALUES (NULL, '" . $id . "', '');";
if ($conn->query($sqlImage) === TRUE) {
} else {
    echo "Error updating record: " . $conn->error;
}
}

//Add Socialrow
function Social($id){
require '../conn.php';
$sqlSocial = " INSERT INTO `user_social` (`social_id`, `user_id`, 
`facebook`, `linkedin`, `twitter`, `instagram`, `youtube`, `website`) VALUES 
(NULL, '" . $id . "', NULL, NULL, NULL, NULL, NULL, NULL);";
if ($conn->query($sqlSocial) === TRUE) {
} else {
    echo "Error updating record: " . $conn->error;
}
}

 //Add Storyrow
function Story($id){
require '../conn.php';
$sqlStory = " INSERT INTO `user_story` (`story_id`, `user_id`, `story`) VALUES (NULL, '" . $id . "', '');";
if ($conn->query($sqlStory) === TRUE) {
} else {
    echo "Error updating record: " . $conn->error;
}
}

//Add Skillrow
function Skill($id){
require '../conn.php';
$sqlSkill = " INSERT INTO `item_skill_label` (`skill_label_id`, `user_id`, `label_5`, `label_6`, `label_7`) VALUES (NULL , '" . $id . "', '', '', '');";
if ($conn->query($sqlSkill) === TRUE) {
} else {
    echo "Error updating record: " . $conn->error;
}
}

 function saveOrganisation($userID, $name, $path, $type, $branche, $niv)
{
require '../conn.php';
$sql = "INSERT INTO `organisations` (`org_id`, `user_id`, `name`, `path`, `type`, `branche`, `niveau`) VALUES (NULL, ?, ?, ?, ?, ?, ?);";
$stmt = $conn->prepare($sql);
$_userID = $userID;
$_name = $name;
$_path = $path;
$_type = $type;
$_branche = $branche;
$_niv = $niv;
$stmt->bind_param("issiii", $_userID, $_name, $_path, $_type, $_branche, $_niv);
$stmt->execute();
$lastID = $conn->insert_id;
$stmt->close();
return $lastID;
}



function updateUser($orgID, $lastID)
{
require '../conn.php';
$sql = "UPDATE `users` SET `org_id` = '" . $orgID . "' WHERE `user_id` = '" . $lastID . "';";
if ($conn->query($sql) === TRUE) {
    header('Location: ../../login');
    die();
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}


//Check of het emailadres al in gebruik is
function usedmail($mail){
require '../conn.php';
$sql = "SELECT * FROM `users` WHERE `username`='" . $mail . "';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    return false;
}
else{
    return true;
}
}

2 个答案:

答案 0 :(得分:0)

$ row2 = mysqli_fetch_array($ result2); //将产生一个数组,我希望 请验证end_date ='$ row2';插入查询中的..部分,以便传递确切的日期(或日期格式)

答案 1 :(得分:0)

include 'functions/conn.php';
$Giveaway = "SELECT `content` FROM `sub_codes` WHERE `current_uses` > '0'";
$result1 = mysqli_query($conn, $Giveaway);
$row = mysqli_fetch_array($result1);

  //here $row is an array , you have to use $row['content']
  if ($_POST['Code']===$row['content']) {

      $sql3 = "SELECT `user_id` FROM `users` ORDER BY `user_id` DESC LIMIT 1";
      $result3 = mysqli_query($conn, $sql3);
      $row3 = mysqli_fetch_array($result3);
      $_start_date = date('Y-m-d');

      //$_end_date = $row2;
      //$row is not yet defined so move this line after $row2;

      $sql2 = "SELECT `end_date` FROM `sub_codes` WHERE `content` = '".$_POST['Code']."'";
      $result2 = mysqli_query($conn, $sql2);
      $row2 = mysqli_fetch_array($result2);

      //again $row2 is an array use $row2['end_date']
      $_end_date = $row2['end_date'];
      $user_id = $row3['user_id'] + 1;
      $sql = "INSERT INTO `partner_subscriptions` SET `user_id` = $user_id, `sub_id` = '99', `allowed_users` = '100', `start_date` = '$_start_date', `end_date` = '$_end_date';";

      if ($conn->query($sql) === TRUE) {
          echo "New record created successfully";
      } else {
          echo "Error: " . $sql . "<br>" . $conn->error;
      }
 else {
  echo "Wrong code";
 }

阅读所有评论,它应该可以工作。