Hello there i would like someone to have a peek at this code and does it solve the question correctly ?? Are there any mistakes or any improvements i could add ??
Also If any one could tell me how to save the data entered by person into one file(XML)so it can be retrieved any time later would appreciate it ;)
Thank you

Question 2:
Write a script to allow a user to add property data into a property table ( call it addproperty.php ). The table should initially have 4 columns for the property data (postcode, price, imagefile, visits). At the end of the script display the whole table contents.


Edit Properties <BR><BR>


$dbhandle = sqlite_popen("propertytable", 1234, $err_msg);
  if(!$dbhandle) die("Could not open the database - run CretaActivityTable.php first");
                        //$field is a variable name, field is the table column name and also the form field name
if ($_POST['use']=="add") {   //ADD NEW ITEM
 $Postcode = $_POST['postcode'];   
 $Price = $_POST['price'];  
 $Imagefile = $_POST['imagefile']; 
 $Visits = $_POST['visits'];
 $query = "INSERT INTO activity (postcode, price, imagefile, visits) VALUES('$Postcode', '$Price', '$Imagefile', '$Visits')";
  if(!sqlite_query($dbhandle, $query)) { echo "Could not insert table row"; }
}  // ADD=TRUE

$Xid =  $_POST['Xid'];  //the table row ID needed to change/update or delete that row

if ($_POST['use']=="change") {  //CHANGE EXISTING ITEM
 $Postcode = $_POST['postcode']; 
 $Price = $_POST['price'];  
 $Imagefile = $_POST['imagefile']; 
 $Visits = $_POST['visits'];
 $query = "UPDATE activity SET postcode='$Postcode', price='$Price', imagefile='$Imagefile', visits='$Visits' WHERE id='$Xid'";
  // print " sql is $query endsql";
  if(!sqlite_query($dbhandle, $query)) { echo "Could not update table row"; }

								// DELETE ITEM
if ( ($_POST['use']=="delete") AND  ( isset($_POST['allowdelete']) ) ) {
  $query = "DELETE FROM activity WHERE id='$Xid'";
  if(!sqlite_query($dbhandle, $query)) { echo "Could not delete table row"; }

$query = sqlite_query($dbhandle, 'SELECT * FROM activity ORDER BY postcode');  //result set goes into query
$result = sqlite_fetch_all($query, SQLITE_ASSOC);  //calls columns by name (or NUM for col num eg 0,1..)

//simple display
 foreach ($result as $arow) {
   print "RowID:". $arow['id'].", Post Code:". $arow['postcode'].", Price:". $arow['price'].", Imagefile:". $arow['imagefile'].", Visits:". $arow['visits']." <br>";

//table and form display 
print "<table>";
print '<tr><td>RowID ...Post Code...Price...Imagefile...Visits </td><td> </td></tr>  ';
foreach ($result as $arow) {
	<form action='' method=post> 
	<input type='hidden' name='Xid' value='<?=$arow['id']?>' />
	 <?=$arow['id'] ?> 
       <input type='text' name='Postcode' value="<?=$arow['postcode']?>" size=4 /> 
       <input type='text'  name='Price' value="<?=$arow['price']?>"  />
       <input type='text' name='Imagefile' value="<?=$arow['imagefile']?>"   /> 	
	   <input type='text' name='Visits' value="<?=$arow['visits']?>"   />
	     <input type='hidden' name='use' value='change' /> 
    <input type='submit' value='Save Row' /> Or
    <td> <form action='' method=post>
          <input type='hidden' name='Xid' value='<?=$arow['id']?>' /> 
		  <input type='checkbox' name='allowdelete'  />
          <input type='hidden' name='use' value='delete' /> 
		  <input type='submit' value='Delete Row (check box first)' /> 
print "</table>";

<form action='' method=post> 
    Post Code 	<input type='text' name='Postcode' /> 
    Price 		<input type='text' name='Price' />
    Image file 	<input type='text' name='Imagefile' /> 
	Visits  	<input type='text' name='Visits' />
				<input type='hidden' name='use' value='add' /> 
				<input type='submit' value='Add' /> 
<?php   //this script can be run once to re-initialise the database -   to drop the existing table and recreate the table 
  $dbhandle = sqlite_popen("propertytable", 1234, $err_msg);
  if(!$dbhandle) die("Could not open the database");

     $query = "DROP TABLE activity";
  if(!sqlite_query($dbhandle, $query)){ echo "table 'activity' not deleted (may be not yet exists)"; }

  $query = "CREATE TABLE activity( id INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY , postcode VARCHAR(25), price VARCHAR(25), imagefile VARCHAR(25), visits(VARCHAR(25))";
  if(!sqlite_query($dbhandle, $query)){ echo "table 'activity' created"; }

 ?> Table created

This code is addpropertytable.php
When i run this code it displays nicely, but when i try to add something to table ( create table) it says "Could not insert table row" why ??
And also how to modify this code to save the data to table and xml(or a txt file) ??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.