Go Back   Webmaster Malaysia Forum » Website Design & Development » Website Programming

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 20-03-2007, 12:24 PM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
Data xleh masuk dlm table..

nie code yg saya gune utk insert data dlm table.. tp xde 1 data pun yg msuk..
PHP Code:
<?php 
$host
'localhost';
$username'root';
$password'';

$connect =mysql_connect($host$username$password) or
die (
"could not connect to MySQL sever in localhost");
$db="care_pair";
mysql_select_db($db$connect) or
die (
"could not select database");


function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}

$editFormAction $HTTP_SERVER_VARS['PHP_SELF'];
if (isset(
$HTTP_SERVER_VARS['QUERY_STRING'])) 
{
  
$editFormAction .= "?" $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
{
  
$insertSQL sprintf("INSERT INTO Report(Report_No, Name_of_Vessel, Report_Time,
                       Date_of_Accident, Time_of_Accident, IP_Name, IP_Surname, IP_DOB,
                       IP_Nationality, IP_Sex, IP_Address, IP_Passport) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
,                     
                       
GetSQLValueString($_POST['Report_No'], "text"),
                       
GetSQLValueString($_POST['Name_of_Vessel'], "text"),
                       
GetSQLValueString($_POST['Report_Time'], "text"),
                       
GetSQLValueString($_POST['Date_of_Accident'], "text"),
                       
GetSQLValueString($_POST['Time_of_Accident'], "text"),
                       
GetSQLValueString($_POST['IP_Name'], "text"),
                       
GetSQLValueString($_POST['IP_Surname'], "text"),
                       
GetSQLValueString($_POST['IP_DOB'], "text"),
                       
GetSQLValueString($_POST['IP_Nationality'], "text"),
                       
GetSQLValueString($_POST['IP_Sex'], "text"),
                       
GetSQLValueString($_POST['IP_Address'], "text"),
                       
GetSQLValueString($_POST['IP_Passport'], "text"));

  
$Result1 mysql_query($insertSQL) or die( "There was an error running '$query' " mysql_error());
?>
}
saya gune code yg same utk database yg berlainan (bawah), tp data leh masuk..
ape silapnyer utk dtabase care_pair(atas)??

ni code utk database yg lain, dgn code yg same:
PHP Code:
<?php
$host
'localhost';
$username'root';
$password'';

$connect =mysql_connect($host$username$password) or
die (
"could not connect to MySQL sever in localhost");
$db="Idora_Laundry_System";
mysql_select_db($db$connect) or
die (
"could not select database");

function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}

$editFormAction $HTTP_SERVER_VARS['PHP_SELF'];
if (isset(
$HTTP_SERVER_VARS['QUERY_STRING'])) {
  
$editFormAction .= "?" $HTTP_SERVER_VARS['QUERY_STRING'];
}

