duanbing6955 2019-03-11 09:40
浏览 51
已采纳

查询不会插入但不会出错

What i'm trying to accomplish with my code is that it first checks if the code is right and then it has to insert the query. But somehow it won't insert... and i don't know what i did wrong on the first sight.

My 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";
}

And my 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/>

This is what i tried so far. If you need more code ask me. But I think this is enough. I don't get any errors.

EDIT

this is the remaining code of my insert page

<?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条回答 默认 最新

  • douhanzhen8927 2019-03-11 10:17
    关注
    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";
     }
    

    read all the comments , it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。