Results 1 to 6 of 6

Thread: Need help in PHP post forms to MySQL

  1. #1
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Need help in PHP post forms to MySQL

    Hello,
    I wrote that code on my website:
    HTML Code:
    <form action="comments.php" method="post">
              <input type="hidden" name="id" value="NULL" />
              <input type="hidden" name="data" value="NULL" /></p>
    		<p>Komentaras:  <br />
              <input name="Komentarai" height="50px" type="text" value="" size="60" maxlength="5000" />
    	    <br />
    		<input type="submit"  value="Komentuoti"/>
    </form>
    in another file comments.php i wrote:
    PHP Code:
    <? 
    $DBazes_serveris 
    "xxxxx.xxxxxxx.com";
    $DBazes_database "xxxxxx_xxxx";
    $DBazes_vartotojas "xxxxx_xxxx";
    $DBazes_slaptazodis "xxxxxxx";
    $DBazes_lentele "PHPMySQL";
    //////////////////////////////////////////// Prisijungimui 
    $login mysql_connect($DBazes_serveris$DBazes_vartotojas$DBazes_slaptazodis) or die ('Nepavyko prisijunkti');
    //////////////////////////////////////////// LENTELE
    @mysql_select_db($DBazes_lentele);
    $irasymas_i_db "INSERT INTO $DBazes_lentele VALUES ( '$id','$Komentarai', '$data') ";
    $rezultatas mysql_query($irasymas_i_db);
    mysql_close();

    print 
    "
    Jusu komentaras: 
    $Komentarai
    "
    ;
    ?>
    These codes i think have mistakes, so If you could help to fix my problem please tell me ;(
    Or if you know another way to post form info to MySQL please tell me, cause I want to learn how to post and get all info to show on php file.
    Last edited by Total_me; 08-22-2009 at 02:50 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You're not calling the function mysql_query();, you're only associating a variable with it. Additionally, you need to have the 'id' as your PRIMARY_KEY on your MySQL system, and set it to AUTO_INCREMENT, as INTEGER.

    This will fix the query porblem for oyu:
    PHP Code:
    <? 
    $DBazes_serveris 
    "xxxxx.xxxxxxx.com";
    $DBazes_database "xxxxxx_xxxx";
    $DBazes_vartotojas "xxxxx_xxxx";
    $DBazes_slaptazodis "xxxxxxx";
    $DBazes_lentele "PHPMySQL";
    //////////////////////////////////////////// Prisijungimui 
    $login mysql_connect($DBazes_serveris$DBazes_vartotojas$DBazes_slaptazodis) or die ('Nepavyko prisijunkti');
    //////////////////////////////////////////// LENTELE
    @mysql_select_db($DBazes_lentele);
    $irasymas_i_db "INSERT INTO $DBazes_lentele VALUES ( '$id','$Komentarai', '$data') ";
    $rezultatas mysql_query($irasymas_i_db);
    mysql_close();

    echo 
    "Jusu komentaras:".$Komentarai;

    ?>
    HTH
    - Josh

  3. The Following User Says Thank You to JShor For This Useful Post:

    Total_me (08-22-2009)

  4. #3
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Kk i find out. If any1 wants to know how had to be here you go. I hope it will be great example:
    [html]<form action="comments.php" method="post">
    <input type="hidden" name="id" value="NULL" />
    <input type="hidden" name="data" value="NULL" /></p>
    <p>Komentaras: <br />
    <input name="Komentarai" height="50px" type="text" value="" size="60" maxlength="5000" />
    <br />
    <input type="submit" value="Komentuoti"/>
    </form>[html]
    comments.php
    PHP Code:
    <? 
    $DBazes_serveris 
    "xxxxxxxxxx"///////// Server, host
    $DBazes_database "xxxx_mysql"///////// DataBase
    $DBazes_vartotojas "xxxx_mysql"////////// Username
    $DBazes_slaptazodis "xxxxx"////////// Password
    $DBazes_lentele "table"/////// Table name
    //////////////////////////////////////////// Prisijungimui 
    $con mysql_connect("$DBazes_serveris","$DBazes_vartotojas","$DBazes_slaptazodis");
    if (!
    $con)
      {
      die(
    'Nepavyko prisijunkti: ' mysql_error());
      }

    mysql_select_db("$DBazes_database"$con);

    $sql="INSERT INTO table(id, Komentarai, data)
    VALUES
    ('
    $_POST[id]','$_POST[Komentarai]','$_POST[data]')";

    if (!
    mysql_query($sql,$con))
      {
      die(
    'Klaida: ' mysql_error());
      }
    echo 
    "Irasyta"/// Recorded

    mysql_close($con)
    ?>

  5. #4
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    You're not calling the function mysql_query();, you're only associating a variable with it. Additionally, you need to have the 'id' as your PRIMARY_KEY on your MySQL system, and set it to AUTO_INCREMENT, as INTEGER.

    This will fix the query porblem for oyu:
    PHP Code:
    <? 
    $DBazes_serveris 
    "xxxxx.xxxxxxx.com";
    $DBazes_database "xxxxxx_xxxx";
    $DBazes_vartotojas "xxxxx_xxxx";
    $DBazes_slaptazodis "xxxxxxx";
    $DBazes_lentele "PHPMySQL";
    //////////////////////////////////////////// Prisijungimui 
    $login mysql_connect($DBazes_serveris$DBazes_vartotojas$DBazes_slaptazodis) or die ('Nepavyko prisijunkti');
    //////////////////////////////////////////// LENTELE
    @mysql_select_db($DBazes_lentele);
    $irasymas_i_db "INSERT INTO $DBazes_lentele VALUES ( '$id','$Komentarai', '$data') ";
    $rezultatas mysql_query($irasymas_i_db);
    mysql_close();

    echo 
    "Jusu komentaras:".$Komentarai;

    ?>
    HTH
    Yes it is Thank you man! You helped me not 1st time But.. Maybe can you write a code how to show info out of DB? To show DB info (posted info) in my php file? Cause i cant imagine how to do it

  6. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You can use mysql_fetch_array() in a while{} statement to show each record of db.

    Example:
    PHP Code:
    <?php

    $qq 
    mysql_query("SELECT * FROM $DBazes_lentele") or die(mysql_error());

    while(
    $r mysql_fetch_array$qq )) {
    echo 
    $r[id]."<br>";
    echo 
    $r[val]."<br>";
    echo 
    $r[val]."<br>";
    }

    ?>
    Ref:
    http://php.net/while
    http://php.net/mysql_fetch_array
    http://php.net/mysql_query

    Those should help you out.

    HTH
    - Josh

  7. The Following User Says Thank You to JShor For This Useful Post:

    Total_me (08-22-2009)

  8. #6
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by JShor View Post
    You can use mysql_fetch_array() in a while{} statement to show each record of db.

    Example:
    PHP Code:
    <?php

    $qq 
    mysql_query("SELECT * FROM $DBazes_lentele") or die(mysql_error());

    while(
    $r mysql_fetch_array$qq )) {
    echo 
    $r[id]."<br>";
    echo 
    $r[val]."<br>";
    echo 
    $r[val]."<br>";
    }

    ?>
    Ref:
    http://php.net/while
    http://php.net/mysql_fetch_array
    http://php.net/mysql_query

    Those should help you out.

    HTH

    Thank you again I added you to friends. You pro?

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
  •