Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A common way would be to store the id as a hidden field when you are editing the page. This way when the user submits the page, if there is an id present, you issue the UPDATE commands, and if there isn't one present, you know it's a new page, and issue the INSERT commands.</p> <p>If you need me to be more thorough let me know.</p> <p>Edit: Being More Thorough</p> <p>I'll make a simple, complete, example of what I mean.</p> <p>Form.php pseudo code</p> <pre><code>//set default values for fields //print form tag if (isset($'id',$_GET)) { //fetch data from database //print hidden id field //override default values for fields } //print rest of fields using default values (possibly overridden) </code></pre> <p>DoForm.php pseudo code</p> <pre><code>//Sanitize user input if (isset('id',$_POST)) { //UPDATE database with user input } else { //INSERT new rows into table with user input } </code></pre> <p>Let's say you have a php file called Form.php which is responsible for displaying the form, and another php script called DoForm.php which is responsible for handling the form.</p> <p>If a user visits Form.php with no ID specified (http://example.com/Form.php) then it will display the following form:</p> <pre><code>&lt;form method="post" action="DoForm.php"&gt; &lt;input type="text" name="name" value="" /&gt; &lt;input type="text" name="title" value="" /&gt; ... other stuff ... &lt;/form&gt; </code></pre> <p>The user will add some information, click on the submit button and DoForm will get the following POST variables:</p> <pre><code>"name" =&gt; "NewPageName" "title" =&gt; "My First Webpag" [intetional typo, see later] ... other stuff ... </code></pre> <p>DoForm will check to see if <code>$_POST['id']</code> exists. Since it doesn't DoForm issues the INSERT commands to add a new page.</p> <p>Later on, the user realises the made a typo, and goes to fix it. The user clicks on the "Edit Page" control for "NewPageName" which will be <code>http://example.com/Form.php?id=1</code></p> <p>Form.php see's that id is set, so the form it prints out is as follows:</p> <pre><code>&lt;form method="post" action="DoForm.php"&gt; &lt;input type="hidden" name="id" value="1" &lt;input type="text" name="name" value="NewPageName" /&gt; &lt;input type="text" name="title" value="My First Webpag" /&gt; ... other stuff ... &lt;/form&gt; </code></pre> <p>The user fixes their type, changing Webpag to Webpage, and hits submit. DoForm gets the following Post variables</p> <pre><code>"id" =&gt; 1 "name" =&gt; "NewPageName" "title" =&gt; "My First Webpage" ... other stuff ... </code></pre> <p>DoForm sees that id is set, and so uses UPDATE instead of INSERT.</p> <p>Any more clear?</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