master82 Posted July 27, 2006 Share Posted July 27, 2006 $sql = "INSERT INTO table (field1, field2, field3 etc...) VALUES ('value1','value2','value3' etc...)";That is how I normally do an INSERT, however, the current Insert I am writing is inserting into a table with too many fields for me to actually write, yet I only need to insert a value into one field and the rest can be populated from the databases default settings!So does anyone know how I can do an insert into 1 field?does this work:$sql = "INSERT INTO table (field2) VALUES ('value2')";or do I need to list the whole fieldlist and values? Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/ Share on other sites More sharing options...
wildteen88 Posted July 27, 2006 Share Posted July 27, 2006 That will work yes. The general syntax is this[code]INSERT INTO tbl_name ([col1, col2....]) VALUES ([Value1, Value2...])[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64737 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 How exactly should I change the code I have - see below:[code]$sql = mysql_query("INSERT INTO answers (id) VALUES ('$newid')");$result = mysql_query($sql,$conn) or die("An error has occured - Please contact admin, stating error @ 1a");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64807 Share on other sites More sharing options...
cdc5205 Posted July 27, 2006 Share Posted July 27, 2006 [code]$sql = "INSERT INTO answers (id) VALUES ('$newid')";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64810 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 Sorry, that didn't work Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64814 Share on other sites More sharing options...
cdc5205 Posted July 27, 2006 Share Posted July 27, 2006 show all the code you have, you are making a connection to the db right at $conn? Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64815 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 [code]<?phpsession_start();if(isset($_SESSION['random'])) {//do nothing}else{$newid = rand(1,9999999);$_SESSION['random'] = $newid;include("../connect.php");$sql = "INSERT INTO answers (id) VALUES ('$newid')";$result = mysql_query($sql,$conn) or die("An error has occured - Please contavt admin stating error @ point A1");}?>[/code]Basic HTML under thisYes, the connection is working fine though, when i run the script it stops with the output "an error has occured - pl..."All I need is to insert (add a new row in the database table) by simply only inputing a single value (id) - although there are about 100 other colomns, but I really dont want to writing them all out and assigning them values too during the insert statement (if its blank the database inserts the default i have specified). Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64818 Share on other sites More sharing options...
cdc5205 Posted July 27, 2006 Share Posted July 27, 2006 are you getting an error at the die() statement? try not making $result a var. [code]<?phpsession_start();if(isset($_SESSION['random'])) {//do nothing}else{$newid = rand(1,9999999);$_SESSION['random'] = $newid;include("../connect.php");$sql = "INSERT INTO answers (id) VALUES ('$newid')";mysql_query($sql) or die("An error has occured - Please contavt admin stating error @ point A1" . mysql_error());}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64819 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 Still got the "An error has occured - please contact admin..."Its looking increasingly like I will have to do this the hard and long way :( Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64821 Share on other sites More sharing options...
cdc5205 Posted July 27, 2006 Share Posted July 27, 2006 no you shouldn't have to, id o it all the time, did you remove the $conn var from the mysql_query and you are selecting a database in connect.php? mysql_select_db("mydb"); Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64823 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 Yes I removed the $conn var, and I am selecting the correct database (and host, user, p/w) - double checked just, along with the table name and fieldname.I think the computer is going out the window in a minute! lol Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64826 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 Can you show me a piece of your code where you have done it? Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64828 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 You can also use the alternative insert syntax:[code]<?php$sql = "INSERT INTO answers SET id = '$newid'";mysql_query($sql) or die("An error has occured with the query: $sql<br>Please contact admin stating error @ point A1<br>" . mysql_error());?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64830 Share on other sites More sharing options...
cdc5205 Posted July 27, 2006 Share Posted July 27, 2006 [code]<?php mysql_connect("localhost","un","pw");mysql_select_db("dbname");$bob="Bob";mysql_query("INSERT INTO users(username) VALUE ('$bob')") or die("query" .mysql_error());echo "done";?>[/code]i just typed this and tested it and it works Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64836 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 Thanks for that Ken, but it only gave me the same output - but in a nicer way.Think I'll sleep on it - will probably be something simple, or I need to empty my cache and they will work lol Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64844 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 Please post the output you recieved. That can usually point to the error.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64845 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 Here is what I get Ken[code]An error has occured with the query: INSERT INTO answers SET id = '162515'Please contact admin stating error @ point A1Access denied for user 'vu4543'@'localhost' (using password: NO)[/code]By any chance, would the last line indicate a problem with the connection or is that what always appears the way you do it? Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64852 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 Yes, that error says you don't have permission to access the database, which is why nothing works. Check your username and password.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64853 Share on other sites More sharing options...
master82 Posted July 27, 2006 Author Share Posted July 27, 2006 I have and they are correct - the host has moved all the SQLK databases around - bet they changed the host name again without telling anyone!!!!!Strange thing is, if I run my connect.php where the information is stored, it runns perfect without the die() message I added to check if it worked.I'll get onto the host - thanks for your time :-) Quote Link to comment https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/#findComment-64855 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.