SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot connect php script to xampp database...please help asap..thanks

    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.

    product_insert.html

    Code HTML4Strict:
     <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

    Code PHP:
    <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 product (Description, Quantity, Price, Vend_id)
     VALUES (NULL, '$Description', '$Quantity', '$Price', '$Vend_id')";
     
     $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>
    Last edited by TechnoBear; Nov 2, 2013 at 07:30. Reason: Code tags added

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do any PHP scripts work at all? Do you have a web server properly configured?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Do any PHP scripts work at all? Do you have a web server properly configured?
    Yes I have used other php code and connected correctly.. other then that I dont know anyting about configuring the server correctly. I am taking a class did what they said do, and starting doing homework.. other programs worked.. but this one does not...

    This is what i get when i hit the create button on the from

    ; mysql_select_db( "final_exam") or die(mysql_error()); //echo "Successfully opened DB.
    "; //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"); ?>

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I don't have any experience of XAMPP, but it looks like it's definitely not parsing that .php file. Have you tried a simpler script, or restarting XAMMP?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made some correction to the code

    now im getting this.

    "; mysql_select_db( "final_exam") or die(mysql_error()); echo "Successfully opened DB.
    "; ?>

    here is the new code
    <html>
    <head></head>
    <body>
    PHP Code:
        <?php
                mysql_connect
    ("localhost""root"""
                    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/>";
            
    ?>
            <?php
                
    //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, '
    $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: db_connect.php");        
                
            
    ?>
    <!--Insert Complete: click <a href="product_list.html">here</a> to go back to the list!-->
    </body>
    </html>


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •