Jump to content

INSERT command shortcut


master82

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?
Link to comment
https://forums.phpfreaks.com/topic/15820-insert-command-shortcut/
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).
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]
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
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?
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 :-)

Archived

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

×
×
  • 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.