couldn't insert into mysql database, why?

Discussion in 'PHP' started by yuyue_hirakiseira, May 3, 2010.

  1. #1
    in try.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta content="en-us" http-equiv="Content-Language" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>ID</title>
    </head>

    <body>

    <form action="" method="post">
    ID: <input name="Text1" type="text" /><br />
    <br />
    Model: <input name="Text2" type="text" /><br />
    <br />
    Year: <input name="Text3" type="text" /><br />

    <?php
    if(isset($_POST['id']) && isset($_POST['model']) && isset($_POST['year'])){
    $id=$_POST['id'];
    $model=$_POST['model']
    $year=$_POST['year']

    include('test1a.php');

    //$sql = "INSERT into customer (name, address, phone) VALUES ('$name','$add','$phone')";
    $sql = "INSERT into cars (id, model, year) VALUES ('$id','$model','$year')";
    $result = db_query($sql);

    echo '<script type="text/javascript">
    alert(\'The Data Have Been Stored~~\');
    </script>';

    }

    ?>
    </form>

    </body>

    </html>


    the database connection
    <?php
    //connection to the database & server
    $config_dbServerHost = '127.0.0.1';
    $config_dbUsername = 'root';
    $config_dbPassword = '2614352';
    $config_dbName = 'examples';
    $link = mysql_connect($config_dbServerHost, $config_dbUsername, $config_dbPassword)
    or die('Could not connect: ' . mysql_error());

    mysql_select_db($config_dbName) or die('Could not select database');

    function db_query($sQuery)
    {

    $aResult = @mysql_query($sQuery) or die('Query failed: ' . mysql_error() . "<br/> SQL: $sQuery");
    if((substr($sQuery,0,6))=="INSERT")
    {
    $finalResult = mysql_insert_id();
    }
    return $finalResult;
    }

    //select a database to work with
    //$selected = mysql_select_db("examples",$dbhandle)
    // or die("Could not select examples");



    //fetch tha data from the database
    //while ($row = mysql_fetch_array($result)) {
    // echo "ID:".$row{'id'}." Name:".$row{'model'}." Year: ". //display the results
    // $row{'year'}."<br>";
    /*
    $sql="INSERT INTO cars (id, model, year)
    VALUES
    ('$_POST[id]','$_POST[model]','$_POST[year]')";

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

    }
    //close the connection
    mysql_close($dbhandle);
    */
    ?>

    can't get ti insert into the database, could you guys pls help me on it
    i'm new to php and don't expect i know a lot since i'm just more than a beginner...
    thank you
     
    Last edited: May 3, 2010
    yuyue_hirakiseira, May 3, 2010 IP
  2. zac439

    zac439 Notable Member

    Messages:
    3,074
    Likes Received:
    214
    Best Answers:
    0
    Trophy Points:
    260
    #2
    1. - Is this being hosted on your home machine, or on an actual production server?
    2. - How is the script not working? It's hard to diagnose a problem when we don't have symptoms.
     
    zac439, May 4, 2010 IP
  3. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it's on my laptop...
    well i just hav no idea cause it just can't insert into my database...
    i have some data in my database too...just can retrieve all the data but not insert and other functions...
    just no idea why... as it can connect to database
     
    yuyue_hirakiseira, May 4, 2010 IP
  4. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #4
    any errors or warning??? can you echo the insert statement
     
    bartolay13, May 4, 2010 IP
  5. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    the connection is ok but i can't run the try.php
    the error is this
    HTTP Error 500.0 - Internal Server Error
    The page cannot be displayed because an internal server error has occurred.
     
    yuyue_hirakiseira, May 4, 2010 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    one reason of the internal error is,

    do you have a .htaccess file in your directory??

    is rewrite_module in your apache enabled??
     
    bartolay13, May 4, 2010 IP
  7. eddieadams

    eddieadams Well-Known Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #7
    Just a wild guess:
    Try commenting out
    #DirectoryIndex index.php

    in your .htaccess file
     
    eddieadams, May 4, 2010 IP
  8. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    err...officially blur now...
    what is .htaccess file and where to locate it?
     
    yuyue_hirakiseira, May 4, 2010 IP
  9. zac439

    zac439 Notable Member

    Messages:
    3,074
    Likes Received:
    214
    Best Answers:
    0
    Trophy Points:
    260
    #9
    I highly doubt you need to meddle with .htaccess.

    Instead, post the code that allows you to get results from the database. There should be a reason it's working and your given code isn't.
     
    zac439, May 4, 2010 IP
  10. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    i totally have no idea where i can locate that file .htaccess...
    apache, isn't it for unix?
     
    yuyue_hirakiseira, May 4, 2010 IP
  11. zac439

    zac439 Notable Member

    Messages:
    3,074
    Likes Received:
    214
    Best Answers:
    0
    Trophy Points:
    260
    #11
    Like I said, I highly doubt the problem is with .htaccess. If you want to solve the problem, refer to my previous post.
     
    zac439, May 4, 2010 IP
  12. yuyue_hirakiseira

    yuyue_hirakiseira Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    may i know what code???
    i've pasted all my code here...so sorry.. i really have no idea...
     
    yuyue_hirakiseira, May 4, 2010 IP