-3

下面附上我的PHP代码。表mylibrary包含 isbn 和 title 记录。PHP 脚本无法连接到 mysql。为什么 ?

<?php
$conn_error = 'Could not connect.';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db='b_database';

if(!@mysql_connect($mysql_host, $mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
  die($conn_error);

}
?>

<html>
<head>
<meta name="description" content="Php Code for View, Search, Edit and Delete Record" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Student Record</title>
</head>
<body>
<center>
<h1><u>Library Database</u></h1>
</center>
<?
if($_POST["do"]=="store")
{
$isbn=$_POST["isbn"];
$title=$_POST["title"];
if($query_run = mysql_query($query)){
  $query="insert into mylibrary value
  ('$isbn','$title')";
  mysql_query($query);
  echo "Successfully store in DATABASE";
  }
  ?>
  <form name="add" method="post" action="add.php">
  <table style=" border:1px solid silver" cellpadding="5px" cellspacing="0px"
  align="center" border="0">
  <tr>
  <td colspan="4" style="background:#0066FF; color:#FFFFFF; fontsize:
  20px">ADD RECORD</td>
  </tr>
  <tr>
  <tr>
  <td>Enter ISBN</td>
  <td><input type="text" name="isbn" size="20"></td>
  </tr>
  <tr>
  <td>Enter TITLE</td>
  <td><input type="text" name="title" size="20"></td>
  </tr>
  <tr>
  <td colspan="4" align="center"><input type="hidden" name="do" value="store">
  <input type="submit" value="ADD RECORD"></td>
  </tr>
  </table>
  </form>
  <p align="center"><a href="index.php">Go Back to Home</a></p>
  <?
  include("search.php");?>
  </body>
  </html>
4

2 回答 2

0

这是一个数据库连接必填字段:

<?php
# Type="MYSQL"
# HTTP="true"
$hostname_localhost = "localhost";
$database_localhost = "database";
$username_localhost = "root";
$password_localhost = "yourpass";
$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
于 2014-09-25T06:57:30.940 回答
0

错误在于您的if语句,当您使用赋值语句作为布尔值时,这是不正确的。你应该这样做

...
$query_run = mysql_query($query);
if ($query_run) {
echo"Successfully added.";
}

否则该语句将不起作用。

编辑:

 $connect = mysql_connect($mysql_host, $mysql_user,$mysql_pass);    
 if (!$connect) die($conn_error);
 mysql_select_db($mysql_db) or die($conn_error);

我尝试使用此代码代替您的代码,并且有效。我相信你有@几次用符号而不是美元符号

于 2013-08-04T03:00:41.337 回答