Location: PHPKode > projects > SQLite PHP Admin > spa/redit.php
<?php
/***********************************************************************

		Copyright (c) 2011, Andrew V. Pleshakov	(hide@address.com)
		All rights reserved.

		This file is part of SPA (SQLite PHP Admin)
		See file LICENSE.txt for details

************************************************************************/

require_once('include.php');

if ($_POST['type'] == 'edit') {
	if (!isset($_POST['rowid'])) {
		header('Location: main.php');
		die();
	}
	$rowid = intval($_POST['rowid']);
	$columns = '';
	foreach (array_keys($_POST) as $column) {
		if (preg_match('/^column__/',$column)) {
			$columns .= ',\''.str_replace("column__", "", $column). "' = '" .$pla_db->escapeString($_POST[$column]). "'";
		}
	}
	if($columns) {
		$columns = substr($columns,1);
		$_SESSION['lquery'] = "UPDATE '" .$_POST['object']. "' SET " .$columns. " WHERE _ROWID_ = $rowid";
		$query = iconv(DEF_CHARSET,$encoding,$_SESSION['lquery']);
		if($pla_db->exec($query))
			header('Location: tbrowse.php?object='.urlencode($_POST['object']));
		else
			header("Location: redit.php?object=".urlencode($_POST['object'])."&rowid=$rowid&err=".urlencode($pla_db->error));
	}
	else header('Location: tbrowse.php?object='.urlencode($_POST['object']));
	die();
}
else if ($_POST['type'] == 'add') {	// INSERT NEW ROW
	$columns = $values = '';
	foreach (array_keys($_POST) as $column) {
		if (preg_match('/^column__/',$column) && $_POST[$column] != '') {
			$columns .= ',\''.str_replace("column__", "", $column).'\'';
			$values .= ",'" .$pla_db->escapeString($_POST[$column]). "'";
		}
	}
	if($columns) {
		$columns = substr($columns,1);
		$values  = substr($values,1);
		$_SESSION['lquery'] = "INSERT INTO '" .$_POST['object']. "' (" .$columns. ") VALUES(" .$values. ")";
		$query = iconv(DEF_CHARSET,$encoding,$_SESSION['lquery']);
		if($pla_db->exec($query))
			header('Location: tbrowse.php?object='.urlencode($_POST['object']));
		else
			header("Location: redit.php?object=".urlencode($_POST['object'])."&err=".urlencode($pla_db->error));
	}
	else header('Location: tbrowse.php?object='.urlencode($_POST['object']));
	die();
}

if ($_GET['type'] == 'delete') {
	$rowid = intval($_GET['rowid']);
	$_SESSION['lquery'] = "DELETE FROM '{$_GET['object']}' WHERE _ROWID_ = $rowid";
	$pla_db->exec($_SESSION['lquery']);
	header('Location: tbrowse.php?object='.urlencode($_GET['object']));
	die();
}
//////////////////////// MAIN BODY //////////////////////
if (!isset($_GET['object']))
	raiseError('No table selected','Internal error');
$object = $_GET['object'];
$tar['table'] = $object;

if (!isset($_GET['rowid'])) {
	$type = 'add';
	$tar['h3'] = "Add row to table '{$object}'";
}
else {
	$rowid = intval($_GET['rowid']);
	$type = 'edit';
	$tar['h3'] = "Edit row for table '{$object}'";
}
$tar['err'] = '';
if(isset($_GET['err']))
	$tar['err'] = '<div class="error"><b>SQLite error</b><br />'.iconv($encoding,DEF_CHARSET,$_GET['err'])."</div>\n";

$columns = $pla_db->getTableColumns($object);
$k = 0;
$maxk = '';
foreach ($columns as $row) {
	$col_name[$k] = $row['name'];
	$col_value[$k] = $row['type'].' '.$row['notnull'];
	if ($col_value[$k] == '') { $col_value[$k] = 'typeless'; }
	if(preg_match('/INTEGER PRIMARY KEY/i',$row['type'])) {
		$pla_db->query("SELECT max({$row['name']}) FROM '{$object}'");
		$row = $pla_db->fetchArray();
		$maxk = strval(intval($row[0]) + 1);
//		die("Primary key = {$key}, MAX={$maxk}");
	}
	$k++;
}

if ($type == 'edit') {
	$pla_db->query("SELECT * from '$object' WHERE _ROWID_ = $rowid");
	$row = $pla_db->fetchArray();
}
$nr_fields = count($col_name);
$s = '';
for ($i=0; $i<$nr_fields; $i++) {
	$rr['row'] = '';
	$rr['col_name'] = $col_name[$i];
	$rr['col_value'] = $col_value[$i];
	$rr['text'] = preg_match('/TEXT/i',$col_value[$i]);
	if ($type == 'edit') $rr['row'] = htmlentities(iconv($encoding,DEF_CHARSET,$row[$i]),ENT_QUOTES,DEF_CHARSET);
	else if(stripos($col_value[$i],'primary key')) $rr['row'] = $maxk;
	$s .= design_render('extra/edit',$rr);//?
}
$tar['edit'] = $s;
$tar['rowid'] = $rowid;
$tar['type'] = $type;

die(design_render('table/tedit',$tar));
Return current item: SQLite PHP Admin