if ((isset(
$HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form2")) {
  
$insertSQL sprintf("INSERT INTO Supplier (Supp_Id, Supp_Name, Supp_Add, Supp_Phone) VALUES (%s, %s, %s, %s)",
                       
GetSQLValueString($HTTP_POST_VARS['Supp_Id'], "text"),
                       
GetSQLValueString($HTTP_POST_VARS['Supp_Name'], "text"),
                       
GetSQLValueString($HTTP_POST_VARS['Supp_Add'], "text"),
                       
GetSQLValueString($HTTP_POST_VARS['Supp_Phone'], "text"));
                       

  
  
$Result1 mysql_query($insertSQL) or die(mysql_error());
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 21-03-2007, 10:50 AM
yipguseng's Avatar
Lost Webmaster
 
Join Date: Jan 2007
Location: Petaling Jaya
Posts: 857
Rep Power: 42
yipguseng will become famous soon enough yipguseng will become famous soon enough
Send a message via MSN to yipguseng
well, if u ask me to see any difference on the codes i can tell is this 2

GetSQLValueString($HTTP_POST_VARS['Supp_Id'], "text")
GetSQLValueString($_POST['Report_No'], "text"),

anyway, my suggestion is, dun go so fast in inserting a while lot of data without trying it 1st. Do a similar codes but only insert 1 or 2 data, just to make sure everything is working fine. Then when u r sure the data is inserted to the db successfully, then only u proceed with the rest of ur data

if u straight away copy and paste and modify, u wont know where's does the error comes from if the data cannot b insert
__________________
Boonage - Freedom is Everything
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 21-03-2007, 11:15 AM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
Quote:
Originally Posted by yipguseng View Post

GetSQLValueString($HTTP_POST_VARS['Supp_Id'], "text")
GetSQLValueString($_POST['Report_No'], "text"),
Actually, is there any difference between $HTTP_POST_VARS and $_POST ??
it is because, i used other database with the same method/code to insert.. n it worked fine(data inserted).. so, dunno whts wrong with this database..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 27-03-2007, 04:13 PM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
okay.. now im using this sql to insert..
PHP Code:
$RNo=$_POST['Report_No'];
$NVessel=$_POST['Name_of_Vessel'];
$RTime=$_POST['Report_Time'];
$DAccident=$_POST['Date_of_Accident'];
$TAccident=$_POST['Time_of_Accident'];
$IPName=$_POST['IP_Name'];
$IPSname=$_POST['IP_Surname'];
$IPDOB=$_POST['IP_DOB'];
$IPNat=$_POST['IP_Nationality'];
$IPsex=$_POST['IP_Sex'];
$IPSAdd=$_POST['IP_Address'];
$IPPass=$_POST['IP_Passport'];
$DOEvent=$_POST['Desc_of_Event'];

$query="INSERT INTO Report(Report_No, Name_of_Vessel, Report_Time, Date_of_Accident, Time_of_Accident, IP_Name, IP_Surname, IP_DOB, 
IP_Nationality, IP_Sex, IP_Address, IP_Passport, Desc_of_Event) VALUES ('$RNo','$NVessel', '$RTime','$DAccident',
'$TAccident','$IPName', '$IPSname','$IPDOB', '$IPNat',
'$IPsex','$IPAdd','$IPPass', '$DOEvent')"
;              

$result=mysql_query($query) or die( "There was an error running '$query' " mysql_error()); 
the values were inserted, but it became like this when i browse the database in phpadmin.. (shows in the picture link below). the first columns empty..



when i refresh the form, there would be an sql error
Quote:
There was an error running 'INSERT INTO Report(Report_No, Name_of_Vessel, Report_Time, Date_of_Accident, Time_of_Accident, IP_Name, IP_Surname, IP_DOB, IP_Nationality, IP_Sex, IP_Address, IP_Passport, Desc_of_Event) VALUES ('','', '','', '','', '','', '', '','','', '')' Duplicate entry '' for key 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 28-03-2007, 10:29 AM
yipguseng's Avatar
Lost Webmaster
 
Join Date: Jan 2007
Location: Petaling Jaya
Posts: 857
Rep Power: 42
yipguseng will become famous soon enough yipguseng will become famous soon enough
Send a message via MSN to yipguseng
if ur Report_No is ur primary key, i assume u will b using auto-increment right? so if u r using auto-increment, u do not need to grab any variable from the form. so the $RNo=$_POST['Report_No']; shouldn't be there so as in the INSERT sql.

of course u will encounter such error bcoz u r using xxxx=$_POST["xxxx"] to grab the variables stored in the https, so once u refresh it, everything in the https will be refresh and store nothing, so ur $_POST has nothing to grab and tat correspond to ('','', '','', '','', '','', '', '','','', '')

again the same advise. Try and few data 1st before u really put in so many into the test.
__________________
Boonage - Freedom is Everything
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 28-03-2007, 10:46 AM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
Thanks.. but my Report_No(primary key) in Report table is not an auto increment.. it has to be filled in by users. and yes, i had tried with few data before, but it still has the same prob..
i think i got the
Quote:
There was an error running 'INSERT INTO Report(Report_No, Name_of_Vessel, Report_Time, Date_of_Accident, Time_of_Accident, IP_Name, IP_Surname, IP_DOB, IP_Nationality, IP_Sex, IP_Address, IP_Passport, Desc_of_Event) VALUES ('','', '','', '','', '','', '', '','','', '')' Duplicate entry '' for key 1
because of the blank columns.. (shows on the image..dont know why it became like tht..) i'm only key in data for one time.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 28-03-2007, 10:57 AM
yipguseng's Avatar
Lost Webmaster
 
Join Date: Jan 2007
Location: Petaling Jaya
Posts: 857
Rep Power: 42
yipguseng will become famous soon enough yipguseng will become famous soon enough
Send a message via MSN to yipguseng
is seldom having the database's primary key to be insert by user themselve.

but anyway, i will try it out tonight when i m at home. coz i cant test it now in the offfice ^^

hope u can wait, but if u have any update on it do post it here
mean time if anybody out there which see this and knows the solution, please do reply
__________________
Boonage - Freedom is Everything
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 30-03-2007, 01:45 PM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
okay.. i had discovered that everytime i opened my form on the browser (without inserting any value), there would be blank columns in my database..
why is that??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 30-03-2007, 01:58 PM
yipguseng's Avatar
Lost Webmaster
 
Join Date: Jan 2007
Location: Petaling Jaya
Posts: 857
Rep Power: 42
yipguseng will become famous soon enough yipguseng will become famous soon enough
Send a message via MSN to yipguseng
sorry for the late trial on this coz is been bz for me for the whole week in the office as well as at home.

but from wat u said above, i would like to know is all ur sql. php processing code and form in the same page?
__________________
Boonage - Freedom is Everything
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 30-03-2007, 02:06 PM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
i don't think it's enough to post all in one page right??
or hw do u want me to do??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 30-03-2007, 02:57 PM
Novice Webmaster
 
Join Date: Mar 2007
Location: Port Klang, Malaysia
Posts: 20
Rep Power: 0
sweets is on a distinguished road
yeyy.. finally succeeded!!
i can insert my data into database without 'that blank columns'!!
i tried using a hidden field named MM_insert on the form.

this is my code:
PHP Code:
if (isset($_POST['MM_insert']))
{
    
$NVessel $_POST['Name_of_Vessel'];
    
$RNo $_POST['Report_No'];
    
$RTime=$_POST['Report_Time'];
    
$DAccident=$_POST['Date_of_Accident'];
    
$TAccident=$_POST['Time_of_Accident'];
    
$IPName=$_POST['IP_Name'];
    
$IPSname=$_POST['IP_Surname'];
    
$IPDOB=$_POST['IP_DOB'];
    
$IPNat=$_POST['IP_Nationality'];
    
$IPsex=$_POST['IP_Sex'];
    
$IPSAdd=$_POST['IP_Address'];
    
$IPPass=$_POST['IP_Passport'];
    
$DOEvent=$_POST['Desc_of_Event'];
    
    
$query "INSERT INTO Report (Name_of_Vessel, Report_No, Report_Time, Date_of_Accident, Time_of_Accident, IP_Name, IP_Surname, 
              IP_DOB, IP_Nationality, IP_Sex, IP_Address, IP_Passport, Desc_of_Event) VALUES('$NVessel', '$RNo', '$RTime','$DAccident',
             '$TAccident','$IPName', '$IPSname','$IPDOB', '$IPNat','$IPsex','$IPAdd','$IPPass', '$DOEvent')"
;
    
mysql_query($query) or die("Error inserting into Report: ".mysql_error());

of course, i tested it with 2 data first.. then, when there's nothing wrong with it, i continue with others..
but sadly, i still had problems with the sql to view all the data..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread: