我的PHP代码如何连接到数据库..使用Xampp

时间:2013-11-02 17:12:50

标签: php mysql apache phpmyadmin xampp

首先请原谅我,如果代码不是有条理的,我试图以一种整洁的方式插入代码,但是当我修复它时,系统超时,所以我不能发布并且必须全部执行再次..所以这次我不打算调整代码..只是将其复制到论坛

我创建了一个名为product_insert.html ..和一个名为product_insert.php的php脚本。它们都位于xampp的htdoc文件夹中名为Final exam的子文件夹中。

将数据输入表单后,下一个屏幕基本上显示了product_insert.php的脚本。我无法弄清楚为什么它没有建立连接。该数据库也称为final_exam。

我已将我的代码编辑为以下内容,但仍然出现错误

<html>
  <head></head>
 <body>

<?php
 mysql_connect("localhost", "root", "Final exam") 
 or die(mysql_error());

//echo "We have successfully connect to our DB.<br/>";

  mysql_select_db( "final_exam") or die(mysql_error());

//echo "Successfully opened DB.<br/>";

//pull values from the URL and put them each in a variable

    $Description = addslashes($_GET["Description"]);
    $Quantity = addslashes($_GET["Quantity"]);
    $Price = addslashes($_GET["Price"]);
    $Vend_id = addslashes($_GET["Vend_id"]);

    if($Description && $Quantity && $Price && $Vend_id)
    {
            echo "test1";

    }
        else
    {
        echo "test2";
    }

    if(isset($Description) && !empty($Description) 
    && isset($Quantity) && !empty($Quantity)
    && isset($Price) && !empty($Price)
    && isset($Vend_id) && !empty($Vend_id))
    {           
      $SQLstring = "INSERT INTO student (id, first_name,last_name,address, e_mail,             
gpa)
VALUES (NULL, '$first', '$last', '$address', '$email', 0.0)";

$QueryResult = @mysqli_query($DBConnect, $SQLstring)
 Or die("Insert Broke!!!");

echo "insert complete";
    }   
    else
    {
    echo "You are missing some values...Please press the back button and retry!";
    }
//redirect back to our list page since the insert worked
 header("location: db_connect.php");        
    ?>{/PHP]

<!--Insert Complete: click <a href="product_list.html">here</a> to go back to the     
list!-->
    </body>
</html>

我已将我的代码编辑为以下内容,但仍然出现错误

<html>
    <head></head>
    <body>
        <?php

        $host = "localhost"; // change this as required
        $username = "root"; // change this as required
        $password = "password"; // change this as required
        $db = "final_exam"; // your DB  

            $DBConnect=mysql_connect("localhost", "root", "password") 
                or die("Could Not Connect");
            //echo "We have successfully connect to our DB.<br/>";

            mysql_select_db( "final_exam")
                or die(mysql_error());
            //echo "Successfully opened DB.<br/>";

            //pull values from the URL and put them each in a variable
            $Description = addslashes($_GET["Description"]);
            $Quantity = addslashes($_GET["Quantity"]);
            $Price = addslashes($_GET["Price"]);
            $Vend_id = addslashes($_GET["Vend_id"]);

            if($Description && $Quantity && $Price && $Vend_id)
            {
                echo "test1";

            }
            else
            {
                echo "test2";
            }

            if(isset($Description) && !empty($Description) 
                && isset($Quantity) && !empty($Quantity)
                && isset($Price) && !empty($Price)
                && isset($Vend_id) && !empty($Vend_id))
            {           
                $SQLstring = "INSERT INTO student (id,                          
VALUES ('$Description', '$Quantity', '$Price', '$Vend_id')";

                $QueryResult = @mysql_query($DBConnect, $SQLstring)
                    Or die("Insert Broke!!!");

                echo "insert complete";
        }   
            else
            {
                echo "You are missing some values...Please press the back 
button and retry!";
            }
            //redirect back to our list page since the insert worked
            header("location: product_list.php");       

        ?>

        <a a href="product_insert.html">Click here</a> to go back to the list!-->
    </body>
 </html>

1 个答案:

答案 0 :(得分:0)

 mysql_connect('localhost', 'mysql_user', 'mysql_password');

您在连接字符串中缺少用户或密码。而不是使用数据库名称。 你也在混合mysqli和mysql函数。