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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

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

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

change

Code: Select all

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

Code: Select all

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

I think... :?
Last edited by feyd on Wed Jun 23, 2004 12:04 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$filename isn't being set correctly.. it's an empty string in fact.. so is $_GET['recordID'] being passed?
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post 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.
Last edited by Cateyes on Wed Jun 23, 2004 1:01 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$HTTP_POST_VARS[$filesname] is not set, or an empty string.

post the form you are using to post the data.
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not just use:

Code: Select all

GetSQLValueString($filesname,"text"));
?
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post 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:
vpinho
Forum Newbie
Posts: 1
Joined: Wed Jun 23, 2004 4:57 am

Post by vpinho »

Hi! I think I might help.

instead of :

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

use:

GetSQLValueString($HTTP_POST_VARS['\$filename'], "text"));
Last edited by vpinho on Wed Jun 23, 2004 5:07 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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:
Post Reply