INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 14, 2013 07:42AM

I have a problem that I need help with. I tried XAMPP and now have WAMP installed hoping the issue would be corrected with the latter - no dice. For years, I've had a development machine on XP albeit with an older version of PHP/MySQL/Apache. All that code was tried and true and it never let me down. Flash forward...

I have installed WAMP on my Windows 7 Pro 64 bit machine and queries always work fine (variety of queries) but with an input form I'm working with, 'occasionally' it inserts into the table just fine but more often than not, does not even though I'm not getting any errors anywhere. For all intents and purposes, it succeeded with the INSERT INTO until I go to view the table contents.

So I'm left to wonder about ram buffers, etc in my W7/64 machine but still have to think it might be a more basic configuration issue that's keeping me from happiness... :-)

Any ideas are really appreciated. I'm not sure if it's ports related, W7 Security, enable_pipes kind of stuff or what and hope that some other W7 users have encountered the problem.

Here's info on the DB & table:
DB/Table structure:
id = int, auto-increment, primary key
event = varchar 250
date = date
name = varchar 20

Engine InnoDB
Collation utf8_general-ci w/UTF-8 Unicode character set

Here's the INSERT INTO page code that receives variables from an input form and writes to the db:

<?php
require_once('connect_info.php');
$DBname = "events";
$table = "events_tbl";

// $id = $_POST['id'];
$event = $_POST['event'];
$date = $_POST['date'];
$name = $_POST['name'];
$year = $_POST['year'];
if (!$event || !$date || !$name || !$year)
{
echo 'You have not entered all the required fields for this data entry.<br />'
.'Please click the browser BACK button, complete the form and try again.';
exit;
}

$con = mysql_connect($DBhost,$DBuser,$DBpass);
if (!$con)
{
die("Connect Fail:" . mysql_error());
}
$db_select = mysql_select_db($DBname, $con);
if (!$db_select)
{
die ("DB Select fail:" . mysql_error());
}

$sqlquery = "INSERT INTO $table VALUES('$event', '$date', '$name', '$year')";
if ($results = mysql_query($sqlquery)) {
$event = stripslashes($event);
}
echo "<hr>$sqlquery<hr>"; // in temporarily for troubleshooting

mysql_close();
print "<center><table border=\"0\"
width=\"500\"><tr><td>";
print "<p><font face=\"verdana\" size=\"+0\"> <center>You
Just Entered This Information into the
Database<p><blockquote>";
print "Event: $event<p>Date: $date<p> Name: $name<p> Year: $year</blockquote></td></tr></table>
</CENTER>
</body>
</html>";
?>

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: stevenmartin99 (Moderator)
Date: January 14, 2013 07:51AM

Your table layout doesnt match your Insert statement

id = int, auto-increment, primary key 
event = varchar 250 
date = date 
name = varchar 20 

and

$sqlquery = "INSERT INTO $table VALUES('$event', '$date', '$name', '$year')";

you didnt mention a year column in the db. But most importantly and whats stopping the query is the id column

for an auto-increment column you need to have that in the Insert as a blank input.
$sqlquery = "INSERT INTO $table VALUES('', '$event', '$date', '$name', '$year')";

or 

$sqlquery = "INSERT INTO $table VALUES('','$event', '$date', '$name')";

will work for you depending if you have a year column or not in the table.

your not getting any error becuase you didnt ask for an error
$results = mysql_query($sqlquery)

you would need to use or die() etc

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]



Edited 6 time(s). Last edit at 01/14/2013 07:58AM by stevenmartin99.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 14, 2013 03:23PM

Good catch on the year and thanks for looking at it. year is a varchar(4). What you saw with the id has been corrected and now looks like this: $sqlquery = "INSERT INTO $table VALUES('$id', '$event', '$date', '$name', '$year')";

It was originally there and removed on a temp basis as I've tried to locate the issue. You probably noticed too that I had the // $id = $_POST['id']; remmed out in my original post which was temp while I removed the INSERT INTO portion of $id.

As I look at the echo of the query, id comes through from the first page fine and is inserted successfully as follows:
INSERT INTO events_tbl VALUES('', 'Chatted with John briefly to see how Sue was doing', '01/14/2013', 'Joe', '2013')

Also, when I wrote that the query hadn't failed, it wasn't in ref to error coding but thanks. What I meant was there had been no obvious failure as the queries have always produced fine (unlike the INSERT INTO). Thanks for your help... :-)

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 14, 2013 03:49PM

