Round Posted November 9, 2006 Share Posted November 9, 2006 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,3objRS.AddNewobjRS("stu_id") = stuidobjRS("mood_id")=moodidobjRS("date_created")=now()objRS.UpdateResponse.Redirect "default.asp?stu_id=" & stuid & "&mood_id=" & moodidFrom 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/ Share on other sites More sharing options...
Orio Posted November 9, 2006 Share Posted November 9, 2006 [code]<?php$sql = "INSERT INTO tbl_details (stu_id, mood_id, date_created) VALUES ('$stuid', '$moodid', ".time().")";$rs = mysql_query($sql,$conn) or die( "Query Error" );if($rs){header( "Location: my_details.php" ); exit;}?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122040 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122057 Share on other sites More sharing options...
Orio Posted November 9, 2006 Share Posted November 9, 2006 Are you connected to the database?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122064 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122073 Share on other sites More sharing options...
Orio Posted November 9, 2006 Share Posted November 9, 2006 1) Are you sure stu_id or mood_id (or both) are not auto_increment?2) For better error messaging (for debugging only), change the part where the query runs to:$rs = mysql_query($sql, $conn) or die( "Query Error:<br>".mysql_error());Orio. Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122075 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 1.) Neither are auto increment beacuse they are values people can choose e.g username and password2.) 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 Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122081 Share on other sites More sharing options...
HuggieBear Posted November 9, 2006 Share Posted November 9, 2006 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?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122084 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122095 Share on other sites More sharing options...
HuggieBear Posted November 9, 2006 Share Posted November 9, 2006 I don't know why Orio suggested the change... Just use the sql function [color=green]now()[/color];RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122098 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 Sorted Thanks,You both have been a great help.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/26677-inserts-solved/#findComment-122106 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.