How to insert data into mysql database from Form

Discussion in 'PHP' started by nile1483, Apr 19, 2007.

  1. #1
    Dear friend,

    I am Just Creating News Page in my website so i create html form with only one textarea and i also create database one text field, i want to know how can i insert data into mysql databse and how can i update it, i want to insert data only first time when databse is blank, other time i want to only update same data, i dont want to create second row, so plz help if anybody have idea.

    Thanks
     
    nile1483, Apr 19, 2007 IP
  2. Chamaro Zwinkels

    Chamaro Zwinkels Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For example:

    <form method='post' action='form.php'>
    <input type='text' name='username' maxlength='20'>
    <input type='submit' value='Submit'>
    </form>


    Form.php:

    <?php
    $username=funtion($_POST[username]);
    if(empty($username)){
    echo 'Some field is empty.';
    }else{
    // here query
    // you can make a query in your phpmyadmin en change this with $username
    }
    ?>

    Mind: you have to make function "function"... (see php.net)

    Chamaro
     
    Chamaro Zwinkels, Apr 20, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    That's totally wrong, if you don't know the answer to a question then do not attempt to answer it.

    @op can we see the orginal form so we can make code that will work for you without you editing it ? also a description of the database would be good, or a dump from phpmyadmin even better.
     
    krakjoe, Apr 20, 2007 IP
  4. nile1483

    nile1483 Active Member

    Messages:
    345
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Friend now i add the record by following way, record added successfully but

    my problem is different that i want to add new record if database is blank otherwise any record is present i want to update "present record" it. it means i want to handle only single record how can i do it?


    <?
    $host = "localhost";
    $username="dbusername";
    $password="dbpassword";
    $database="dbname";

    $con = mysql_connect($host, $username, $password);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($database, $con);

    $sql="INSERT INTO home (content)
    VALUES
    ('$_POST[homecontent]')";

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";
    mysql_close($con)
    ?>
     
    nile1483, Apr 20, 2007 IP