Jump to content

Recommended Posts

I have this small snippet of asp code, which I need to replicate in php.

set objRS=Server.CreateObject("ADODB.recordset")
objRS.Open "tbl_details", Con, 2,3
objRS.AddNew
objRS("stu_id") = stuid
objRS("mood_id")=moodid
objRS("date_created")=now()
objRS.Update

Response.Redirect "default.asp?stu_id=" & stuid & "&mood_id=" & moodid

From looking at it I think it selects a table(tbl_details), creates a new record using the variables stuid & moodid into the columns stu_id & mood_id.

I have tried this but no luck:-

$sql = "insert into tbl_details (stu_id, mood_id, date_created)
          values( \"$stuid\", \"$moodid\", current_date());

$rs = mysql_query( $sql, $conn )
          or die( "Query Error" );

if($rs)
{
header( "Location:my_details.php" ); exit();
}
?>
Link to comment
https://forums.phpfreaks.com/topic/26677-inserts-solved/
Share on other sites

I've just tried that too and its still giving me "Query Error"

I have even just tried this to test it:-

[code]
<?php

$sql = "INSERT INTO tbl_details (stu_id, mood_id)
          VALUES ('Test', 'Test')";

$rs = mysql_query($sql,$conn)
                or die( "Query Error" );

if($rs)
{
header( "Location: my_details.php" ); exit;
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122057
Share on other sites

Yes should be heres whole code:-

[code]<?php

#connect to db
$conn = @mysql_connect(  "localhost", "username", "password" )
      or die( "Err:conn");
#select db
$rs = @mysql_select_db( "dbtest", $conn)
or die( "ERR:Db");

#create query
$sql = "insert into tbl_details (stu_id , mood_id , date_created )
values ( 'Test', 'Test', 'Test')";

$rs = mysql_query( $sql, $conn )
or die( "Query Error" );

if($rs)
{
header( "Location: my_details.php" ); exit;
}
?>
[/code]

The column date_created is just a varchar at the moment.
Link to comment
https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122073
Share on other sites

1.) Neither are auto increment beacuse they are values people can choose e.g username and password

2.) All becomes clear!! (Query Error:Field 'forename' doesn't have a default value).
There are other columns in the table which I do not want to insert into so I guess I should be doing:-

$sql = "insert into tbl_details (stu_id , forename, column_name, mood_id , date_created )
values ( 'test', '', '',  'test', 'test')";

I'll give it a go

Cheers
Link to comment
https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122081
Share on other sites

Yes, I believe that it's implying that forename is set to NOT NULL, meaning it can't be left blank.  If it has a default value, then you can get away without including it in your INSERT code, as the default will be used, but if it doesn't you'll have to provide something.

Incidentally, why are you not wanting to put something into a column that is required?

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122084
Share on other sites

I have set them to null(for now). Originally it didn't matter as I wanted those other columns to be filled out from another page and being not null meant fields were not left blank. (however I can get round this by adding error checking to the form). I will decide which is best later.

I have the column date-created using type datetime. It doesnt like ".time()."
Whats the correct syntax?

Many Thanks

Link to comment
https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122095
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.