0

I'm having a problem updating my DB rows. Inserting is fine but when I'm trying to edit my rows something weird happens:

Image here

I hit edit button in another page, then the user is directed to the addnew.php page and all inputs are filled with data of the row we want to edit. When I click on the Update button, this error occurs.

Here is my addnew.php code:

<?php
  session_start();
  if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
    header('location: login');
  }
  require_once '../core/init.php';
  require_once '../helpers/helper.php';
  include 'includes/header.php';
  $sqlAdd = "";
  //Edit
  if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $sql = "SELECT * FROM words WHERE id = '$edit_id'";
    $getWord = $db->query($sql);
    $word = mysqli_fetch_assoc($getWord);

  }
  //Add
  if (isset($_POST['submit'])) {
    $word = mysqli_real_escape_string($db, sanitize($_POST['word']));
    $phonetic = mysqli_real_escape_string($db, sanitize($_POST['phonetic']));
    $meaning = mysqli_real_escape_string($db, sanitize($_POST['meaning']));
    $engMeaning = mysqli_real_escape_string($db, sanitize($_POST['engMeaning']));
    $example = mysqli_real_escape_string($db, sanitize($_POST['example']));
    $eMeaning = mysqli_real_escape_string($db, sanitize($_POST['eMeaning']));
    if ($word == '' ||
        $phonetic == '' ||
        $meaning == '' ||
        $engMeaning == '' ||
        $example == '' ||
        $eMeaning == ''
        ) {
          echo '<p style="text-align:center;
                          background-color: #E74C3C;
                          margin-top:20px;
                          color:white;
          ">All forms must be filled</p>';
    }else {
      if (isset($_GET['edit'])) {
        $sqlAdd = "UPDATE words SET word = '$word', phonetic = '$phonetic', meaning = '$meaning', engMeaning = '$engMeaning',
         example = '$example', exampleMeaning = '$eMeaning' WHERE id = '$edit_id'";
      }else {
        $sqlAdd = "INSERT INTO words (word,   meaning,   engMeaning,  example,    exampleMeaning,phonetic)
                             VALUES ('$word','$meaning','$engMeaning','$example','$eMeaning',   '$phonetic')";
      }
echo $sqlAdd;
  // $db->query($sqlAdd);
    }
  }
?>

<link rel="stylesheet" href="../styles/admin-style.css">
<title>Add new Words</title>
<div class="container2">

  <?= var_dump($word); ?>
  <form autocomplete="off"  method="post">

    <?= var_dump($word); ?>
    <input type="text" name="word" placeholder="Word" value="<?= ((isset($_GET['edit']))?$word['word']:''); ?>">
    <input type="text" name="phonetic" placeholder="Phonetic" value="<?= ((isset($_GET['edit']))?$word['phonetic']:''); ?>">
    <input type="text" name="meaning" placeholder="meaning" value="<?= ((isset($_GET['edit']))?$word['meaning']:''); ?>">
    <input type="text" name="engMeaning" placeholder="English Meaning" value="<?= ((isset($_GET['edit']))?$word['engMeaning']:''); ?>">
    <textarea type="text" name="example" placeholder="Example" rows="5"><?= ((isset($_GET['edit']))?$word['example']:''); ?></textarea>
    <textarea type="text" name="eMeaning" placeholder="example meaning" rows="5"><?= ((isset($_GET['edit']))?$word['exampleMeaning']:''); ?></textarea>
    <button type="submit" name="submit"><?= ((isset($_GET['edit']))?'Update':'Add'); ?></button>
  </form>
</div>

And the error(one of them):

Warning: Illegal string offset 'word' in C:\wamp64\www\EnglishProject\admin\addnew.php on line 55 Call Stack #TimeMemoryFunctionLocation 10.0002266584{main}( )...\addnew.php:0 a">

var_dump result:

