Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Unanswered: MySQL Table data will not INSERT

    I am attempting to create a MySQL database and table for the first time.
    The test code I wrote (PHP 5) retrieves data that I manually inserted into my table and displays it. However, when attempting to INSERT data into that same table from my web page, the code SEEMS to work, but the table is not updated.
    Here's the code:

    <?php
    //Sample Database Connection Syntax for PHP and MySQL.

    //Connect To Database
    $hostname="mysql.secureserver.net";
    $username="xxxxxx";
    $password="xxxxxxxx";
    $dbname="idaay";
    $usertable="NewsList";
    $yourfield = "Email";

    mysql_connect($hostname,$username, $password)
    or die ("<html><script language='JavaScript'>
    alert('Unable to connect to database! Please try again later.'),
    history.go(-1)</script></html>");

    mysql_select_db($dbname);

    # Check If Record Exists

    mysql_query('INSERT INTO $usertable (Name, Email, Group)
    VALUES ("Dianne", "drt@drt.com", "Newsletter")');

    $query = "SELECT * FROM $usertable";
    $result = mysql_query($query);

    if($result)
    {
    while($row = mysql_fetch_array($result))
    {
    $name = $row["$yourfield"];
    echo "Email: ".$name."<br>";
    }
    }

    ?>

    Thanks in advance!

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    You should really be testing the result from the mysql_query function. Also my preference when developing solutions like this is to use the practice of putting the SQL statement into a variable as follows:

    Code:
    $sql = 'INSERT INTO $usertable (Name, Email, Group) VALUES ("Dianne", "drt@drt.com", "Newsletter")';
    $result = mysql_query($sql);
    if(!$result) {
      echo "ERROR: Unable to process SQL: $sql: " . mysql_error() . "\n";
    }
    // As there is no results returned there is no need to free up the memory used
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Nov 2011
    Posts
    2

    MySQL INSERT issue

    Thanks to it-iss.com! The mysql_error() was a big help. Problem was the Group field. apparently that is a reserved name. When I changed it the INSERT worked.

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •