PDA

View Full Version : Inserting code..



emme85
March 20th, 2007, 01:24 AM
Is there anything wrong with my inserting code here??


<?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());
}
?>
theres no errors, but the datas werent inserted..

borrob
March 20th, 2007, 04:40 AM
shouldn't this:
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
be
VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s' ,'%s','%s')"

have a nice day

emme85
March 20th, 2007, 04:45 AM
Nope.. still not working..
thanks 4 replying..

bwh2
March 20th, 2007, 10:49 AM
echo out your $insertSQL and show it to us.

BetaWar
March 20th, 2007, 08:48 PM
Have you been able to get a shorter version of this script to run? I may have a way to fix it (get it working) but it will redo a lot of the sql as I haven't used this particular way of inserting information before...

emme85
March 20th, 2007, 08:49 PM
Okay.. ive already echo $insertSQL.. but nothing happen..

emme85
March 20th, 2007, 08:52 PM
echo out your $insertSQL and show it to us.


Have you been able to get a shorter version of this script to run? I may have a way to fix it (get it working) but it will redo a lot of the sql as I haven't used this particular way of inserting information before...

Really glad if u could show me how..

BetaWar
March 20th, 2007, 09:05 PM
Okay here is how I would do it (only the query)


$Result1 = mysql_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) VALUES ('" . $_POST['Report_No'] . "','" . $_POST['Name_of_Vessel'] . "','" . $_POST['Report_Time'] . "','" . $_POST['Date_of_Accident'] . "','" . $_POST['Time_of_Accident'] . "','" . $_POST['IP_Name'] . "','" . $_POST['IP_Surname'] . "','" . $_POST['IP_DOB'] . "','" . $_POST['IP_Nationality'] . "','" . $_POST['IP_Sex'] . "','" . $_POST['IP_Address'] . "','" . $_POST['IP_Passport'] . "')") or die( "There was an error running '$query' " . mysql_error());

emme85
March 20th, 2007, 09:16 PM
Okay here is how I would do it (only the query)


$Result1 = mysql_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) VALUES ('" . $_POST['Report_No'] . "','" . $_POST['Name_of_Vessel'] . "','" . $_POST['Report_Time'] . "','" . $_POST['Date_of_Accident'] . "','" . $_POST['Time_of_Accident'] . "','" . $_POST['IP_Name'] . "','" . $_POST['IP_Surname'] . "','" . $_POST['IP_DOB'] . "','" . $_POST['IP_Nationality'] . "','" . $_POST['IP_Sex'] . "','" . $_POST['IP_Address'] . "','" . $_POST['IP_Passport'] . "')") or die( "There was an error running '$query' " . mysql_error());

Hmm... still nothing.. thanks for helping though..
I wonder why it cant store data??:puzzled:

emme85
March 20th, 2007, 10:17 PM
Hey guys.. this is my full code for my webform..
position,const_acc,const_location are tables with checkboxes.
values were store for the checkboxes.. but still nothing in Report table..


<?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($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["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($HTTP_POST_VARS['Report_No'], "text"),
GetSQLValueString($HTTP_POST_VARS['Name_of_Vessel'], "text"),
GetSQLValueString($HTTP_POST_VARS['Report_Time'], "text"),
GetSQLValueString($HTTP_POST_VARS['Date_of_Accident'], "text"),
GetSQLValueString($HTTP_POST_VARS['Time_of_Accident'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_Name'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_Surname'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_DOB'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_Nationality'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_Sex'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_Address'], "text"),
GetSQLValueString($HTTP_POST_VARS['IP_Passport'], "text"));

$Result1 = mysql_query($insertSQL) or die( "There was an error running '$query' " . mysql_error());
}

if(isset($_POST['Injured_person']))
{
$values = implode(",", $_POST['Injured_person']);
$query = "INSERT INTO position (Injured_person) VALUES('$values')";
mysql_query($query) or die("Error inserting into position: ".mysql_error());

}
if(isset($_POST['Type']))
{
$values = implode(",", $_POST['Type']);
$query = "INSERT INTO const_acc (Type) VALUES('$values')";
mysql_query($query) or die("Error inserting into const_acc: ".mysql_error());

}
{
if(isset($_POST['Location']))
{
$values = implode(",", $_POST['Location']);
$query = "INSERT INTO const_location (Location) VALUES('$values')";
mysql_query($query) or die("Error inserting into const_location: ".mysql_error());
}
mysql_close($connect);
}

borrob
March 21st, 2007, 03:47 AM
if you are using php 5 then this is propably the problem:
It's not really a bug but $HTTP_POST_VARS isn't there in PHP5.
so use $_POST

have a nice day and i hope this will solve your prob...

emme85
March 21st, 2007, 03:58 AM
if you are using php 5 then this is propably the problem:
It's not really a bug but $HTTP_POST_VARS isn't there in PHP5.
so use $_POST

have a nice day and i hope this will solve your prob...

thanks borrob.. but im sorry.. id already replace the $HTTP_POST_VARS with $_POST before, but it still failed to store data.. huhuhu:crying:
p/s: yeah, im using php5..

Ahhhhhh... i dont know how to fix it!!! ;(

Thanks in advance..