Jump to content

Odd problem, blank row inserted after intended row...


Kristoff1875
Go to solution Solved by Kristoff1875,

Recommended Posts

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 by Kristoff1875
Link to comment
Share on other sites

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();
}
    
?>
Link to comment
Share on other sites

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 by Kristoff1875
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

+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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Kristoff1875
Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.