Kristoff1875 Posted November 25, 2013 Share Posted November 25, 2013 (edited) Hi guys, having a strange issue at the moment whereby my insert is being followed up by a completely blank row other than the auto inc of the ID, which is how i know it's being inserted after. Here's the code: <? session_start(); include("connect.php"); $_SESSION['Email'] = mysql_real_escape_string($_POST["Email"]); $_SESSION['Name'] = mysql_real_escape_string($_POST["Name"]); $_SESSION['Telephone'] = mysql_real_escape_string($_POST["Telephone"]); mysql_query("INSERT INTO User (Name, Telephone, Email, FirstReg, LastLogin) VALUES ('{$_SESSION['Name']}','{$_SESSION['Telephone']}','{$_SESSION['Email']}',NOW(),NOW())") or die(mysql_error()); ?> The form before this is as follows: <form id="addnew" action="page1.php" method="post"> <label for="Name" class="label large">Your Name</label> <input name="Name" type="text" /> <label for="Telephone" class="label large">Telephone</label> <input name="Telephone" type="text" /> <label for="Email" class="label large">Your Email</label> <input name="Email" type="text" /> <input type="submit" /> </form> And this page with the form also includes a session open, destroy and close at the top to make sure any old details are cleared from the session. Anybody got any ideas why it's inserting a rogue row?! Thanks in advance! Edit: To rule out the session being an issue i've used the following: <? session_start(); include("connect.php"); $Email = mysql_real_escape_string($_POST["Email"]); $Name = mysql_real_escape_string($_POST["Name"]); $Telephone = mysql_real_escape_string($_POST["Telephone"]); mysql_query("INSERT INTO User (Name, Telephone, Email, FirstReg, LastLogin) VALUES ('$Name','$Telephone','$Email',NOW(),NOW())") or die(mysql_error()); ?> And this also comes up with the same issue. Second Edit: <? session_start(); include("connect.php"); $Email = mysql_real_escape_string($_POST["Email"]); $Name = mysql_real_escape_string($_POST["Name"]); $Telephone = mysql_real_escape_string($_POST["Telephone"]); mysql_query("INSERT INTO User (Name, Telephone, FirstReg, LastLogin) VALUES ('$Name','$Telephone',NOW(),NOW())") or die(mysql_error()); ?> Results in just one row... as does: <? session_start(); include("connect.php"); $Email = mysql_real_escape_string($_POST["Email"]); $Name = mysql_real_escape_string($_POST["Name"]); $Telephone = mysql_real_escape_string($_POST["Telephone"]); mysql_query("INSERT INTO User (Name, Email, FirstReg, LastLogin) VALUES ('$Name','$Email',NOW(),NOW())") or die(mysql_error()); ?> and <? session_start(); include("connect.php"); $Email = mysql_real_escape_string($_POST["Email"]); $Name = mysql_real_escape_string($_POST["Name"]); $Telephone = mysql_real_escape_string($_POST["Telephone"]); mysql_query("INSERT INTO User (Telephone, Email, FirstReg, LastLogin) VALUES ('$Telephone','$Email',NOW(),NOW())") or die(mysql_error()); ?> But put all 3 (Name, Telephone and Email) in and it inserts an extra row. Edited November 25, 2013 by Kristoff1875 Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 25, 2013 Share Posted November 25, 2013 Would this help debug? <? session_start(); include("connect.php"); $_SESSION['Email'] = mysql_real_escape_string($_POST["Email"]); $_SESSION['Name'] = mysql_real_escape_string($_POST["Name"]); $_SESSION['Telephone'] = mysql_real_escape_string($_POST["Telephone"]); //note we'll be telling it to set the ID ... change "ID" as necessary for your database. Using the null string will suffice. $query = "INSERT INTO User (ID,Name, Telephone, Email, FirstReg, LastLogin) VALUES ('','{$_SESSION['Name']}','{$_SESSION['Telephone']}','{$_SESSION['Email']}',NOW(),NOW())"; $result = mysql_query($query); if (!$result) { //your error code } else { echo "Insert successful. Last insert id: ".mysql_insert_id(); } ?> Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 25, 2013 Share Posted November 25, 2013 Having read your edits, I think we need to know your table structure.Also, please note that mysql_* is deprecated LONG ago, and your code is in danger of becoming hopelessly antiquated in less than 5 years. You might take a look at mysqli_* functions.... Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) Is it much work to move to mysqli? I will look in to it, thanks. After trying: <? session_start(); include("connect.php"); $_SESSION['Email'] = mysql_real_escape_string($_POST["Email"]); $_SESSION['Name'] = mysql_real_escape_string($_POST["Name"]); $_SESSION['Telephone'] = mysql_real_escape_string($_POST["Telephone"]); //note we'll be telling it to set the ID ... change "ID" as necessary for your database. Using the null string will suffice. $query = "INSERT INTO User (UserID, Name, Telephone, Email, FirstReg, LastLogin) VALUES ('NULL','{$_SESSION['Name']}','{$_SESSION['Telephone']}','{$_SESSION['Email']}',NOW(),NOW())"; $result = mysql_query($query); if (!$result) { //your error code } else { echo "Insert successful. Last insert id: ".mysql_insert_id(); } ?> Resulted in: Insert successful. Last insert id: 15724 And only inserting one row and no blank one... Table structure: CREATE TABLE `User` ( `UserID` int(11) NOT NULL AUTO_INCREMENT, `Email` tinytext NOT NULL, `Name` tinytext NOT NULL, `Address1` text NOT NULL, `Address2` text NOT NULL, `Address3` text NOT NULL, `Town` text NOT NULL, `County` text NOT NULL, `Postcode` text NOT NULL, `FirstReg` datetime NOT NULL, `LastLogin` datetime NOT NULL, `UseBizAddress` int(11) NOT NULL, `Telephone` text NOT NULL, `ContactBy` varchar(11) NOT NULL, `Agree` int(11) NOT NULL Edited November 25, 2013 by Kristoff1875 Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 25, 2013 Share Posted November 25, 2013 Well, do you think you have it fixed, then? I always call the auto_increment field in my inserts, using '' as I showed. As for moving to mysqli_ --- I didn't find it too hard. Basics: 1. Mysqli_ requires the connection passed to it for most calls: mysqli_query($connection,$sql); I believe the reason is to allow for multiple connections to different hosts. 2. Some of the "functions" are gone because Mysqli already takes care of them. I still find myself typing "$result->num_rows();" --- but I don't need to; Mysqli has already returned that number, and it's now a variable (and NOT a function): 'echo $result->num_rows." were returned.";' 3. There is no mysqli_result function (IMO, this kind of bites --- I used it often for grabbing one result from a query). I found someone had written one, and I use it when I can find no better option. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 25, 2013 Share Posted November 25, 2013 3. There is no mysqli_result function (IMO, this kind of bites --- I used it often for grabbing one result from a query). I found someone had written one, and I use it when I can find no better option. +1 I liked to use that after just doing a SELECT COUNT() query - ie a single field, single row result set. Kristoff - I see you aren't checking if any data was actually posted. Is it the absence of data that's causing your blanks? Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 Apologies, I spoke too soon... It's still inserting a blank row after, but no error. Thanks for the Mysqli advice, I will definitely look in to it. Cheers Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 +1 I liked to use that after just doing a SELECT COUNT() query - ie a single field, single row result set. Kristoff - I see you aren't checking if any data was actually posted. Is it the absence of data that's causing your blanks? I don't believe so, the data is definitely being posted and the row is being populated, but it then goes on to create a second row that has no data in it. Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 25, 2013 Share Posted November 25, 2013 Weird bug, then. Try converting your insert to not use curly braces, maybe? There aren't any semicolons in the input, are there? I guess you might try adding one in the *right* place. Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 Sorry, how do you mean adding semicolons in the right place? I've taken everything out of the code, and using this: <? include("connect.php"); mysql_query("INSERT INTO User (UserID, Name, Telephone, Email, FirstReg, LastLogin) VALUES ('NULL','Name','Telephone','Email',NOW(),NOW())") or die(mysql_error()); ?> Results in 2 rows being inserted, although both contain the same data, just different ID's. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 25, 2013 Share Posted November 25, 2013 Seems like you are going to that page twice somehow Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 Using this: <? include("connect.php"); $query = "INSERT INTO User (UserID, Name, Telephone, Email, FirstReg, LastLogin) VALUES ('NULL','Name','Telephone','Email',NOW(),NOW())"; $result = mysql_query($query); if (!$result) { echo "error"; } else { echo "Insert successful. Last insert id: ".mysql_insert_id(); } ?> I have just had: Insert successful. Last insert id: 15759 However it has also inserted id 15759 Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 Seems like you are going to that page twice somehow It does seem like that, however with so little code, I don't understand how? Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 25, 2013 Share Posted November 25, 2013 I mean: //note there are TWO semicolons below; one is for SQL... $sql = "insert into mysql_conventions (id,name,description) values ('','Semicolons','SQL statements should end with a semicolon.'); "; $query = mysqli_query($some_server,$sql); At this point, we're kind of grasping at straws. You might double-check Barand's question; perhaps the page IS being submitted more than once... Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 Ok, i'm not sure why I didn't test it before, but I just had a thought that maybe it was chrome causing an issue... 3 attempts on Safari and it's only inserted 1 row each time. Is it really possible that this could be an issue with chrome? Do you guys have chrome and could you test the page for me if I sent you a direct link?? Cheers Quote Link to comment Share on other sites More sharing options...
Kristoff1875 Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) I mean: //note there are TWO semicolons below; one is for SQL... $sql = "insert into mysql_conventions (id,name,description) values ('','Semicolons','SQL statements should end with a semicolon.'); "; $query = mysqli_query($some_server,$sql); At this point, we're kind of grasping at straws. You might double-check Barand's question; perhaps the page IS being submitted more than once... I'm literally not even submitting the page anymore, simply loading up a page and having the insert on that page. Barand's question made me consider the browser, and it's working on safari but not chrome. Not sure whether this may be an addon causing it? Edit: Have just tested incognito in chrome, and that only inserts one... which suggests to me that it's an addon! Edited November 25, 2013 by Kristoff1875 Quote Link to comment Share on other sites More sharing options...
Solution Kristoff1875 Posted November 25, 2013 Author Solution Share Posted November 25, 2013 Right. I've just turned off all of my add-ons, and guess what, it's not inserting the extra row... The only issue is, I've turned them all back on, and it's still not inserting the extra row!! Frustrating i've wasted so much time on it for nothing! Tip to everyone who may have this same issue: DISABLE ALL OF YOUR EXTENSIONS, TRY INCOGNITO OR TRY ANOTHER BROWSER BEFORE YOU WASTE TOO MUCH TIME ON SOMETHING THAT MAY NOT BE AN ISSUE WITH YOUR CODE!!!!! Quote Link to comment Share on other sites More sharing options...
Barand Posted November 25, 2013 Share Posted November 25, 2013 'SQL statements should end with a semicolon. Yes when calling within phpmyadmin etc or from the mysql command line but not when calling a single query using mysqli_query() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.