I'm having a problem updating my DB rows. Inserting is fine but when I'm trying to edit my rows something weird happens:
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)