如何使这个PHP代码按我想要的方式工作?

时间:2017-06-21 20:38:14

标签: php

我试图在我自己的php代码中添加一个if语句,我正在创建de site我需要的东西,但需要绝望的一些帮助,以便按照我想要的方式工作,而不会损坏原始代码

编写代码是为了创建随机单词的翻译。

  1. 首先,我在创建新单词时所做的就是从wordslist中选择单词。
  2. 然后会出现translation table,我可以通过它添加翻译。
  3. 然后,如果我点击保存(“добави”)按钮,就会创建翻译。
  4. 不幸的是,当你单击保存时,翻译在数据库中创建为新的,具有新的word_id,但它们没有链接到我从单词列表中选择的单词,这实际上是一个word_id号。

    基本上,有没有机会将翻译链接到现有的word_id数字,而不是创建新的?

    以下是我目前正在使用的three php个文件:

    // insert_lang.php
    
    require_once("session.php");
    require_once("class.user.php");
    
    $auth_user = new USER();
    $user_id = $_SESSION['user_session'];
    
    $stmt_word = $auth_user->runQuery("SELECT * FROM words WHERE word_id IN (19,20, 21)");
    $stmt_word->execute();
    $num = $stmt_word->rowCount();
    $wordRow=$stmt_word->fetchAll(PDO::FETCH_ASSOC);
    
    if($wordRow)
    {
        foreach($wordRow as $k => $v)
        {
            $stmt_word1 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'English', word_meaning = '".$v['word_name']."'");
            $stmt_word1->execute();
    
            $stmt_word2 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'Български', word_meaning = '".$v['word_name']."'");
            $stmt_word2->execute();
    
            $stmt_word3 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'Гръцки', word_meaning = '".$v['word_name']."'");
            $stmt_word3->execute();
    
            $stmt_word4 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'Латински', word_meaning = '".$v['word_name']."'");
            $stmt_word4->execute();
    
            $stmt_word5 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'Нидерландски', word_meaning = '".$v['word_name']."'");
            $stmt_word5->execute();
    
            $stmt_word6 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'Старобългарски', word_meaning = '".$v['word_name']."'");
            $stmt_word6->execute();
    
            $stmt_word7 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$v['word_id']."', word_language = 'Старогръцки', word_meaning = '".$v['word_name']."'");
            $stmt_word7->execute();
        }
    }
    
    // insert_trans.php
    require_once("session.php");
    require_once("class.user.php");
    
    $auth_user = new USER();
    $user_id = $_SESSION['user_session'];
    
    $word_id = $auth_user->insertQuery("INSERT INTO words SET word_name = '".addslashes($_POST['word_name'])."', word_language = '".addslashes($_POST['word_language'])."', word_status = '1', date_added = '".date('Y-m-d H:i:s')."'");
    
    if(isset($_POST['word_language_meanings']))
    {
        foreach($_POST['word_language_meanings'] as $k => $v)
        {
            $stmt_word1 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$word_id."', word_language = '".addslashes($_POST['word_language_meaning_lang'][$k])."', word_meaning = '".addslashes($v)."', date_added = '".date('Y-m-d h:i:s')."'");
            $stmt_word1->execute();
        }
    }
    
    //echo '<pre>'; print_r($_POST); print_r($_FILES); exit;
    
    header('Location: dictionary.php?word_id='.$word_id.'&language='.$_POST['word_language'].'&page=trans');
    exit;
    
    // save_trans.php
    
    require_once("session.php");
    require_once("class.user.php");
    
    $auth_user = new USER(); 
    $user_id = $_SESSION['user_session'];
    
    $stmt_word = $auth_user->runQuery("UPDATE words SET word_name = '".addslashes($_POST['word_name'])."', word_language = '".addslashes($_POST['word_language'])."' WHERE word_id = '".$_POST['word_id']."'");
    $stmt_word->execute();
    
    $stmt_word1 = $auth_user->runQuery("DELETE FROM word_language_meanings WHERE word_id = '".$_POST['word_id']."'");
    $stmt_word1->execute();
    
    if(isset($_POST['word_language_meanings']))
    {
        foreach($_POST['word_language_meanings'] as $k => $v)
        {
            $stmt_word1 = $auth_user->runQuery("INSERT INTO word_language_meanings SET word_id = '".$_POST['word_id']."', word_language = '".addslashes($_POST['word_language_meaning_lang'][$k])."', word_meaning = '".addslashes($v)."', date_added = '".date('Y-m-d h:i:s')."'");
            $stmt_word1->execute();
        }
    }
    
    //echo '<pre>'; print_r($_POST); print_r($_FILES); exit;
    
    header('Location: dictionary.php?word_id='.$_POST['word_id'].'&language='.$_POST['word_language'].'&page=trans');
    exit;
    

1 个答案:

答案 0 :(得分:0)

检查该值是否已存在,您可以尝试通过操作下面的代码来满足您的需求,直到它工作为止。正如评论中所建议的那样,使用mysqli_num_rows函数检查下面是否有使用此代码的ID:

$sql0 = "SELECT * FROM word_language_meanings WHERE word_id='".$v['word_id']."', word_language = 'English', word_meaning = '".$v['word_name']."'");
$result0 = mysqli_query($conn, $sql0);
    //check to see if id exists by counting rows found
    if (mysqli_num_rows($result0) > 0) {
    //write a query to do "update" instead of insert
}
else {
    //write a query to insert
}

根据您的需要,您可能需要检查多个不同的ID,如果是这样,使用“AND”编辑这个“检查是否存在”查询并进行少量添加/更改并不困难