Jump to content


Photo

INSERT command shortcut


  • Please log in to reply
18 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 05:38 PM

$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?


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 July 2006 - 05:41 PM

That will work yes. The general syntax is this
INSERT INTO tbl_name ([col1, col2....]) VALUES ([Value1, Value2...])


#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 08:21 PM

How exactly should I change the code I have - see below:

$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");


#4 cdc5205

cdc5205
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 July 2006 - 08:34 PM

$sql = "INSERT INTO answers (id) VALUES ('$newid')";


#5 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 08:41 PM

Sorry, that didn't work

#6 cdc5205

cdc5205
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 July 2006 - 08:43 PM

show all the code you have, you are making a connection to the db right at $conn?

#7 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 08:51 PM

<?php
session_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");
}
?>
Basic HTML under this


Yes, 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).

#8 cdc5205

cdc5205
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 July 2006 - 08:54 PM

are you getting an error at the die() statement? try not making $result a var.
<?php
session_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());
}
?>


#9 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 09:00 PM

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 :(

#10 cdc5205

cdc5205
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 July 2006 - 09:01 PM

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");

#11 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 09:05 PM

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

#12 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 09:06 PM

Can you show me a piece of your code where you have done it?

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 09:08 PM

You can also use the alternative insert syntax:
<?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());
?>

Ken

#14 cdc5205

cdc5205
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 July 2006 - 09:15 PM

<?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";
?>

i just typed this and tested it and it works

#15 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 09:30 PM

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

#16 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 09:31 PM

Please post the output you recieved. That can usually point to the error.

Ken

#17 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 09:52 PM

Here is what I get Ken

An error has occured with the query: INSERT INTO answers SET id = '162515'
Please contact admin stating error @ point A1
Access denied for user 'vu4543'@'localhost' (using password: NO)

By any chance, would the last line indicate a problem with the connection or is that what always appears the way you do it?

#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 09:55 PM

Yes, that error says you don't have permission to access the database, which is why nothing works. Check your username and password.

Ken

#19 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 27 July 2006 - 09:58 PM

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 :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users