Hi
I am trying to insert a record into an SQLite database, but it only seems to take effect when I insert it the second time. The code I am using is shown below:
<html>
<body>
<h1>Demonstration of SQLite database under Serlient</h1>
<?php
if ($db = sqlite_open('addressbook', 0666, $sqliteerror)) {
sqlite_query($db, 'CREATE TABLE contacts (contactid int identity autoincrement, lastname varchar,firstname varchar, tel varchar,fax varchar,email varchar)');
// CODE TO ADD A CONTACT
echo "Here we are";
if($_REQUEST['action'] != "") {
echo "We have actioon";
if($_REQUEST['action'] == "add") {
echo "Adding...";
$sql = "INSERT INTO contacts (lastname,firstname,tel,fax,email) VALUES ('" . $_REQUEST['lastname'] . "','" . $_REQUEST['firstname'] . "','" . $_REQUEST['tel'] . "','" . $_REQUEST['fax'] . "','" . $_REQUEST['email'] . "');";
echo "SQL: " . $sql;
sqlite_query($db,$sql);
}
if($_REQUEST['action'] == "delete") {
echo "Deleting...";
sqlite_query($db,'DELETE FROM contacts WHERE contactid = $id;');
}
}
} else {
die($sqliteerror);
}
?>
<h2>Addresses</h2>
<?php
$result = sqlite_query($db,'SELECT * FROM contacts;');
if(sqlite_num_rows($result) > 0) {
echo "<table>";
echo "<tr>";
echo "<td>Contact ID</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Tel</td>";
echo "<td>Fax</td>";
echo "<td>E-mail</td>";
echo "<td> </td>";
echo "</tr>";
} else {
echo "<p>No addresses</p>";
}
while(sqlite_next($result)) {
$row = sqlite_fetch_array($result);
echo "<tr>";
echo "<td>" . $row[0] . "</td>"; // contactid
echo "<td>" . $row[1] . "</td>"; // lastname
echo "<td>" . $row[2] . "</td>"; // firstname
echo "<td>" . $row[3] . "</td>"; // tel
echo "<td>" . $row[4] . "</td>"; // fax
echo "<td>" . $row[5] . "</td>"; // e-mail
echo "<td><a href='sqlite.php?action=delete&id=" . $row[0] . "'>Delete</a></td>";
echo "</tr>";
}
if(sqlite_num_rows($result) > 0) {
echo "</table>";
}
?>
<h2>Add Contact</h2>
<form action="sqlite.php" method="get">
<input type="hidden" name="action" value="add">
First Name: <input type="text" name="firstname">
<br>
Last Name: <input type="text" name="lastname">
<br>
Tel: <input type="text" name="tel">
<br>
Fax: <input type="text" name="fax">
<br>
E-mail: <input type="text" name="email">
<br>
<input type="submit" value="Add">
</form>
</body>
</html>