Jump to content

QUERY Help


SeanHarding

Recommended Posts

Im looking for the bast way to insert information into my database. (using php ofc).

 

$q = "INSERT INTO db_name ('id', 'name', 'email', 'time') VALUES (NULL, '$name', '$email', '$time')";
$r = mysqli_query($dbconnect, $q);
if (mysqli_affected_rows($dbconnect) == 1) {
   echo 'Cool';
} else {
   echo 'Naff';
}

or

 

$q = "INSERT INTO db_name VALUES (NULL, '$name', '$email', '$time')";
if(mysqli_query($dbconnect, $q) == 1) {
  echo 'Cool';
} else {
  echo 'Naff';
}

 

Or are there others out there that work equaly aswell for accumulating members to a website?

Link to comment
Share on other sites

I like the first one, because it's clear what fields you're updating. I doubt you need to include 'id' in your query. It probably auto increments itself. There's one other way to do inserts:

INSERT INTO db_name set name = '$name', email = '$email', time = '$time';

 

Also, I'd be sure you use the date/time datatypes that MySQL offers, because they make searching by date easier later on.

Link to comment
Share on other sites

I like the first one, because it's clear what fields you're updating. I doubt you need to include 'id' in your query. It probably auto increments itself. There's one other way to do inserts:

INSERT INTO db_name set name = '$name', email = '$email', time = '$time';

 

Also, I'd be sure you use the date/time datatypes that MySQL offers, because they make searching by date easier later on.

 

Thanks for that, but my current database is alot bigger than that, including addresses, selection boxes ect. Basicaly I only look at the database to see if any one new has joined so Auto Incriment is perfect for me, I have made some complex searches for users who have to use the database using things like IN BOOLEAN MODE (that was hard to find out.)

 

But yer im just basicaly looking for the best way to insert information  ;D

Link to comment
Share on other sites

Im guessing that you can input information into a database in a few different ways with the same results.

 

So how about i have alot of information comming from a HTML form (addresses and a few random questions).

 

What would be the best way to insert alot of information into a database, or would it have no effect?

Link to comment
Share on other sites

If the name attributes in your form matches up with your DB's field name, you could do something like this . . .

 

unset( $_POST[ 'submit'], $_POST[ 'agree'] ); // Just for demonstration. Be sure you remove any fields not going in the DB

$q = 'insert into `table` set ';

while ( list( $field, $value ) = each( $_POST ) ) {
   $q .= $field . ' = "' . mysql_real_escape_string( $value ) . '", ';
}
$q = rtrim( ', ');
mysql_query( $q );

 

Of course, you could wrap all of this in a function, and then just do something like: saveCleanedForm( $_POST );

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.