2012-07-10 50 views
0

我剛開始用PHP交手,也做了一些MySQL作爲下面它的接口。我無法使這個腳本工作,因爲它部分來自一本書,我認爲在某些地方我出錯了。這讓我瘋狂!更新和刪除信息從一個MySQL表

任何幫助,將不勝感激&感謝。

<?php 

require_once "login.php"; 

$db_server = mysql_connect($db_hostname,$db_username,$db_password); 

if(!$db_server) 
{ 
die("Couldn't connect to MySql" . mysql_error()); 
} 

mysql_select_db($db_database,$db_server) 
or die("Sorry, couldn't connect to database" . mysql_error()); 

if(isset($_POST['delete']) && isset($_POST['isbn'])) 
{ 
$isbn = get_post('isbn'); 
$query="DELETE FROM classics WHERE isbn='$isbn'"; 

if(!mysql_query($query,$db_server)){ 
echo "Delete failed: $query ". mysql_error() . "<br/><br/>";  
} 

if(isset($_POST['author']) && 
    isset($_POST['title']) && 
    isset($_POST['category']) && 
    isset($_POST['year']) && 
    isset($_POST['isbn'])){ 

    $author = get_post('author'); 
    $title= get_post('title'); 
    $category = get_post('category'); 
    $year = get_post('year'); 
    $isbn = get_post('isbn'); 


    $query="INSERT INTO classics VALUES". 
    "('$author','$title','$category','$year','$isbn')"; 

    if(!mysql_query($query,$db_server)){ 
    echo "Update content of table failed: ". mysql_error() . "<br/><br/>"; 
    } 
    } 
echo <<<_SOQ 

<form action="sqltest.php" method="post"> 
    <pre> 
    Author <input type="text" name="author"/> 
    Title <input type="text" name="title"/> 
    Category <input type="text" name="category"/> 
    Year <input type="text" name="year"/> 
    ISBN <input type="text" name="isbn"/> 
    <input type="submit" value="ADD RECORD"/> 
    </pre> 
</form> 
_SOQ; 

    $query = "SELECT* FROM classics";  
    $result = mysql_query($query); 

    if(!$result) die("Database access failed: " .mysql_error()); 
    $rows = mysql_num_rows($result); 

    for($i=0;$i<$rows;++$i){ 

    $row = mysql_fetch_row($query); 
    echo <<<_SOQ 

    <pre> 

    Author $row[0]; 
    Title $row[1]; 
Category $row[2]; 
    Year: $row[3]; 
    ISBN: $row[4]; 

    </pre> 

<form action="sqltest.php" method="post">  
<input type="hidden" name="delete" value="yes"/> 
<input type="hidden" name="isbn" value="$row[4]"/>  
<input type="submit" value="DELETE RECORD"/>  
</form> 
_SOQ; 

}  

mysql_close($db_server);  

function get_post($var){ 
return mysql_real_escape_string($_POST[$var]); 
} 

>

+0

哪一部分無法正常工作? – andrewsi 2012-07-10 17:05:58

+0

我在最後一行發生錯誤。 – 2012-07-10 17:06:19

+0

解析錯誤:語法錯誤,H中意想不到的$結束:\ XAMPP \ htdocs中\ PHP_testers \ Accessing_Mysql_in_php \ sqltest.php上線93 – 2012-07-10 17:06:35

回答

1

我想你需要一個額外的大括號:

if (isset($_POST['delete']) && isset($_POST['isbn'])) { 
    $isbn = get_post('isbn'); 
    $query="DELETE FROM classics WHERE isbn='$isbn'"; 

    if(!mysql_query($query,$db_server)){ 
     echo "Delete failed: $query ". mysql_error() . "<br/><br/>";  
    } 
} 

我添加了第二個右括號 - 它現在編譯?

Editted補充:

您收盤HEREDOC引用(_SOQ)都用空格縮進 - 你不能在與結束標記線的起點任何空白。

+0

謝謝,但沒有,不就解決了。 – 2012-07-10 17:15:52

+0

你是否得到相同的錯誤? – andrewsi 2012-07-10 17:21:40

+0

@MikeThornley - 我在回答中添加了另一個問題 – andrewsi 2012-07-10 17:38:44

1

看起來你沒有一個右括號您:

if(isset($_POST['delete']) && isset($_POST['isbn'])) 
+0

已關閉它,但尚未排序。不管怎麼說,還是要謝謝你。 – 2012-07-10 18:42:15

0

非常感謝大家的幫助下,昨日,大家好。我現在設法對這個腳本進行排序。各種各樣的錯字和輕微錯誤都是這個問題,主要是爲了達成諒解。再次感謝。

<?php 

require_once "login.php"; 

$db_server = mysql_connect($db_hostname,$db_username,$db_password); 
if(!$db_server) die("Sorry, MySql connection failure: " . mysql_error()); 

mysql_select_db($db_database, $db_server) 
    or die("Sorry, DB connection failure: " . mysql_error()); 

/*Code to test for deletion of a field of the table*/ 

if(isset($_POST['delete']) && isset($_POST['isbn'])) 
{ 
$isbn = get_post('isbn'); 
$query = "DELETE FROM classics WHERE isbn='$isbn'"; 

if(!mysql_query($query)) die("Sorry, deletion failed: $query" . "<br/><br/>"); 
} 


/*Code to enter fields of the table*/ 

if(isset($_POST['author']) && 
isset($_POST['title']) && 
isset($_POST['category']) && 
isset($_POST['year']) && 
isset($_POST['isbn'])) 
{ 

$author = get_post('author');  
$title = get_post('title');   
$category = get_post('category');  
$year = get_post('year');  
$isbn = get_post('isbn'); 

$query = "INSERT INTO classics VALUES". 
     "('$author','$title','$category','$year','$isbn')"; 

if(!mysql_query($query)) die("Update: $query failed" . mysql_error()); 

} 


echo<<<_SOQ 
<form action="sqltest.php" method="post"><pre> 
Author: <input type="text" name="author"/> 
Title: <input type="text" name="title"/>  
Category: <input type="text" name="category"/> 
Year:  <input type="text" name="year"/> 
ISBN:  <input type="text" name="isbn"/> 
<input type="submit" value="ADD RECORD"/> 
</pre></form> 

_SOQ;

$query = "SELECT* FROM classics"; 
$result = mysql_query($query); 

if(!$result) die("Sorry couldn't get the resource" . mysql_error()); 

$rows = mysql_num_rows($result); 

for($j=0;$j<$rows;++$j){ 
$row = mysql_fetch_row($result); 

echo<<<_SOQ 
<pre> 
Author $row[0] 
Title $row[1] 
Category $row[2] 
Year $row[3] 
ISBN $row[4] 

<form action="sqltest.php" method="post"><pre> 
<input type="hidden" name="delete" value="yes"/> 
<input type="hidden" name="isbn" value="$row[4]"/> 
<input type="submit" value="DELETE RECORD"/> 
</pre></form> 

_SOQ; }

mysql_close($db_server); 

function get_post($var){ 
    return mysql_real_escape_string($_POST[$var]); 
} 

?>