Results 1 to 5 of 5

Thread: Cant get form to Post to mysql database.

  1. #1
    Join Date
    Aug 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant get form to Post to mysql database.

    My head is wrecked at my first attempt at posting to a mysql database.

    When i click submit I just get an error message from the "Database.php" script.

    I'd be greatful if some one could review my code and see if you and spot any amature mistakes.

    All the database names password and usernames are correct so I suspect something wrong in the php code.

    Here is my code.
    Code:
     <html>
    <head><title>DATABASE SUBMIT FORM</title></head>
    
    <body>
    
    
    
    <form method="post" action="Database.php">
    
    Title: <input type="text" name="title" size="30"><br>
    
    Description: <input type="text" name="description" size="30"><br>
    
    Date: <input type="text" name="date" size="30" value=""><br>
    
    Url: <input type="text" name="url" size="35" value="http://www.mywebsitename.com"><br>
    
    Photographer: <input type="text" name="photographer" size="30"><br>
    
    
    <input type="Submit" value="Submit to Database">
    
    </form>
    
    
    
    </body>
    
    </html>

    Code:
     <?php
    
    $title = $_POST['title'];
    
    $description = $_POST['description'];
    
    $date = $_POST['date'];
    
    $url = $_POST['url'];
    
    $photographer = $_POST['photographer'];
    
    
    
    mysql_connect("www.mysite.com", "u*******1", "5******d") or die(mysql_error());
    
    mysql_select_db ("database name");
    
    $query = "INSERT INTO K4 (title, description, date, url, photographer) VALUES ('NULL','".title."', '".description."', '".date."', '".url."', 
    
    '".photographer."')";
    
    mysql_query($query) or die ('Error updatng the database');
    
    echo " Database Updated With: ".title. " ".description." ".date." ".url." ".photographer."";
    
    ?>
    I was havng trouble creating a table with phpMyadmin but found this script that done the job for me.

    Code:
     <?php
    // Connects to your Database
    mysql_connect("www.mysite.com", "u*****1", "5*****d") or die(mysql_error());
    
    mysql_select_db("database name") or die(mysql_error());
    
    mysql_query("CREATE TABLE K4 
    (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    title VARCHAR(30), 
    description VARCHAR(30), 
    date VARCHAR(30), 
    url VARCHAR(30), 
    Photographer VARCHAR(30)
    )");
    
    Print "Your table has been created";
    ?>
    Last edited by trazix; 01-05-2010 at 01:23 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Make sure your also including a mysql_error() after the mysql_select_db!

    Change your $query variable to:
    PHP Code:
    $query "INSERT INTO K4 (title, description, date, url, photographer) VALUES ('NULL','".$title."', '".$description."', '".$date."', '".$url."', 

    '"
    .$photographer."')"
    And change:
    PHP Code:
    echo " Database Updated With: ".title" ".description." ".date." ".url." ".photographer.""
    To:
    PHP Code:
    echo "Database Updated With: $title $description $date $url $photographer"
    Good luck! If it doesn't work, please post the error
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your reply Nile, after doing what you said i get the following message

    Parse error: syntax error, unexpected T_STRING in /mnt/w0802/d31/s23/b0317026/www/mywebsitename.com/KMC/database/Database2.php on line 17

    Not being to familliar with php or mysql is this the correct place to put the error statement.

    Code:
     
    mysql_select_db ("d60651442") mysql_error();

    This is how my code looks now.
    Code:
    <?php
    
    $title = $_POST['title'];
    
    $description = $_POST['description'];
    
    $date = $_POST['date'];
    
    $url = $_POST['url'];
    
    $photographer = $_POST['photographer'];
    
    
    
    mysql_connect("www.mywebsitename.com", "u*******1", "5******d") or die(mysql_error());
    
    mysql_select_db ("d60651442") mysql_error();
    
    
    $query = "INSERT INTO K4 (title, description, date, url, photographer) VALUES ('NULL','".$title."', '".$description."', '".$date."', '".$url."', 
    
    '".$photographer."')";
    
    mysql_query($query) or die ('Error updatng the database');
    
    echo "Database Updated With: $title $description $date $url $photographer";
    
    ?>

  4. #4
    Join Date
    Oct 2008
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default

    try this code for your database.php.
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","u*******1","5******d");
    if (!
    $con) {
        die(
    'Could not connect: ' mysql_error());
    }
    mysql_select_db("d60651442"$con);
    $sql="INSERT INTO K4 (title, description, date, url, photographer) 
    VALUES ('
    $_POST[title]','$_POST[description]','$_POST[date]','$_POST[url]','$_POST[photographer]')";
    if (!
    mysql_query($sql,$con)) {
        die(
    'Error: ' mysql_error());
    }
    ?>
    This is what i use for my insert.

  5. #5
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    Try This:
    Form:
    Code:
    <form method="post" action="Database.php">
    Title: <input type="text" name="title" size="30"><br>
    Description: <input type="text" name="description" size="30"><br>
    Date: <input type="text" name="date" size="30" value=""><br>
    Url: <input type="text" name="url" size="35" value="http://www.mywebsitename.com"><br>
    Photographer: <input type="text" name="photographer" size="30"><br>
    <input type="Submit" value="Submit">
    </form>
    Database.php:
    Code:
    <?php
    $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company
    $db_user = "db_username"; // change to your database username
    $db_password = "password"; // change to your database password
    $database = "db_name"; // provide your database name
    $db_table = "table_name"; // provide table name
    
    
    $db = mysql_connect($hostname, $db_user, $db_password);
    mysql_select_db($database,$db);
    ?>
    <HTML>
    <HEAD>
    <TITLE>PAGE_TITLE</TITLE>
    </HEAD>
    <body>
    <p>
      <?php
    if (isset($_REQUEST['Submit'])) {
    # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
    $sql = "INSERT INTO $db_table(name,description,date,url,photographer) values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['description']))."','".mysql_real_escape_string(stripslashes($_REQUEST['date']))."','".mysql_real_escape_string(stripslashes($_REQUEST['url']))."','".mysql_real_escape_string(stripslashes($_REQUEST['photographer']))."')";
    if($result = mysql_query($sql ,$db)) {
    echo '<h1>Thank you</h1>Your details been added to our database<br><br>;
    
    echo "<a href='index.php'>Back to Index</a>"; // page to be took back to after submission
    } else {
    echo "ERROR: ".mysql_error();
    }
    } else {
    ?>
    <form method="post" action="Database.php">
    Title: <input type="text" name="title" size="30"><br>
    Description: <input type="text" name="description" size="30"><br>
    Date: <input type="text" name="date" size="30" value=""><br>
    Url: <input type="text" name="url" size="35" value="http://www.mywebsitename.com"><br>
    Photographer: <input type="text" name="photographer" size="30"><br>
    <input type="Submit" value="Submit">
    </form>
    <?php
    }
    ?>
    </body>
    </html>

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
  •