Jump to content

Adding empty rows on INSERT


a2bardeals

Recommended Posts

I have a few different pages on my site with a basic set up of:

Page_1.php is a normal html form with the POST method and the action set to Page_2.php a couple hidden variables mostly user defined text inputs or select fields. On submit it sends data to Page_2.php which updates a MySQL database using:

[code]$con = mysql_connect("localhost","root", "*******");


mysql_select_db("database", $con);

$sql="INSERT INTO addcampus
(school, city, state, email, send)
VALUES
('$_POST[school]','$_POST[city]','$_POST[state]','$_POST[email]','$_POST[send]')";


if (!mysql_query($sql,$con)) { die('ErrorTran: ' . mysql_error()); }
[/code]


the database table is also set with an ID column that auto increments and is the primary index. Often my database has been inserting one or multiple empty rows with just an ID. So basically the user data is there but where the user data row has an id of 10 the empties are 11 12 13 and so on. At one point i had a table with 1400 rows and only 20 of them actually had information in them. This is very frustrating for maintenance! Anyone have any ideas let me know. Thanks!
Link to comment
Share on other sites

you could always check the posts before entering it into the database..
cause i guess this is what's happening, someone is just sending an empty form..

use something like this for each post:

[code]if(!isset($_POST["school"]))
  $empty = TRUE;[/code]

or loop through the posts in some other way.

then use:
[code]
if(!$empty) {
$school = mysql_escape_string($_POST["school"]) // for every post

//and then the sql-query with $school instead of $_POST["school"] etc.

}[/code]


and it should be $_POST["school"] or $_POST['school'] etc.
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.