and the REALLY weird part of this... If I reduce the entry for 'event' back to 150 characters or below, the entry will likely be successful whereas if the entry is around the 250 level, it most likely will fail with no warning.

I might expect the entry to be truncated for anything exceeding the 250 mark for event but the problem is that it appears to go in, showing no error and echoing to the screen just fine. You just know it didn't make it when you run a query or visually look at the table contents. I just successfully made an entry using about a 100 character message for event but failed numerous times when it was around 250 for event. event=varchar(250)

AND THAT... is one thing that makes me wonder about running this pack on a W7/64 machine (memory buffers, etc). Then on the other hand, there's probably a lot of people who claim it works flawlessly on their W7/64 machines just as an older version of PHP/MySQL/Apache did on my old XP machine. ALTHOUGH, on that XP machine, I'm thinking the engine is ISAM where currently I'm using InnoDB

So frustrating...

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: stevenmartin99 (Moderator)
Date: January 14, 2013 04:53PM

If your using auto increment you cannot have the $id

Just use '',


Insert returns true if successful , so if the line doesn't insert you can catch that using the error logging capabilities of mysql

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 14, 2013 08:58PM

Thanks Steven. You think it's significant given the first page passes '' to $id ???

<input type="hidden" name="id" value="">


Also, can you give me some pointers on error logging via MySQL ??? I'm wondering if some configuration file needs to be tweaked in WAMP.


Thanks again...

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: stevenmartin99 (Moderator)
Date: January 14, 2013 09:01PM

no im not saying this..


im saying that in the sql insert, you cannot write '$id'

becasue you cannot set a value for something that is auto incrementing, also you cannot leave it out of the insert so you must use

$sqlquery = "INSERT INTO $table VALUES('', '$event', '$date', '$name', '$year')";

not

$sqlquery = "INSERT INTO $table VALUES('$id', '$event', '$date', '$name', '$year')";

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 14, 2013 09:50PM

So help me to understand better, even though the contents of the variable '$id' is in fact, '' - I shouldn't use it anyway. I only question it because in an echo of the query, I can see there was nothing there and that it was indeed ''.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: stevenmartin99 (Moderator)
Date: January 14, 2013 09:56PM

Get Teamviewer! And ill help

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 15, 2013 05:38AM

Thanks, I'll get Teamviewer. This whole thing has really sucked. I used to have complete confidence that what was entered into the form, made it to the database. That confidence has really waned now.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 15, 2013 05:43AM

This is the installer package I downloaded:
wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24-32b.exe

wonder if it would have made a difference had I installed the 64 bit version...

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 15, 2013 03:56PM

This is from my MySQL error log this morning. Anything glaring that should be corrected?

130115 6:39:03 [Note] Plugin 'FEDERATED' is disabled.
130115 6:39:03 InnoDB: The InnoDB memory heap is disabled
130115 6:39:03 InnoDB: Mutexes and rw_locks use Windows interlocked functions
130115 6:39:03 InnoDB: Compressed tables use zlib 1.2.3
130115 6:39:03 InnoDB: Initializing buffer pool, size = 128.0M
130115 6:39:03 InnoDB: Completed initialization of buffer pool
130115 6:39:03 InnoDB: highest supported file format is Barracuda.
130115 6:39:03 InnoDB: Waiting for the background threads to start
130115 6:39:04 InnoDB: 1.1.8 started; log sequence number 1762858
130115 6:39:05 [Note] Server hostname (bind-address): '(null)'; port: 3306
130115 6:39:05 [Note] - '(null)' resolves to '::';
130115 6:39:05 [Note] - '(null)' resolves to '0.0.0.0';
130115 6:39:05 [Note] Server socket created on IP: '0.0.0.0'.
130115 6:39:05 [Note] Event Scheduler: Loaded 0 events
130115 6:39:05 [Note] wampmysqld: ready for connections.
Version: '5.5.24-log' socket: '' port: 3306 MySQL Community Server (GPL)

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: RiggsFolly (---.as13285.net)
Date: January 15, 2013 04:58PM

Just a small point, well actually I dont think its that small.

Coding your INSERTS the way you have, your query is dependant upon the order that the fields were created in the table, if somebody moves the order of the fields on the database, because they think it looks nicer with year following date, your code will be really messed up.

If you code your query like this:

INSERT INTO $table (`event`, `date`, `name`, `year`) VALUES('$event', '$date', '$name', '$year')

You are specifying the field names and the data in the same order, it cannot go wrong even if somebody changes the creation order on the table.

You never need to specify the ID (autoincrement) fields as MySQL looks after creating those as only it knows what it should be.