array (size=8)
  'id' => string '23' (length=2)
  'word' => string 'a1aaaaaa' (length=8)
  'meaning' => string 'sssaaaa' (length=7)
  'engMeaning' => string 'assssssssa' (length=10)
  'example' => string 'aaaaaaaaaaaaaaas' (length=16)
  'exampleMeaning' => string 'saaaaaaaaaaaa' (length=13)
  'phonetic' => string 'aaasss' (length=6)
  'views' => string '0' (length=1)
  • How you are getting the value for `$edit_id`? Comment the line `$db->query($sqlAdd);` and use `echo $sqlAdd;`. Check the query. You will get an idea. – Sriram G Jul 20 '18 at 07:30
  • I did it and then tested the SQL code in phpMyAdmin and it worked correctly. The problem occurs when I do it in the form. @SriramG – GoddamnAllien Jul 20 '18 at 08:25
  • Possible duplicate of [Illegal string offset Warning PHP](https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – CBroe Jul 20 '18 at 11:44
  • @CBroe It's not! not even close. – GoddamnAllien Jul 20 '18 at 11:58
  • Do a var_dump after if (isset($_POST['submit'])) { and post it – AllisonC Jul 20 '18 at 12:17
  • It _is_ a duplicate in that it explains what the _reason_ for this error is. You are supposed to make the effort to _understand_ that reason now, and then try and fix your problem yourself with that new knowledge. – CBroe Jul 20 '18 at 12:47
  • 1
    _“I don't understand why people downvote when they don't have the answer.”_ - http://idownvotedbecau.se/noresearch/, http://idownvotedbecau.se/noattempt/ This is not about “not having” the answer, but about how much effort you are willing to make to begin with. The duplicate explains what the reason for the error is - have you understood that? If yes, start thinking about what you need to do now to fix your code in that regard. If still not - then _ask_ about what _exactly_ you did not understand about the explanation. – CBroe Jul 20 '18 at 12:50
  • It's been 3 days I'm trying to fix this problem. I'm not dumb at coding but this error is against everything I know about coding logic! I checked the "duplicated" question page. I read the second answer (with 203 views). It says this error happens when we're trying to use a string as a full array, Here I'm having an array fed by DB. It works correctly when I'm filling the form with its items( $word['word'] ), but when I hit Update it gives me the error. I'm not being lazy, I'm doing everything I can. So I think it's not fair telling me I'm not making enough effort. @CBroe – GoddamnAllien Jul 20 '18 at 16:57
  • _"Here I'm having an array fed by DB"_ - no, you don't. Use `var_dump` to check, even if you are "sure" ... – CBroe Jul 20 '18 at 17:12
  • And where exactly did you place this var_dump statement ...? Show us the full, actual script, please, and not just snippets that could have all or nothing in between. – CBroe Jul 20 '18 at 17:34
  • I did it. I added the result to my question. $word is loaded. @CBroe – GoddamnAllien Jul 20 '18 at 17:34
  • Show us _where_ you placed it, otherwise this doesn't say much. – CBroe Jul 20 '18 at 17:35
  • I edited my question. It's in the above the isset($_POST) snippet.@CBroe – GoddamnAllien Jul 20 '18 at 17:42
  • So you managed to put it in the wrong place ... now put it directly before you start generating that form ... surprise! – CBroe Jul 20 '18 at 18:35
  • Did it. $word is loaded. Again.@CBroe – GoddamnAllien Jul 20 '18 at 18:45
  • Can't be. PHP isn't lying to you just for the fun of it. Show us the _full_ script as-is, including where exactly the debug output is placed. – CBroe Jul 20 '18 at 19:08
  • What do you think happens in your script, when you click that update button? Remember, the form method is POST and the button name is `submit` ... – CBroe Jul 20 '18 at 19:09
  • I edited again. now it contains all the addnew.php code. @CBroe – GoddamnAllien Jul 20 '18 at 19:16
  • I thought my SQL code was wrong. After testing in phpMyAdmin, I saw my SQL code was fine. – GoddamnAllien Jul 20 '18 at 19:19
  • 1
    It is impossible that you would get this debug output, if the addnew.php script was called by submitting that same form. Then `$_POST['submit']` would be set, and the very first thing you do inside that `if` branch, is overwrite `$word` with the mysql-escaped value of the form field named `word`. This could only produce the debug output you have shown _before_ this form was submitted for the first time. – CBroe Jul 20 '18 at 19:20
  • Got it! I had a bad naming. It's fixed. thank you! @CBroe – GoddamnAllien Jul 20 '18 at 19:41

1 Answers1

0

You are using $word['word'] in the HTML part. But you have not assigned the variable $word['word']. You only have $word.

Instead of using $word['example'] use $example. Same for other variables also.

Edit:

There isn't enough information about your code. But you can try like this on your input tag ((isset($word['word']))?$word['word']:'');. This is not a good solution. But you have to learn to debug your code.

Sriram G
  • 369
  • 3
  • 14