4 Replies - 671 Views - Last Post: 13 March 2012 - 03:09 PM Rate Topic: -----

#1 bhavyanshu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 12

Problem with sql query.not updating the path of img file

Posted 11 March 2012 - 12:55 PM

Hi,
I am uploading an image through php and i want to store the path of the uploaded image in the sql database.
I have 2 pages

1.) insert.php

<?php

session_start();
if (!isset($_SESSION["MM_Username"]))
{
	$_SESSION["message"] = "Please Login";
}

?>
 <!--next comes the form, you must set the enctype to "multipart/frm-data" 
and use an input type "file" -->
 <form name="newad" method="post" enctype="multipart/form-data"  
action="chkupload.php">
 <table>
 	<tr><td><input type="file" name="image"></td></tr>
 	<tr><td><input name="Submit" type="submit" value="Upload image">
       </td></tr>
 </table>	
 </form>




this page works absolutely fine

2)chkupload.php
This is where the problem occurs
The problem is that the upload works fine.It creates the file in the images/ folder but does not save the path of it in the sql table. So basically i need help with this.
Also its under user authentication. Its a profile picture.

<?php require_once('Connections/mb.php'); 

$loginUsername = $_SESSION['MM_Username'];

//define a maxim size for the uploaded images in Kb
 define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the
// file  is an image by checking the extension.
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

//This variable is used as a flag. The value is initialized with 0 (meaning no 
// error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
 $errors=0;
//checks if the form has been submitted
 if(isset($_POST['Submit'])) 
 {
 	//reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	//if it is not empty
 	if ($image) 
 	{
 	//get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['image']['name']);
 	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 	//if it is not a known extension, we will suppose it is an error and 
        // will not  upload the file,  
	//otherwise we will do more tests
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension !=
 "png") && ($extension != "gif")) 
 		{
		//print error message
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
//get the size of the image in bytes
 //$_FILES['image']['tmp_name'] is the temporary filename of the file
 //in which the uploaded file was stored on the server
 $size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
	echo '<h1>You have exceeded the size limit!</h1>';
	$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images 
//folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
	echo '<h1>Copy unsuccessfull!</h1>';
	$errors=1;
}}}}

//If no errors registred, print the success message
 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h1>File Uploaded Successfully! Try again!</h1>";
 }



mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("musibridge") or die(mysql_error());
echo "Connected to Database";
$result = mysql_query("UPDATE artist92 SET path= $newname WHERE email = $loginUsername") 
or die(mysql_error());  

 ?>




The error i get is
Notice: Undefined variable: _SESSION in C:\xampp\htdocs\MB\chkupload.php on line 3
File Uploaded Successfully! Try again!
Connected to MySQL
Connected to DatabaseYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'jpg WHERE email =' at line 1


Please help me fix this. Its uploading but not updating the column path of the table artist92

This is my login page.
artlog.php
<?php require_once('Connections/mb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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;
}
}

$colname_Recordsetartist = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Recordsetartist = $_SESSION['MM_Username'];
}
mysql_select_db($database_mb, $mb);
$query_Recordsetartist = sprintf("SELECT * FROM artist92 WHERE email = %s", GetSQLValueString($colname_Recordsetartist, "text"));
$Recordsetartist = mysql_query($query_Recordsetartist, $mb) or die(mysql_error());
$row_Recordsetartist = mysql_fetch_assoc($Recordsetartist);
$totalRows_Recordsetartist = mysql_num_rows($Recordsetartist);
$query_Recordsetartist = "SELECT * FROM artist92";
$Recordsetartist = mysql_query($query_Recordsetartist, $mb) or die(mysql_error());
$row_Recordsetartist = mysql_fetch_assoc($Recordsetartist);
$totalRows_Recordsetartist = mysql_num_rows($Recordsetartist);

$colname_Recordsetartist = "-1";
if (isset($_SESSION['MM_email'])) {
  $colname_Recordsetartist = $_SESSION['MM_email'];
}
mysql_select_db($database_mb, $mb);
$query_Recordsetartist = sprintf("SELECT * FROM artist92 WHERE email = %s", GetSQLValueString($colname_Recordsetartist, "text"));
$Recordsetartist = mysql_query($query_Recordsetartist, $mb) or die(mysql_error());
$row_Recordsetartist = mysql_fetch_assoc($Recordsetartist);

$colname_Recordsetartist = "-1";
if (isset($_SESSION['MM_email'])) {
  $colname_Recordsetartist = $_SESSION['MM_email'];
}
mysql_select_db($database_mb, $mb);
$query_Recordsetartist = sprintf("SELECT * FROM artist92 WHERE email = %s", GetSQLValueString($colname_Recordsetartist, "text"));
$Recordsetartist = mysql_query($query_Recordsetartist, $mb) or die(mysql_error());
$row_Recordsetartist = mysql_fetch_assoc($Recordsetartist);
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['email'])) {
  $loginUsername=$_POST['email'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "artistprofile.php";
  $MM_redirectLoginFailed = "artlog.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_mb, $mb);
  
  $LoginRS__query=sprintf("SELECT email, password FROM artist92 WHERE email=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $mb) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with sql query.not updating the path of img file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Problem with sql query.not updating the path of img file

Posted 12 March 2012 - 07:06 AM

session_start() must be at the top of every page in which you're using sessions.

$result = mysql_query("UPDATE artist92 SET path= $newname WHERE email = $loginUsername") 

You need to quote strings in SQL queries.

And this is not really much of a database question, so moved to PHP.
Was This Post Helpful? 0
  • +
  • -

#3 bhavyanshu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 12

Re: Problem with sql query.not updating the path of img file

Posted 12 March 2012 - 07:44 AM

View PostJackOfAllTrades, on 12 March 2012 - 07:06 AM, said:

session_start() must be at the top of every page in which you're using sessions.

$result = mysql_query("UPDATE artist92 SET path= $newname WHERE email = $loginUsername") 

You need to quote strings in SQL queries.

And this is not really much of a database question, so moved to PHP.


Done it.
Now the SQL syntax error and session variable issue is gone but it still does not update the path in the table artist92
What to do?
Was This Post Helpful? 0
  • +
  • -

#4 bhavyanshu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 12

Re: Problem with sql query.not updating the path of img file

Posted 13 March 2012 - 11:26 AM

Isnt it now a database question???
Since its not updating the path in the artist table.. :/
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Problem with sql query.not updating the path of img file

Posted 13 March 2012 - 03:09 PM

No, not really. Sort of. I probably answered you with this in my previous reply:

Quote

You need to quote strings in SQL queries.


Also, and I'm really sick of saying this (no offense, I realize you're new here), but when you're building queries from dynamic data and they're failing and/or not working as you expect, then you NEED to somehow print or log the query that was generated and not working. You can then take that query and run it directly against the database in a SQL client to test it.

This is straight-up logical thinking. Hmmm...my query isn't working, I wonder why? Maybe I should check what I'm sending to the database.

Also, your table name of "artist92" is suspicious and leads me to believe your database might just be an utter mess.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1