If you need to see the id that is created there is a PHP command to ask

mysql_insert_id()


So for example:
$q = "INSERT INTO $table (`event`, `date`, `name`, `year`) VALUES($event, $date, $name, $year)";

$result = mysql_query($q);

if ( $result === TRUE ) {
    $created_id = mysql_insert_id($result);
} else {
    INSERT FAILED so do your error processing here
}

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 15, 2013 10:03PM

Thanks RiggsFolly - I tried "INSERT INTO $table (`event`, `date`, `name`, `year`) VALUES($event, $date, $name, $year)" and it worked the first time but not efforts after that. I'm thinking about uninstalling this 32bit version of WAMP and installing a 64 bit version to see if the issues go away. This mystery of sometimes it posts to the table and sometimes it doesn't is starting to make me think it might be more of a windows 64 thing than anything but I may be wrong in that thinking...


I was unable to get this to run as a copy and paste (but will revisit it and try to get it going ($q = "INSERT INTO $table (`event`, `date`, `name`, `year`) VALUES($event, $date, $name, $year)";

$result = mysql_query($q);

if ( $result === TRUE ) {
$created_id = mysql_insert_id($result);
} else {
INSERT FAILED so do your error processing here
}
)

Thanks again.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: RiggsFolly (---.as13285.net)
Date: January 15, 2013 10:42PM

Dont waste your time going to 64bit. That is definitely not you problem. The 64bit version is more likely to be missing somethign you might need as not all the PHP extensions have been converted to 64 bit.


Try this.

When you have your done this line ( I took the ticks off the field names they are not absolutely necessary )

$q = "INSERT INTO $table (event,date,name,year) VALUES($event, $date, $name, $year)";

echo out the $q like this

echo $q;

Then copy and paste the query that should now be visible on your browser into phpMyAdmin and run it there. If it works it works, if not it will give you an error message that might be more useful to you.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: stevenmartin99 (Moderator)
Date: January 16, 2013 12:19AM

Can you please stop this thread, ive explained and iv offerred help.,

THE PROBLEM IS THE ID.

YOU KEEP IGNORING WHAT I AM SAYING ABOUT THE ID.

you have to have it as i WORTE and you didnt do it, you changed what i wrote.

ill be deleting this thread in 24 hours, and this is not a wamp issue at all,

there is not problems with INSERT, people use it to insert millions of records a minute, there is no issue with it being unreliable.

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 16, 2013 03:07AM

stevenmartin99 Wrote:
-------------------------------------------------------
> Can you please stop this thread, ive explained and
> iv offerred help.,
>
> THE PROBLEM IS THE ID.
>
> YOU KEEP IGNORING WHAT I AM SAYING ABOUT THE ID.
>
> you have to have it as i WORTE and you didnt do
> it, you changed what i wrote.
>
> ill be deleting this thread in 24 hours, and this
> is not a wamp issue at all,
>
> there is not problems with INSERT, people use it
> to insert millions of records a minute, there is
> no issue with it being unreliable.


Sorry that you feel that way Steven. So far, I've tried every tip that I've seen posted (yours included) and nothing has seemed to solve my mystery on this. I also installed Teamviewer and emailed you last night at the addresses on your signature to let you know that I had done so. I guess you can delete the thread but I would hope you not because surely, someone else will have similar issues one day and it was my hope all along that if I found an answer, the records helps others.



Edited 1 time(s). Last edit at 01/16/2013 05:52AM by cover.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 16, 2013 03:30AM

Steven, how do we connect through Teamviewer ? I have it installed but don't know how to hook up.

Options: ReplyQuote
Re: INSERT INTO mystery - HELP !
Posted by: cover (---.nctv.com)
Date: January 16, 2013 05:49AM

Hey to close this out, after adding more error coding, I started to pick up on an error (mysql error 1064: SQL Syntax) that I was chasing and it repeatedly referred to an InnoDB engine. I changed the table to MyISAM and it works every time now. I've read where InnoDB is preferred but couldn't get the consistency as my INSERT INTO continued to error out (as discussed in this thread). After reading through my MySQL error log, I saw references to the InnoDB and went to the mysql my.ini file to UNcomment the recommended lines for InnoDB but couldn't get Apache to fire back up so undid that and changed my DB table engine to MyISAM.


Thanks again for your help and to others who replied too. grinning smiley

Over and out...



Edited 1 time(s). Last edit at 01/16/2013 05:51AM by cover.

Options: ReplyQuote


Sorry, only registered users may post in this forum.