#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Cannot connect php script to xampp database..Please help


    First forgive me if the code is not in an organized manner, I have tried to insert code in a neat manner but by the time I fix it, the system times out, so I cant post and have to do it all over again..so this time I am not going to adjust the code.. just copy past it into forum

    I created a from called product_insert.html.. and a php script called product_insert.php. They are both located in a sub folder called Final exam, in the htdoc folder of xampp.

    After entering data into form, the next screen basically shows me the script of the product_insert.php. I cannot figure out why it does not make the connection. Also the database is called final_exam. Please review code, and let me know what you see is wrong..keep in mind i am a beginner so please do not make assuptions that i must know a particular thing.. pretend i dont know anything and break it down. thank you


    product_insert.html
    This is the form

    <html>
    <head></head>
    <body>
    <form action="Product_insert.php" method="get">
    Description: <input name="Description"/>
    <br/>
    Quantity: <input name="Quantity"/>
    <br/>
    Price: <input name="Price"/>
    <br/>
    <input type="submit" value="Create Product"/>
    </form>
    </body>
    </html>


    product_insert.php
    php code should connect to database

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

    PHP Code:
        <?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");        
        
    ?>
    <!--Insert Complete: click <a href="product_list.html">here</a> to go back to the list!-->
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    i made some code chagnes but still getting errors


    the script is not parsing correctly and does not connect ot database.. using xampp


    <html>
    <head></head>
    <body>
    PHP Code:
    <?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, first_name,last_name,address, e_mail, gpa)
                        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>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Originally Posted by dariusd7
    After entering data into form, the next screen basically shows me the script of the product_insert.php.
    Do you mean it actually shows you the full scripting of the file? If this is the case, it has nothing to do with your database. PHP is not installed correctly.
    He who knows not and knows not he knows not: he is a fool - shun him. He who knows not and knows he knows not: he is simple - teach him. He who knows and knows not he knows: he is asleep - wake him. He who knows and knows he knows: he is wise - follow him
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0
    [QUOTE=Triple_Nothing]Do you mean it actually shows you the full scripting of the file? If this is the case, it has nothing to do with your database. PHP is not installed correctly.[/QUOTe

    Ok how do I fix it?
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    What on earth did you do to XAMPP that it won't recognize PHP files any longer?

    I guess reinstalling it is the easiest solution. Make sure to backup the htdocs folder, all MySQL databases and any custom configuration.

    Apart from that, your code has plenty of issues. Maybe I'll go through that later.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    maybe too simple but how are you navigting to your pages?

    your url should be like http://localhost....
    NOT c:\documents....
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Talking


    Originally Posted by Jacques1
    What on earth did you do to XAMPP that it won't recognize PHP files any longer?

    I guess reinstalling it is the easiest solution. Make sure to backup the htdocs folder, all MySQL databases and any custom configuration.

    Apart from that, your code has plenty of issues. Maybe I'll go through that later.
    I am not surprised there are plenty of issues, because I been researching and changing so many different things to make it work. So if the original issue is the php configuration Then there is no way for me to know what is correct in my code and what isn't because the result never works do to my configuration. So please let me know what is wrong I appreciate it, I really want to learn . I love sql, plsql, php and anything database oriented. So I want to get this...Any advise on a really good tutorial site would be appreciated too...Thanks
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Can you execute any PHP script on your PC? In other words, is the problem limited to this particular script?

    The code issues I mentioned earlier have nothing to do with your current problem. You'll encounter them when your script finally runs:

    • The mysql_* functions you're using are ancient. They've been replaced with modern database extensions almost a decade ago, and they're about to be removed from PHP completely. Don't wait for that to happen. Switch to PDO or MySQLi.
    • You have several security issues like the obsolete and unreliable addslashes() function or the careless exposure of internal error messages. Check The 6 worst sins of security to fix this.
    • Your use of isset() and empty() doesn't make sense. You need to call those functions before you access $_GET values. That's the whole point of isset(): To check if a value exists. Also, using both isset() and !empty() is redundant, because !empty() already implies isset().
    • Never use the GET method for data changes. This is semantically wrong and can lead to huge problems. For example, search engine bots may accidentally trigger a product insertion by visiting the URL. The GET method is strictly for fetching data (hence the name). If you wanna change data, you need POST.
    • Don't use this terrible @ error suppression. Error messages exist for a reason. Hiding them will cover up code problems and make debugging harder.
    • The redirect at the end makes no sense, because this will discard any previous output.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo