2019-03-13 07:52

函数php中的if / else语句没有插入[关闭]


what i want to do is that it checks the input field and after that it will insert the following query or it it gives an error message. My problem is that my query won't insert.

My PHP function that won't work (other file then html file):

function Code($userID) {
require '../conn.php';
    $sql = "SELECT `current_uses` FROM `sub_codes` WHERE `content` = '".$_POST['Code']."'";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_array($result);
    if ($row['current_uses'] > 0){
        $query = "INSERT INTO `partner_subscriptions` (`id`, `user_id`, `sub_id`, `allowed_users`, `start_date`, `end_date`) VALUES (NULL, ?, ?, ?, ?, ?);";
        $stmt = $conn->prepare($query);
        $_userID = $userID;
        $_subID = '99'; 
        $_allowedUsers = '100';
        $_startDate = date('Y-m-d');
        $sql2 = "SELECT `end_date` FROM `sub_codes` WHERE `content` = '".$_POST['Code']."'";
        $result2 = mysqli_query($conn, $sql2);
        $row2 = mysqli_fetch_array($result2);
        $_endDate = $row2['end_date'];
        $stmt->bind_param("sssiiii", $_userID, $_subID, $_allowedUsers, $_startDate, $_endDate);
        $lastID = $conn->insert_id;
        return $lastID;
    }else {
        echo "Wrong code";

My html file:

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

The rest of my PHP file (that i think you need to know):

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']);

Code($lastID, $_POST['Code']);
$path = saveImage();
$orgID = saveOrganisation($lastID, $_POST['organisation'], $path, $_POST['type'], $_POST['branche'], $niv);
updateUser($orgID, $lastID);
else {
 header('Location: ../../mailerror');

every other function works normal except the code function and i don't really know why. I appreciate your help!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsxon40042 dsxon40042 2年前

    I had to change "sssiiii" to "iiiss" because Every single character of your 'sssiiii' stands for a single value that is bound to the statement.

    点赞 评论 复制链接分享
  • doufutao4428 doufutao4428 2年前

    Well, for explanation reasons how to use mysqli the right way. First of all, you have to keep control of your code. Always check what happens and catch any mistakes. You don 't do that and that 's the reason you don 't know, why your insert statement is not executed.

    Error Handling for the win!

    Use the results, which are explained in detail in the manual. Nearly every mysqli method returns a false value, when something went wront. Use it!

    $sql = "SELECT current_uses FROM sub_codes WHERE content = ?";
    $stmt = mysqli_prepare($connection, $sql);
    // Is there a prepared statement?
    if (!$stmt) {
        die(printf('Something went wrong: %s.', mysqli_error($connection)));
    // use the mysqli statement (one type definition per used variable)
    $result = mysqli_stmt_bind_param($stmt, "s", $_POST['code']);
    if (!$result) {
        die(printf('Something went wrong: %s.', mysqli_stmt_error($stmt)));
    // execute the statement
    $result = mysqli_stmt_execute($stmt);
    if (!$result) {
        die(printf('Something went wrong: %s.', mysqli_stmt_error($stmt)));

    As you can see it is necessary to check what the result of each mysqli function call is to avoid unpredictable behavior of your script. Always keep in mind not to use post variables directly in sql statements. This is a huge mistake and opens your script for several vulnerabilities via sql injection.

    Please read one of the many sql injection topics here on stack overflow to understand what sql injection is and how you can prevent it: How can I prevent SQL injection in PHP?

    点赞 评论 复制链接分享