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
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]
Link to comment
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).
Link to comment
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]
Link to comment
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
Link to comment
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
Link to comment
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?
Link to comment
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 :-)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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