0

我刚刚开始掌握 PHP,并做了一些 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]);   
 }

?>

4

3 回答 3

1

看起来您的以下内容没有右括号:

if(isset($_POST['delete']) && isset($_POST['isbn']))
于 2012-07-10T17:57:15.293 回答
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/>";    
    }
}

我已经添加了第二个右大括号 - 它现在可以编译吗?

编辑添加:

您的结束 HEREDOC 引用 ( _SOQ) 都由空格缩进 - 您不能在带有结束标记的行的开头有任何空格。

于 2012-07-10T17:11:16.893 回答
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]);
}

?>

于 2012-07-11T19:04:49.863 回答