Location: PHPKode > projects > HypatiaDB > hypatiadb/tblcreate2.php
<?php

include("common_db.php");
include("functions.php");
dbconnect($host, $username, $password);
$strings = loadStrings($lang, 'TBLC2');
headers();
html();
head($strings['TC2_TITLE']);
menu();
navpane();


if($_SESSION['session_user_status'] == 'viewer' || $_REQUEST['database']) {
	printAuthError(); die();
}

$tbl_name = $_POST['tbl_name'];

echo('<div id="mainpane">');

$errors = "";

if ($_REQUEST) {
	$count = intval($_REQUEST['fieldcount']);
	$key_count=0;

	for($i = 0; $i < $count; $i++) {
		$names[$i] = mysql_real_escape_string($_REQUEST["field$i"]);

//		if(strlen($lvalue) == 0) {
//			$error .= '<p class="warning">' . sprintf($strings['TC_ERR_NONAME'], ($match[1] + 1)) . '</p>';
//		} else if (strlen($lvalue) > 64) {
//			$error .= '<p class="warning">' . sprintf($strings['TC_ERR_TOOLONG'], $lvalue) . '</p>';
//		}
      
		$types[$i]=$_REQUEST["type$i"];
		$lengths[$i]=$_REQUEST["length$i"];
		$nulls[$i]=$_REQUEST["null$i"];
		$defaults[$i]=$_REQUEST["default$i"];
		$indicies[$i]=$_REQUEST["index$i"];

	}
}

$current_db = $_REQUEST['database'];

mysql_selectdb($current_db) or die(sprintf($strings['TC_ERR_SELDB'], mysql_error()));

$table_query = "CREATE TABLE $tbl_name (";

for($i=0; $i <$count; $i++) {
	$table_query .= " `$names[$i]` ";
	switch($types[$i]) {
		case 'VARCHAR':
		case 'TEXT':
			$table_query .= " $types[$i]($lengths[$i]) ";

			if($nulls[$i] == "NULL") {
				$table_query .= " NULL";
			} elseif($nulls[$i] == "NOT NULL") {
				$table_query .= " NOT NULL DEFAULT '" . mysql_real_escape_string($defaults[$i]) . "'";
			}
		break;
		case 'INTEGER':
			$table_query .= " INT ";

			if($nulls[$i] == "NULL") {
				$table_query .= " NULL";
			} elseif($nulls[$i] == "NOT NULL") {
				$table_query .= " NOT NULL DEFAULT '" . mysql_real_escape_string($defaults[$i]) . "'";
			}
		break;
		case 'DATE':
		case 'TIME':
		case 'DATETIME':
			$table_query .= " $types[$i]";

			if($nulls[$i] == "NULL") {
				$table_query .= " NULL";
			} elseif($nulls[$i] == "NOT NULL") {
				$table_query .= " NOT NULL DEFAULT '" . mysql_real_escape_string($defaults[$i]) . "'";
			}
		break;
		case 'ENUM':
			//TODO
		break;
		case 'INT UNSIGNED AUTO_INCREMENT':
			if($has_key) {
				//we already have one auto_increment field!
				printError($strings['TC_MULTIPLEAUTO']);
			} else {
				//all is well
				$table_query .= " INT UNSIGNED AUTO_INCREMENT";
				$has_key = true;
				$pkeys[] = $names[$i];
			}
		break;
	}

	if($i != $count - 1)
		$table_query .= ", ";
}



if(!isset($has_key)) {
	$has_key=false;
}

for($i=0; $i<$count; $i++) {
	if($indicies[$i] == "prime") {
		$has_key = true;
		$pkeys[] = $names[$i];
	}
}

//since we don't support multiple primary keys at the moment, we need
//to fail if there's more than one
if(count($pkeys) > 1)
	printError($strings['TC_MULTIPLEPRIMARY']);

if($has_key) {
	$table_query .= ", PRIMARY KEY (";
	$table_query .= implode(",", $pkeys);
	$table_query .= ")";
}

$has_ind = false;
for($i=0; $i<$count; $i++) {
	if($indicies[$i] == "index") {
		$has_ind = true;
		$idx[] = $names[$i];
	}
}

if($has_ind) {
	$table_query .= ", INDEX (";
	$table_query .= implode(",", $idx);
	$table_query .= ")";
}

unset ($idx);
$has_uind = false;
for($i=0; $i<$count; $i++) {
	if($indicies[$i] == "unique") {
		$has_uind = true;
		$idx[] = $names[$i];
	}
}

if($has_uind) {
	$table_query .= ", UNIQUE INDEX (";
	$table_query .= implode(",", $idx);
	$table_query .= ")";
}

$table_query .= ")";

if(isset($error)) {
	echo $error;
} else {
	if(mysql_query($table_query)) {
		echo('<p>' . $strings['TC_SUCCESS'] . '</p>');
	} else {
		echo('<p class="error">' . mysql_error() . '</p>');
	}
}
   
echo('</div>');
endhtml();

?>
Return current item: HypatiaDB