Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>here is an example of such a code, using templates.<br> working CRUD application based on the idea of passing id</p> <p>dunno, though, why do you need to pass freshly generated id.</p> <pre><code>&lt;? mysql_connect(); mysql_select_db("new"); $table = "test"; if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: $name = mysql_real_escape_string($_POST['name']); if ($id = intval($_POST['id'])) { $query="UPDATE $table SET name='$name' WHERE id=$id"; } else { $query="INSERT INTO $table SET name='$name'"; } mysql_query($query) or trigger_error(mysql_error()." in ".$query); header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit; } if (!isset($_GET['id'])) { //listing part: $LIST=array(); $query="SELECT * FROM $table"; $res=mysql_query($query); while($row=mysql_fetch_assoc($res)) $LIST[]=$row; include 'list.php'; } else { // form displaying part: if ($id=intval($_GET['id'])) { $query="SELECT * FROM $table WHERE id=$id"; $res=mysql_query($query); $row=mysql_fetch_assoc($res); foreach ($row as $k =&gt; $v) $row[$k]=htmlspecialchars($v); } else { $row['name']=''; $row['id']=0; } include 'form.php'; } ?&gt; </code></pre> <p>templates:<br> form.php</p> <pre><code>&lt;? include TPL_TOP ?&gt; &lt;form method="POST"&gt; &lt;input type="text" name="name" value="&lt;?=$row['name']?&gt;"&gt;&lt;br&gt; &lt;input type="hidden" name="id" value="&lt;?=$row['id']?&gt;"&gt; &lt;input type="submit"&gt;&lt;br&gt; &lt;a href="?"&gt;Return to the list&lt;/a&gt; &lt;/form&gt; &lt;? include TPL_BOTTOM ?&gt; </code></pre> <p>and list.php:</p> <pre><code>&lt;? include TPL_TOP ?&gt; &lt;a href="?id=0"&gt;Add item&lt;/a&gt; &lt;? foreach ($LIST as $row): ?&gt; &lt;li&gt;&lt;a href="?id=&lt;?=$row['id']?&gt;"&gt;&lt;?=$row['name']?&gt;&lt;/a&gt; &lt;? endforeach ?&gt; &lt;? include TPL_BOTTOM ?&gt; </code></pre>
 

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