2016-11-27 8 views
0

私はPHP/SQLを初めて使いました。指定されたデータを書式設定されたテーブルに挿入するフォームを作成しようとしています。それをちょっと試してみたら、私は主な機能を働かせることができましたが、私のスクリプトは1列以上のデータを挿入しているようですが、私の人生は理由を理解できません。 、表が作成されPHP/SQLフォーム:挿入が不適切

#!/usr/local/bin/php -d display_errors=STDOUT 
<?php 
    // begin this XHTML page 
    print('<?xml version="1.0" encoding="utf-8"?>'); 
    print("\n"); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:v="urn:schemas-microsoft-com:vml"> 
<head> 
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> 
<title>Accessing a SQLite 3 Database using PHP</title> 
</head> 
<body> 
<p> 
<?php 


$database = "students.db";   


try 
{  
    $db = new SQLite3($database); 
} 
catch (Exception $exception) 
{ 
    echo '<p>There was an error connecting to the database!</p>'; 

    if ($db) 
    { 
     echo $exception->getMessage(); 
    } 

} 


// define tablename + fieldnames 
$table = "bruins"; 
$field1 = "name"; 
$field2 = "sid"; 
$field3 = "gpa"; 


// Create the table 
$sql= "CREATE TABLE IF NOT EXISTS $table (
$field1 varchar(100), 
$field2 int(9), 
$field3 decimal(3,1) 
)"; 
$result = $db->query($sql); 

print "<h3>Creating the table</h3>"; 
print "<p>$sql</p>"; 

// Extract SID and GPA from the $_GET data. 
$name = $_GET['name']; 

$SID = $_GET['SID']; 

$GPA = $_GET['GPA']; 


// Insert a new record to DB with name = $name, sid = $SID and gpa = $GPA 
$sql = "INSERT INTO $table ($field1, $field2, $field3) 
    VALUES ('$name', '$SID', '$GPA')"; 


print "Inserting a new record to the bruins table the command I am using is:</br>"; 
print "$sql"; 
$result = $db->query($sql); 


// print an XHTML table to display the current table 
$sql = "SELECT * FROM $table"; 
$result = $db->query($sql); 


print "<table border='border'>\n"; 
print " <tr>\n"; 
print "  <th>" . $field1 . "</th>\n"; 
print "  <th>" . $field2 . "</th>\n"; 
print "  <th>" . $field3 . "</th>\n"; 
print " </tr>\n"; 

// obtain the results from the SELECT query as an array holding a record 
while($record = $result->fetchArray()) 
{ 
    print " <tr>\n"; 
    print " <td>" . $record[$field1] . "<td>\n"; 
    print " <td>" . $record[$field2] . "<td>\n"; 
    print " <td>" . $record[$field3] . "<td>\n"; 
    print " </tr>\n"; 
} 

print "</table>\n"; 
?> 
</body> 
</html> 

データを提出すると、しかし、すべてのSID文字列はGPA列であり、GPAの文字列は、独自の空欄に配置されています。ここで私が作ったスクリプトです。どんなアドバイス/洞察力も素晴らしいでしょう:-)

+0

最初は正しく見えましたが、結果を画面に表示できますか?これはそれではないかもしれませんが、​​を正しく閉じることはできません。 –

+0

で閉じてください。これは本当にちょうど....​​タグを正しく閉じることです。私はばかげた笑いを感じる。ありがとうございました!! – demboiz

+0

恐ろしい@demboiz、私はそれがうまくいった:) –

答えて

0

私はちょうど誰かがコメントにそれを見ない場合には、ここで再び答えを掲示しています。 コードの最後にsを正しく閉じて、問題を解決しました。

関連する問題