simple adding of data to a table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tomhughes_86@hotmail.com

    #1

    simple adding of data to a table

    Hi,
    I'm basically creating a system which add records to a pre made SQL
    database built in SQLyog.
    So far i'va had no problems actually connecting to the database and
    displaying resulst but I just cannot make it add records. I know its
    connected to the database as a I made a mistake with connection
    details on purpose to see if it recognised it.
    I just add data into a text field, click submit and the page goes to
    a
    new, blank page with no changes to the table when I check in SQLyog.
    Any help would be much appreciated, as I am very new to PHP.
    Thanks Tom.

    Here is the code I have tried (simple add before I code the proper
    system):


    HTML page
    <form action="script. php" method="post">
    Test: <input type="text" name="test2"><b r>
    <input type="Submit">
    </form>


    and the PHP script to add data:
    <?
    $host="localhos t"
    $username="root ";
    $database="test ";


    $test2=$_POST['test2'];


    mysql_pconnect( $host,$username );
    @mysql_select_d b($database) or die( "Unable to select database");


    $query = "INSERT INTO test2 VALUES ('','$test2')";
    mysql_query($qu ery);


    mysql_close();
    ?>

  • Rik

    #2
    Re: simple adding of data to a table

    <tomhughes_86@h otmail.comwrote :
    So far i'va had no problems actually connecting to the database and
    displaying resulst but I just cannot make it add records.
    $test2=$_POST['test2'];
    $query = "INSERT INTO test2 VALUES ('','$test2')";
    mysql_query($qu ery);
    Well, let's check why mysql doesn't like it:


    mysql_query($qu ery) or die("Error in query:\n$query\ nMySQL
    sais:\n".mysql_ error());

    --
    Rik Wasmus

    Comment

    • Michael Fesser

      #3
      Re: simple adding of data to a table

      ..oO(tomhughes_ 86@hotmail.com)
      >and the PHP script to add data:
      ><?
      <?php
      >$host="localho st"
      >$username="roo t";
      root?! You should create and use a normal user account.
      >$database="tes t";
      >
      >
      >$test2=$_POS T['test2'];
      Check with isset() if $_POST['test2'] exists.
      >mysql_pconnect ($host,$usernam e);
      mysql_connect(. ..);
      >@mysql_select_ db($database) or die( "Unable to select database");
      >
      >
      >$query = "INSERT INTO test2 VALUES ('','$test2')";
      >mysql_query($q uery);
      No error handling. What does mysql_query() return? Or mysql_error()?
      Is error_reporting set to E_ALL?

      Micha

      Comment

      Working...