SeanHarding Posted August 7, 2008 Share Posted August 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
Johntron Posted August 7, 2008 Share Posted August 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
SeanHarding Posted August 7, 2008 Author Share Posted August 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted August 7, 2008 Share Posted August 7, 2008 The syntax with VALUES() is my preference -- just make sure you list the columns, too. Quote Link to comment Share on other sites More sharing options...
SeanHarding Posted August 7, 2008 Author Share Posted August 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
Johntron Posted August 7, 2008 Share Posted August 7, 2008 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 ); Quote Link to comment Share on other sites More sharing options...
SeanHarding Posted August 7, 2008 Author Share Posted August 7, 2008 Ok I have multiple HTML fields check boxes, full text fields, dropdown menu's everything in one <form> field. All i need to know is the best way to put this information into my database. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 7, 2008 Share Posted August 7, 2008 All i need to know is the best way to put this information into my database. I feel like you're asking the PHP version of this question... 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.