If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > MySQL Table data will not INSERT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-29-11, 17:47
idaay idaay is offline
Registered User
 
Join Date: Nov 2011
Posts: 2
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!
Reply With Quote
  #2 (permalink)  
Old 11-30-11, 02:41
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
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
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 11-30-11, 16:00
idaay idaay is offline
Registered User
 
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!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On