PHP can not get script to connect to database
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: can not get script to connect to database

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    can not get script to connect to database

    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:
    <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:
    <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 PeejAvery; November 11th, 2013 at 01:13 PM. Reason: Added proper coding tags

  2. #2
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    277

    Re: can not get script to connect to database

    PHP Code:
    $conn mysql_connect($dbHostAddress$username$password);

            if (!
    $conn) {
                die(
    'Could not connect: ' mysql_error());
            }

            
    mysql_select_db($databaseName);

            
    $results mysql_query($$sqlQuery$conn);
            if (!
    $results) {
                die(
    'Invalid query: ' mysql_error());
            }

            
    //mysql_close($conn);

            //I use this to return each row as an array
            
    $stack=array();
            while (
    $ra=mysql_fetch_array($results)) {
                 
    array_push($stack,$ra);
            }

    //....and then later:

    foreach($stack as $column => $value){
        echo 
    '<br>here is a column: '.$column;
        echo 
    '<br>here is a column value: '.$value;

    Last edited by PeejAvery; November 11th, 2013 at 01:14 PM. Reason: Added PHP tags
    David Meikle
    Quantum Unit Solutions, LLC
    www.quantumunit.com
    FusionSEO RSS Feeds

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center