0

当我尝试此代码时,出现类似错误;

Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/28/10527728/html/TestSite/page.php on line 20
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/content/28/10527728/html/TestSite/page.php on line 20
Warning: mysql_query() expects parameter 2 to be resource, null given in /home/content/28/10527728/html/TestSite/page.php on line 21
Could not update data: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

代码:

<?php
 ob_start();
 session_start();
 if(!isset($_SESSION['Username'])){
     header("Location: page.php");
 }    

  if(isset($_POST['update']))
  {
        $Memberid = $_POST['Memberid'];
        $Username = $_POST['Username'];

        $sql = "UPDATE members ".
   "SET Username = $Username ".
   "WHERE Memberid = $Memberid" ;

  mysql_select_db($finalform);
  $retval = mysql_query( $sql, $conn );
  if(! $retval )
  {
    die('Could not update data: ' . mysql_error());
  }
  echo "Updated data successfully\n";
  mysql_close($conn);
        }
  else
  {
  ?>

用于编辑成员表的用户名的 HTML,采用数据库最终形式;

  <form method="post" action="<?php $_PHP_SELF ?>">
  <table width="400" border="0" cellspacing="1" cellpadding="2">
  <tr>
  <td width="100">Name</td>
  <td><input name="Username" type="text" id="Username"></td>
  </tr>

  <td width="100"> </td>
  <td>
  <input name="update" type="submit" id="update" value="Update">
  </td>
  </tr>

  </table>
  </form>

  <?php
  } //Curly bracket close from the else from the begining
  ?>

  </body>
  </html>
4

2 回答 2

1

您缺少mysql_connect()函数。你正在关闭$conn,所以让我们使用它:

$conn = mysql_connect('localhost', 'user', 'pass');

话虽如此,您真的应该尝试使用PDO或类似的替代方案

于 2013-03-05T22:40:02.947 回答
0
  $conn = mysql_connect( server, username, password );

IE :

  $conn = mysql_connect( 'localhost', 'userName', 'yourPassword' );

我同意尝试 PDO,对于立即生效的解决方案,请尝试我的解决方案。

于 2013-03-05T22:42:34.537 回答