检查| Drop |创建|插入表用PHP的ODBC
我真的为此而努力......检查| Drop |创建|插入表用PHP的ODBC
我得到这个东西的工作方式约90%...什么我有如果表中不存在,正在核实麻烦或不...
我有2个像页面,第一个只发送$_POST
到主要功能页面。这是一个单一的输入。
场景:一个输入提交到submit.php并插入到临时数据库中,然后在此页面上可以插入更多输入,直到完成...我们将在那里停止。
它需要做什么,检查以确保表格从一开始就不存在,如果存在,则将其放下。创建表格 - 然后插入并更新行数。
这是我到目前为止有:
#17 - $FirstSub = $_SESSION['version']; //True if this is the first time page has loaded
if(isset($conn)){ // Check connection
//create table string
$createTemp = "CREATE TABLE temp_Ticket_Tracker(
PickT_Num int NOT NULL UNIQUE,
us_name varchar(20) NOT NULL,
ticket_status varchar(10) NOT NULL,
time_stamp varchar(20) NOT NULL,
PRIMARY KEY(PickT_Num)
)";
//Insert String
$insert = "INSERT INTO temp_Ticket_Tracker VALUES ('". $_POST['PickNum'] ."','{$_SESSION['User']}','GREEN','{$TimeStamp}')";
//row count string
$query = "Select COUNT(*) as TicketNum FROM temp_Ticket_Tracker";
//Duplicate tester string
$tested = "SELECT PickT_Num FROM temp_Ticket_Tracker WHERE '". $_POST['PickNum'] ."' = PickT_Num";
//Drop's table
$drop = "DROP TABLE temp_Ticket_Tracker";
//Here is where it starts to screw up -
#35 - $firstTest = odbc_exec($conn,$query); //execution string
$NumRows = odbc_fetch_array($firstTest); //Sets array
$countingRows = $NumRows['TicketNum'];
//This is "Supposed" to ask is this the first time you've been here and does the table exist? Answer (Yes, Yes)
if($FirstSub === 'true' && $countingRows >= 1){
odbc_exec($conn,$drop);
odbc_exec($conn,$createTemp);
odbc_exec($conn,$insert);
$_SESSION['version'] = 'false';
$int = odbc_exec($conn,$query);
$intNum = odbc_fetch_array($int);
$TicCount = $intNum['TicketNum'];
odbc_close($conn);
//Again asks if first time and does the table exist? Answer should be(Yes, NO)
}else if($FirstSub === 'true' && !$firstTest){
odbc_exec($conn,$createTemp);
odbc_exec($conn,$insert);
$_SESSION['version'] = 'false';
$int = odbc_exec($conn,$query);
$intNum = odbc_fetch_array($int);
$TicCount = $intNum['TicketNum'];
odbc_close($conn);
//Asks if this is first time and assums Table is created
}else if($FirstSub === 'false'){
$is = odbc_exec($conn,$tested);
$test = odbc_fetch_array($is);
//Check to make sure duplicate has not been entered
if($test == NULL){
odbc_exec($conn,$insert);
$int = odbc_exec($conn,$query);
$intNum = odbc_fetch_array($int);
$TicCount = $intNum['TicketNum'];
odbc_close($conn);
//Passes error if duplicate is used
}else{
echo '<script type="text/javascript">',
'window.onload = function() {',
'document.getElementById("error").innerHTML = "You have already scanned this ticket!";};',
'</script>';
$int = odbc_exec($conn,$query);
$intNum = odbc_fetch_array($int);
$TicCount = $intNum['TicketNum'];
}
}else{
echo '<p> OOPS! looks like we may have a problem!</p>';
$_SESSION['version']='true';
}
}else{echo "Try again...";}
一切完美的作品!除了当整个表被提交(关闭屏幕),并临时表被删除...
输入进来的[FirstSub = "true" & table does NOT Exist]
我得到这个错误...
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'temp_Ticket_Tracker'., SQL state S0002 in SQLExecDirect in C:\Program Files (x86)\EasyPHP5.3.0\www\idea\ticket\greenFunction.php on line 35
Warning: odbc_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP5.3.0\www\idea\ticket\greenFunction.php on line 36 (Line numbers look like #n -)
我想您正在寻找此SQL命令:
IF OBJECT_ID ('YourDatabase.YourSchema.temp_Ticket_Tracker') IS NOT NULL
DROP TABLE Temp_Ticket_Tracker
这将检查表是否存在,然后删除它,如果它。用它替换$drop
,并且可以取出一些其他表格检查逻辑。
$drop = "IF OBJECT_ID ('YourDatabase.YourSchema.temp_Ticket_Tracker') IS NOT NULL DROP TABLE Temp_Ticket_Tracker";
非常感谢!我会更新我的代码。 –