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

    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:

    //Sample Database Connection Syntax for PHP and MySQL.

    //Connect To Database
    $yourfield = "Email";

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


    # Check If Record Exists

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

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

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


    Thanks in advance!

  2. #2
    Join Date
    Sep 2009
    San Sebastian, Spain
    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:

    $sql = 'INSERT INTO $usertable (Name, Email, Group) VALUES ("Dianne", "", "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)
    Follow me on Twitter

  3. #3
    Join Date
    Nov 2011

    MySQL INSERT issue

    Thanks to! 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