0

so i have this website that has all the info stored normally in the db in romanian language. also the site has an option for english and all the data is stored in the db in a weird way (haven't seen this till now)

so this is an entry in the translations table. 57 is the primary key, en is the language, 1 is the id of the translated story, title is the field in the stories table and "photographers of alba iulia" is the translated title

i have to add translations for title, description, and story main text (and later for each picture and so on)

i came up with this code:

$lang = "en";
$story_title = "title";
$stories = "stories";
$story_description = "abstract";
$story_text = "story_text";

if(isset($_POST['title']) && isset($_POST['description']) && isset($_POST['story_text'])) {

    if(!empty($_POST['title'])){
        $sql = "INSERT INTO translations (lang, tabel, cheie, camp, traducere) VALUES (:lang, :table, :story_id, :camp, :title)";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':lang', $lang);
        $stmt->bindParam(':table', $stories);
        $stmt->bindParam(':story_id', $_GET['id']);
        $stmt->bindParam(':camp', $story_title);
        $stmt->bindParam(':title', $_POST['title']);

        if($stmt->execute()){
            $message = "titlul A FOST ADAUGAT";

             if(!empty($_POST['description'])){
                $sql = "INSERT INTO translations (lang, tabel, cheie, camp, traducere) VALUES (:lang, :table, :story_id, :camp, :description)";
                $stmt = $conn->prepare($sql);
                $stmt->bindParam(':lang', $lang);
                $stmt->bindParam(':table', $stories);
                $stmt->bindParam(':story_id', $_GET['id']);
                $stmt->bindParam(':camp', $story_description);
                $stmt->bindParam(':description', $_POST['description']);

                if($stmt->execute()){
                    $message2 = "descrierea a fost adaugata";

                    if(!empty($_POST['story_text'])){
                        $sql = "INSERT INTO translations (lang, tabel, cheie, camp, traducere) VALUES (:lang, :table, :story_id, :camp, :text)";
                        $stmt = $conn->prepare($sql);
                        $stmt->bindParam(':lang', $lang);
                        $stmt->bindParam(':table', $stories);
                        $stmt->bindParam(':story_id', $_GET['id']);
                        $stmt->bindParam(':camp', $story_text);
                        $stmt->bindParam(':text', $_POST['text']);

                        if($stmt->execute()){
                            $message3 = "toate campurile au fost adaugate in baza de date";
                           // header("Location: http://localhost/auth2/index.php");
                        }
                        else {
                            $message2 = "EROARE in adaugarea textului povestii";
                        }

                    }

                }
                 else {
                     $message2 = "EROARE in adaugarea descrierii";
                } 

            }
        }
        else {
            $message3 = "EROARE in adaugarea titlului";
        }
    }
}

else {
    echo "nu ie setate boss";
}

and this is the form

   <form action="translate.php" method="POST" name="myForm">


        <input type="text" name="title" id="titlu_trad" placeholder="title" required>
        <textarea name="description" id="desc_trad" placeholder="description" id="desc" required></textarea>
        <textarea name="text" id="editor1" placeholder="story text" rows="10" cols="60" required></textarea>
        <script>
            // Replace the <textarea id="editor1"> with a CKEditor
            // instance, using default configuration.
            CKEDITOR.replace( 'editor1' );
        </script>
        <br>
        <br>
        <button type="submit" name="submit">Trimite formularul</button>
    </form>    

after i submit i get this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cheie' cannot be null' in C:\xampp\htdocs\auth2\translate.php:75 Stack trace: #0 C:\xampp\htdocs\auth2\translate.php(75): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\auth2\translate.php on line 75

any advice?

Knut Terra
  • 21
  • 1
  • 1
  • 9
  • To find a problem you have to debug your code. Check the provided link for the pointers. – Your Common Sense Jul 11 '16 at 11:20
  • use try catch statement to check error – Mandeep Gill Jul 11 '16 at 11:30
  • wow. just found 7 problems. thanks for the advice :). altought the question is not really a duplicate because one of my concerns is if my approach of an if in an if and so on is correct for adding to the db... anyways, thanks again – Knut Terra Jul 11 '16 at 11:32
  • Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cheie' cannot be null' in C:\xampp\htdocs\auth2\translate.php:75 Stack trace: #0 C:\xampp\htdocs\auth2\translate.php(75): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\auth2\translate.php on line 75 – Knut Terra Jul 11 '16 at 11:49
  • so you know what is your problem now. Feel the power of error reporting :) – Your Common Sense Jul 11 '16 at 11:52
  • yea. that's one step towards success. now what? why is cheie even null? cheie is associated with :story_id which i get from the id from the url and it works because i managed to print it... – Knut Terra Jul 11 '16 at 11:57
  • Why cheie is null is a question you are supposed to answer yourself. Don't you think anyone can tell you why some data on your server is missed? – Your Common Sense Jul 11 '16 at 13:19
  • ok. i finally managed to add data to the db. now i have a different problem. the field cheie that i had problems with adds NULL. the other fields add ok... – Knut Terra Jul 11 '16 at 13:51

0 Answers0