PHP菜鸟请教各位大虾,这个程序主要实现将新进的书写入到数据库中,我用的是xampp集成开发环境,到底怎么链接到MysQL里,我不懂啊!
newbook.html:
<html>
<head>
  <title>Book-O-Rama - New Book Entry</title>
</head><body>
  <h1>Book-O-Rama - New Book Entry</h1>  <form action="insert_book.php" method="post">
    <table border="0">
      <tr>
        <td>ISBN</td>
         <td><input type="text" name="isbn" maxlength="13" size="13"></td>
      </tr>
      <tr>
        <td>Author</td>
        <td> <input type="text" name="author" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Title</td>
        <td> <input type="text" name="title" maxlength="60" size="30"></td>
      </tr>
      <tr>
        <td>Price $</td>
        <td><input type="text" name="price" maxlength="7" size="7"></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="Register"></td>
      </tr>
    </table>
  </form>
</body>
</html>insertbook.php
<html>
<head>
  <title>Book-O-Rama - New Book Entry</title>
</head><body>
  <h1>Book-O-Rama - New Book Entry</h1>  <form action="insert_book.php" method="post">
    <table border="0">
      <tr>
        <td>ISBN</td>
         <td><input type="text" name="isbn" maxlength="13" size="13"></td>
      </tr>
      <tr>
        <td>Author</td>
        <td> <input type="text" name="author" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Title</td>
        <td> <input type="text" name="title" maxlength="60" size="30"></td>
      </tr>
      <tr>
        <td>Price $</td>
        <td><input type="text" name="price" maxlength="7" size="7"></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="Register"></td>
      </tr>
    </table>
  </form>
</body>
</html> 

解决方案 »

  1.   

    根本都没看到你连接数据库的代码  哪儿能报PHP Error: Could not connect to database. Please try again later呢?首先你的保证你的数据库启动了 并赋予了某个用户访问某个数据库的权限来自php网站上的一个简单的连接mysql并插入数据的例子 用到了Transactions<?php
    //在这里 先获取你提交的表单信息 比如
    if(isset($_POST['isbn'])){
        $isbn=isset($_POST['isbn'];
    }
    //下面那些数据库的相关信息 你得根据你的实际情况了 插入语句当然也要按你实际情况了
    try {
      $dsn="mysql:host=localhost;dbname=mydb";
      $dbh = new PDO($dsn, 'username', 'password',array(PDO::ATTR_PERSISTENT => true));
      echo "Connected\n";
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  $dbh->beginTransaction();
      $dbh->exec("insert into staff (id, first, last) values (23, 'Joe', 'Bloggs')");
      $dbh->exec("insert into salarychange (id, amount, changedate) values (23, 50000, NOW())");
      $dbh->commit();
      
    } catch (Exception $e) {
      $dbh->rollBack();
      echo "Failed: " . $e->getMessage();
    }
    ?>
      

  2.   

    汗 上面那个获取表单数据的写错了 多写了个isset(if(isset($_POST['isbn'])){
        $isbn=$_POST['isbn'];
    }
      

  3.   

    只需要通过XAMPP界面MySql 后的Admin...找到books数据库,向该数据库增加一个用户,设置用户名:bookorama、密码:bookorama123并赋予权限就行了。 示例代码@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');中的第2、3字串就是指连接数据库的用户名和密码。