Jump to content

Archived

This topic is now archived and is closed to further replies.

master82

INSERT command shortcut

Recommended Posts

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

Share this post


Link to post
Share on other sites
That will work yes. The general syntax is this
[code]INSERT INTO tbl_name ([col1, col2....]) VALUES ([Value1, Value2...])[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
show all the code you have, you are making a connection to the db right at $conn?

Share this post


Link to post
Share on other sites
[code]
<?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");
}
?>
[/code]
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).

Share this post


Link to post
Share on other sites
are you getting an error at the die() statement? try not making $result a var.
[code]<?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());
}
?>[/code]

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Please post the output you recieved. That can usually point to the error.

Ken

Share this post


Link to post
Share on other sites
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 A1
Access 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?

Share this post


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

Ken

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites

×

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.