Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert data / update if already exists
    text
    copied!<p>This code works. I can't figure out how to insert data into db If user pressed "SAVE" button for the first time or update data.</p> <p><strong>The php side</strong></p> <pre><code>&lt;?php require '../../core/includes/common.php'; $name=filter($_POST['name'], $db); $title=filter($_POST['title'], $db); $parentcheck=filter($_POST['parentcheck'],$db); if(isset ($_POST['parent'])) $parent=filter($_POST['parent'],$db); else $parent=$parentcheck; $menu=filter($_POST['menu'], $db); $content = $db-&gt;escape_string($_POST['content']); $result=$db-&gt;query("INSERT INTO menu (parent, name, showinmenu) VALUES ('$parent', '$name', '$menu')") or die($db-&gt;error); $new_id = $db-&gt;insert_id; $result2=$db-&gt;query("INSERT INTO pages (id, title, content) VALUES ('$new_id', '$title', '$content')") or die($db-&gt;error); if ($new_id&gt;0){ echo "{"; echo '"msg": "success" '; echo "}"; }else{ echo "{"; echo '"err": "error"'; echo "}"; } ?&gt; </code></pre> <p><strong>UPDATE</strong> </p> <p>Thanks to @jmlsteeke i found the way</p> <p>Place this piece of code in html part</p> <pre><code>&lt;?php $result=$db-&gt;query("INSERT INTO menu (parent, name, showinmenu) VALUES ('555', 'new', '0')") or die($db-&gt;error); $new_id = $db-&gt;insert_id; $result2=$db-&gt;query("INSERT INTO pages (id, title, content) VALUES ('$new_id', 'new', 'new')") or die($db-&gt;error); ?&gt; </code></pre> <p>And added following code into form</p> <pre><code>&lt;input type="hidden" name="id" value="&lt;?=$new_id?&gt;"/&gt; </code></pre> <p>In serverside script used </p> <pre><code>$result=$db-&gt;query("UPDATE pages AS p, menu AS m SET m.parent='$parent', m.name='$name', m.showinmenu='$menu', p.id='$id', p.title='$title', p.content='$content' WHERE m.id='$id' AND p.id=m.id") or die($db-&gt;error); </code></pre> <p>Thank you @jmlsteeke </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload