PHP消息“错误:没有选择数据库”,关注此脚本

时间:2015-01-01 12:42:23

标签: php database

我有这个脚本,但是当我点击主页面上的“发布”按钮时,我不知道这里有什么问题。错误可能来自哪里?

页面脚本:

<?php 
session_start();
    include("dbconnection.php");
 function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

$messages = clean($_POST['message']);
$user =clean($_POST['name']);
$pic =clean($_POST['name1']);
$poster =clean($_POST['poster']);

$sql="INSERT INTO message (messages, user, picture, date_created, poster)
VALUES
('$messages','$user','$pic','".strtotime(date("Y-m-d H:i:s"))."','$poster')";

mysql_query("UPDATE messages SET picture = '$pic' WHERE FirstName='$user'");
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
header("location: lol.php");
exit();

$name=$_POST['name'];
$pic=$_POST['name1'];

mysql_query("UPDATE messages SET picture = '$pic' WHERE FirstName='$name'");
?> 

这是dbconnect文件:

$con = mysql_connect("hostname","username","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("asl", $con);
?>

3 个答案:

答案 0 :(得分:0)

此错误似乎是由选择wrong databasenot selecting it.

引起的

检查dbconnection.php及其中的这一行

mysql_select_db("your_database_name",$your_connection);

查看此行是否存在并指向数据库,并确保此数据库存在

更新似乎您的文件未包含在试用require()中,因此会产生致命错误,您可以看到文件是否包含

 require("dbconnection.php"); // will produce fatal errors 

答案 1 :(得分:0)

确保您已使用mysql_select_db

选择了数据库
mysql_connect('hostname','username','password') or die("not able to connect");
mysql_select_db('myDatabase');

不推荐使用mysql_扩展名。不要使用它

答案 2 :(得分:0)

首先,mysql_connect过时且不安全,更好地使用PDO

    <?php 

    session_start();
    include("dbconnection.php");
    function clean($str) {
            $str = @trim($str);
            if(get_magic_quotes_gpc()) {
                        $str = stripslashes($str);
            }
            return mysql_real_escape_string($str);
        }

    $messages = clean($_POST['message']);
    $user =clean($_POST['name']);
    $pic =clean($_POST['name1']);
    $poster =clean($_POST['poster']);

    $sql = $db->prepare("INSERT INTO message (messages, user, picture, date_created, poster) VALUES (:messages, :user, :picture, :date_created, :poster)");
    $sql->bindParam(':messages', $messages);
    $sql->bindParam(':user', $user);
    $sql->bindParam(':picture', $pic);
    $sql->bindParam(':date_created', strtotime(date("Y-m-d H:i:s")));
    $sql->bindParam(':poster', $poster);

    $stmt = $db->prepare("UPDATE messages SET picture = :picture WHERE FirstName = :user");
    $stmt->bindParam(':picture', $pic);
    $stmt->bindParam(':user', $user);
    $stmt->execute();


    if (!$sql->execute())
    {
            die('Error: ' . mysql_error());
    }


    $name=$_POST['name'];
    $pic=$_POST['name1'];

    $stmt_2 = $db->prepare("UPDATE messages SET picture = :picture WHERE FirstName = :name");
    $stmt_2->bindParam(':picture', $pic);
    $stmt_2->bindParam(':name', $name);
    $stmt_2->execute();

    header("location: lol.php");

    ?> 

这是dbconnect文件:

    <?php
            //Connect to sql db
            try {
                    $user_db = "username";
                    $pass_db = "password";
                    $db = new PDO('mysql:host=localhost;dbname=asl', $user_db, $pass_db);

            } catch (PDOException $e) {
                    print "Error!: " . $e->getMessage() . "<br/>";
                    die();
            }

    ?>