Page 1 of 1

[SOLVED] Can't get variable to insert into MySQL from PHP

Posted: Tue Jun 22, 2004 11:41 pm
by Cateyes
Ok my problem is I am passing a variable from one page to another and it works cause I use a test on line 4, line 3 gets the variable but when I try and input the other 2 variables it's like it either can't see the one I brought over or I am trying to insert it wrond into the record my insert line is between 30 and 40 somewhere. Here is the code.

Code: Select all

<?php
<?php require_once('Connections/Covuploads.php'); ?>
<?php
$filesname = $_GET['recordID'];
echo "$filesname";
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 test (test1, test2, file) VALUES (%s, %s, %s)",
                       GetSQLValueString($HTTP_POST_VARS['test1'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['test2'], "text"),
                       GetSQLValueString($HTTP_POST_VARS['$filename'], "text"));

  mysql_select_db($database_Covuploads, $Covuploads);
  $Result1 = mysql_query($insertSQL, $Covuploads) or die(mysql_error());

  $insertGoTo = "uploadsuccess.htm";
  if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_Covuploads, $Covuploads);
$query_rs_insert = "SELECT * FROM test";
$rs_insert = mysql_query($query_rs_insert, $Covuploads) or die(mysql_error());
$row_rs_insert = mysql_fetch_assoc($rs_insert);
$totalRows_rs_insert = mysql_num_rows($rs_insert);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<p>
  <input name="$filename" type="hidden" id="$filename" value="<?php echo $row_rs_insert['file']; ?>">
</p>
<p>Test 1	
    <input name="test1" type="text" id="test1">
</p>
<p>Test 2	
  <input name="test2" type="text" id="test2">
</p>
<p>
  <input type="submit" name="Submit" value="Submit">
</p>
<p>
  <input type="hidden" name="MM_insert" value="form1">
</p>
</form>
</body>
</html>
<?php
mysql_free_result($rs_insert);
?>

?>
The message that I keep getting is this:
Column 'file' cannot be null

I am probably missing something simple as it seems most of my mistakes are ones that I should be catching but somehow miss.

Posted: Wed Jun 23, 2004 12:03 am
by feyd
change

Code: Select all

GetSQLValueString($HTTP_POST_VARS['$filename'], "text"));
to

Code: Select all

GetSQLValueString($HTTP_POST_VARS[$filename], "text"));

I think... :?

Posted: Wed Jun 23, 2004 12:03 am
by RobertGonzalez
Just a thought, but try removing the quote marks around the filename array index:

Find:

Code: Select all

<?php

                       GetSQLValueString($HTTP_POST_VARS['$filename'], "text")); 

?>
Replace with:

Code: Select all

<?php

                       GetSQLValueString($HTTP_POST_VARS[$filename], "text")); 

?>
You are getting a NULL value because the array is looking for an index of (literally) $filename instead of whatever filename is stored in the variable $filename.

Hope this helps.

Posted: Wed Jun 23, 2004 12:12 am
by Cateyes
OK made the chages now it's giving me one more additional error this is what the page displays
Notice: Undefined variable: filename in C:\My WebSite\testrec.php on line 38

Notice: Undefined index: in C:\My WebSite\testrec.php on line 38
Column 'file' cannot be null


It's like it forgets the variable somehow.
just noticed this line would it be the problem?

Code: Select all

<?php
$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
?>
It's on line 30

Posted: Wed Jun 23, 2004 12:30 am
by feyd
$filename isn't being set correctly.. it's an empty string in fact.. so is $_GET['recordID'] being passed?

Posted: Wed Jun 23, 2004 12:58 am
by Cateyes
OK figured out part of the problem line 3 code

Code: Select all

<?php
$filesname = $_GET['recordID'];
?>
and line 38 code

Code: Select all

<?php
GetSQLValueString($HTTP_POST_VARS[$filesname], "text"));
?>

A damn s caused some of this headache. Now I get a new error on the page here it is

Code: Select all

tutorials/covtest.rar
Notice: Undefined index: tutorials/covtest.rar in Z:\My WebSite\testrec.php on line 38
Column 'file' cannot be null
The first line is the echo command to make sure the value passed from one page to the other.

Posted: Wed Jun 23, 2004 1:01 am
by feyd
$HTTP_POST_VARS[$filesname] is not set, or an empty string.

post the form you are using to post the data.

Posted: Wed Jun 23, 2004 1:06 am
by Cateyes
Just so you know the value that $filesname is equal to for this test is tutorials/covtest.rar would having a slash in in some be the casu even though the data base is set for varchar of 30

Posted: Wed Jun 23, 2004 1:22 am
by feyd
why not just use:

Code: Select all

GetSQLValueString($filesname,"text"));
?

Posted: Wed Jun 23, 2004 1:54 am
by Cateyes
That was the solution feyd I don't know why I keep making things diffucult the simplest solution is usually the answer. I am working on the page that actually loads this page and for some reason after the file is upload I would like it to say upload successfully or load this page but for some reason it quit working it used to say uploaded successfully now it just stays on the page no change I mouse over the button the value I need is there and the file is uploaded so I will work on it later today and if I am not making progress I will come back and ask the masters of php. :)
P.S. I won't say GODS might swell the heads to much :lol:

Posted: Wed Jun 23, 2004 4:57 am
by vpinho
Hi! I think I might help.

instead of :

GetSQLValueString($HTTP_POST_VARS['$filename'], "text"));

use:

GetSQLValueString($HTTP_POST_VARS['\$filename'], "text"));

Posted: Wed Jun 23, 2004 5:04 am
by feyd
uh... no. it's inside a single quote string, which doesn't resolve variables. .. and it was already fixed, if you read the rest of the post. :roll: