Jump to content


Photo

Starting Big Project or How To Learn PHP In A Hurry


  • Please log in to reply
71 replies to this topic

#41 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 22 August 2006 - 10:04 PM

Sorry about the typos :(
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#42 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 22 August 2006 - 10:49 PM

Heh, thought I caught all the typos when I copied the code but didn't know that 'unsert' was supposed to be 'insert' :)

Ok, i'm running this query and nothing is getting inserted into the database(s) at this time.

$sql = "INSERT INTO members (business, name, title, phone, email, url, details, specialties, image, thumb_image) VALUES ('$business', '$name', '$title', '$phone', '$email', '$url', '$details', '$specialties', '$image', '$thumb_image')";
mysql_query($sql);
$newid = mysql_insert_id();

$sql = "INSERT INTO members_cat (memberid, catid) VALUES ('$newid', '$catid')";
mysql_query($sql);

Oddly enough i'm not getting any error messages either. I'll try again with an 'or die' in there and see if something pops up. But why it's not inserting is another question.

#43 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 22 August 2006 - 10:54 PM

try
<?php
$sql = "INSERT INTO members (business, name, title, phone, email, url, details, specialties, image, thumb_image) VALUES ('$business', '$name', '$title', '$phone', '$email', '$url', '$details', '$specialties', '$image', '$thumb_image')";
mysql_query($sql) or die (mysql_error());
?>

and see it you get an error message
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#44 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 22 August 2006 - 11:05 PM

Did that and it did produce an error. It was my turn for a typo. I had 'member_cat' as the table name while the query was looking for 'members_cat'. I made the switch and the data inserted fine.

The next step is figuring how to set up the image and the thumbnail functions. The 'add' form for the member's includes an image upload. I need to have that image available at a specific size for the 'summary' page and then for a fuller size for the 'profile' page.

I've looked around the forums and see lots of issues about thumbs or images but nothing specific to how to operate this during the initial insertion of the data into the mysql database. The other topics seem to address it in more of a 'File Uploader' kind of thing and this is a profile generator whereas the image is uploaded at the same time as their personal and business data.

Ideas?

#45 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 22 August 2006 - 11:38 PM

Have your registration form with a file field for the photo.

<FORM action="otherpage.php" method="POST" enctype="multipart/form-data">
Name : <input type="text" name="name" size="20"><br>
Title: <input type="text" name="title" size="20"><br>
Photo: <input type="file" name="image"><br>
<input type="submit" name="submit" value="Submit">
</FORM>

You are going to need a RESIZE() function which takes as aguments (input_file, output_file, size) and outputs a resized image to the output_file, which in my example code below creates a fuller image 400px wide and a thumb image 100px wide

The processing would be something like this (pseudocode)

    insert record without the filenames
    get $newid = mysql_insert_id()
    $fuller = "path/to/images/image_$newid.jpg";
    $thumb =  "path/to/images/thumb_$newid.jpg";
    resize($_FILES['image']['tmp_name'], $fuller, 400);
    resize($_FILES['image']['tmp_name'], $thumb, 100);
    UPDATE members SET image = '$fuller', thumb_image = '$thumb' WHERE memberid = $newid

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#46 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 22 August 2006 - 11:56 PM

Barand, as always you're a great help. Thank you.

Quick question on that. Is there a specific spot in the script where this code should be inserted/implemented? I'm assuming it would be ahead of the mysql_query since the details of the image(s) needs to be created in order to be inserted.

My form already has an 'image' and 'file' input field. I just haven't done anything in the code to process it. I tried another script I found but once again it's for what would be considered a 'stand alone' file upload script and not code that would be for the use in this profile creation setup i'm working on.

I have two folders set up for the images:

/images/
/images/thumbs/

set both permissions to 777 so no problems there.

This is just a guess, but i'm supposed to ignore the first line of your pseudocode i'm assuming?

An edited version would be like this:

get $newid = mysql_insert_id()
    $fuller = "images/image_$newid.jpg";
    $thumb =  "images/thumbs/thumb_$newid.jpg";
    resize($_FILES['image']['tmp_name'], $fuller, 250);
    resize($_FILES['image']['tmp_name'], $thumb, 80);
    UPDATE members SET image = '$fuller', thumb_image = '$thumb' WHERE memberid = $newid


#47 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 August 2006 - 12:02 AM

I'd recommend including the id of the member in the file name for the images so that if they subsequently upload a new image you can overwrite the old. You are not then left with a shed-load of unattached images.

But to do this you need to insert the new record to get its id, so you can allocate names for the image files
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#48 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 23 August 2006 - 12:13 AM

Ok, I agree with what you're describing.

I've inserted this code just behind the data query that inserts all the form contents so the function runs right after the data is processed. This line seems to be an issue as it produces an error no matter where I put it:

UPDATE members SET image = '$fuller', thumb_image = '$thumb' WHERE memberid = $newid;

Error:

Parse error: parse error, unexpected T_STRING in /home2/wwwxxxx/public_html/addbiz.php on line 40


#49 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 August 2006 - 12:19 AM

I did say it was "pseudocode". That code is Mysql, not PHP, so you would need

<?php
mysql_query ("UPDATE members SET image = '$fuller', thumb_image = '$thumb' WHERE memberid = $newid;");
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#50 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 23 August 2006 - 12:31 AM

Grrrr... ok, I should've known that and it didn't dawn on me until you mentioned it. I think i'm getting brain dead.

Made that change and (of course) another error appears.

This line:
resize($_FILES['image']['tmp_name'], $fuller, 250);

Gets this error:

Fatal error: Call to undefined function: resize() in /home2/wwwxxxx/public_html/addbiz.php on line 38


There's two 'resize' functions. I'm a betting man so I'm pretty certain that either/and/or of these would produce the error.



#51 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 August 2006 - 06:34 AM

They certainly will if you haven't written them yet.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#52 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 23 August 2006 - 08:45 PM

Ok, once again i'm lost. So then what do the 'resize' codes refer to?

#53 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 August 2006 - 08:50 PM


You are going to need a RESIZE() function which takes as aguments (input_file, output_file, size) and outputs a resized image to the output_file, which in my example code below creates a fuller image 400px wide and a thumb image 100px wide



If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#54 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 19 September 2006 - 05:56 PM

Ok, back on track with this monster.

Update, have been doing some restructuring of the registration form to coordinate with the insertion of data into multiple tables as per your original example from reply #12:

<?php
$sql = "INSERT INTO member (name, email, etc) VALUES ('$name', '$email', '$etc')";
mysql_query($sql);
$newid = mysql_unsert_id();

$sql = "INSERT INTO members_cat (memberid, categoryid) VALUES ('$newid', '$catid')";
mysql_query($sql);
?>


I did not use this method previously as I was taking a simpler method in using one table to insert the profiles into and then simply displaying them when summoned. However, I have since come to my senses and realize I need to update the method to something more  useful such as what you laid out in this table structure:

category        member_cat        members        specialties
==========      ===========        =========      ============
categoryid --+                id            +--  memberid    --+    id
category                |  memberid    >-+    name        +-<  memberid
                            +-< categoryid        title                speciality
                                                      company
                                                        phone
                                                        email
                                                        details
                                                        image


Here's the question. I've created this series of queries in the 'new' registration form based on your model but receive this error:

Fatal error: Call to undefined function: mysql_unsert_id() in /home2/wwwxxxx/public_html/register-test.php on line 89


The code including line 89 is this:

// Run query
$sql = "INSERT INTO `plateau_pros`(`username`, `password`, `confirmPass`, `firstname`, `lastname`, `email`, `business`, `title`, `address`, `city`, `zip`, `phone`, `fax`, `mobile`, `category`, `comments`, `specialties`, `photo`) VALUES('".@$_POST['username']."', '".@$_POST['password']."', '".@$_POST['confirmPass']."', '".@$_POST['firstname']."', '".@$_POST['lastname']."', '".@$_POST['email']."', '".@$_POST['business']."', '".@$_POST['title']."', '".@$_POST['address']."', '".@$_POST['city']."', '".@$_POST['zip']."', '".@$_POST['phone']."', '".@$_POST['fax']."', '".@$_POST['mobile']."', '".@$_POST['category']."', '".@$_POST['comments']."', '".@$_POST['specialties']."', '".substr(strrchr($eg_uploadFile1, "/"), 1)."'), $eg_objConn1";
mysql_query($sql);
$newid = mysql_unsert_id();  [b][color=red]<-- LINE 89[/color][/b]

$sql2 = "INSERT INTO members_cat (memberid, categoryid) VALUES ('$newid', '$catid')";
mysql_query($sql2);

$sql3 = "INSERT INTO specialties (memberid, specialties) VALUES ('$newid' 'specialties')";
mysql_query($sql3);


I looked up 'UNSERT' and can't find anything on it. Don't know if this is a typo or what. But, it's producing an error like it's 'unknown'. Can you shed some light on this please?


#55 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 September 2006 - 06:02 PM

I looked up 'UNSERT' and can't find anything on it. Don't know if this is a typo or what. But, it's producing an error like it's 'unknown'. Can you shed some light on this please?


As Thorpe pointed out in reply #39 in this thread, it should be "mysql_insert_id()". A typo on my part I'm afraid.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#56 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 19 September 2006 - 06:38 PM

I missed that in reply #39 :(  Sorry. But, glad I found out it was a typo so I don't have to search Google for hours looking for 'unsert'. :)

Now, as you notice i've changed the query codes a bit by addint $sql2 and $sql3 but now there's nothing being inserted. No errors occur...but nothing gets inserted. Once again i'm scratching my head.

Here's the latest snippet. See if you can spot WHY i'm going insane:

// Run query
$sql = "INSERT INTO `plateau_pros`(`username`, `password`, `confirmPass`, `firstname`, `lastname`, `email`, `business`, `title`, `address`, `city`, `zip`, `phone`, `fax`, `mobile`, `category`, `comments`, `specialties`, `photo`) VALUES('".@$_POST['username']."', '".@$_POST['password']."', '".@$_POST['confirmPass']."', '".@$_POST['firstname']."', '".@$_POST['lastname']."', '".@$_POST['email']."', '".@$_POST['business']."', '".@$_POST['title']."', '".@$_POST['address']."', '".@$_POST['city']."', '".@$_POST['zip']."', '".@$_POST['phone']."', '".@$_POST['fax']."', '".@$_POST['mobile']."', '".@$_POST['category']."', '".@$_POST['comments']."', '".@$_POST['specialties']."', '".substr(strrchr($eg_uploadFile1, "/"), 1)."'), $eg_objConn1";
mysql_query($sql);
$newid = mysql_insert_id();

$sql2 = "INSERT INTO members_cat (`memberid`, `categoryid`) VALUES ('$newid', '$catid')";
mysql_query($sql2);

$sql3 = "INSERT INTO specialties (`memberid`, `specialties`) VALUES ('$newid' '$specialties')";
mysql_query($sql3);


#57 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 September 2006 - 06:44 PM

add "... or die(mysql_error())" to each of the query calls. eg

mysql_query($sql) or die(mysql_error());

See if any clues given.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#58 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 19 September 2006 - 06:53 PM

Ok, that produced this:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1


Line 1, of course, is my opening <?php tag. Not sure what this eludes to. Here's the entire block of code:

<?php

// Turn on magic quotes to prevent SQL injection attacks
if(!get_magic_quotes_gpc())
	set_magic_quotes_runtime(1);
	
include 'dbconfig.php';
// Connect to database
$eg_objConn1 = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $eg_objConn1);
// Enable sessions
session_start();
// Set Session Value
$_SESSION['loggedin'] = @$_POST['username'];

$username =$_POST['username'];

// Validate users input
if(!empty($_POST))
	// Check email is a valid email address
	if(isset($_POST['email'])) if(!ereg("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,}\\.[0-9]{1,}\\.[0-9]{1,}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,}|[0-9]{1,})(\\]?)$", $_POST['email'])) $eg_error['email'] = "You must enter a valid email address!";
	// Check password has a value
	if(empty($_POST['password'])) $eg_error['password'] = "You must enter a password!";
	// Check that confirmPass is the same as (comparison)
	if(isset($_POST['confirmPass'])) if($_POST['confirmPass'] != @$_POST['password']) $eg_error['confirmPass'] = "Your passwords do not match!";
	// Check that username is numbers and letters
	if(isset($_POST['username'])) if(ereg("[!\"£\$%\^&\*()\+=\{}[.].][.[.]#~';:@/\.,<>\?\\| ]", $_POST['username'])) $eg_error['username'] = "The user name contains some illegal charactures, only use alpha-numeric charactures.";
	// Check username has a value
	if(empty($_POST['username'])) $eg_error['username'] = "You must enter a user name!";
	// Check if any errors were returned and run relevant code
	if(empty($eg_error))
	{
	
	  //check if username already exists
	  $sql_user_check = "SELECT * FROM plateau_pros WHERE username='$username'";
      $result_name_check = mysql_query($sql_user_check);
      $usersfound = mysql_num_rows($result_name_check);
      mysql_query($sql_user_check);
      
// if user  found, note that and end
if ($usersfound > 0) {
    $eg_error['username'] = "Username $username is already in use. Please choose another username to continue.";
    } else {
      
// Conditional statement
//if(!empty($_POST))

	// Upload File
	$eg_success_File1 = false;
	if(!empty($_FILES['photo']['name']))
	{
		// Check file is not larger than specified maximum size
		$eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false;
		// Check file is of the specified type
		if($eg_allowUpload)
			$eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false;
		
		if($eg_allowUpload)
		{
			if(is_uploaded_file($_FILES['photo']['tmp_name']))
			{
				$eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/";
				
				$eg_uploadFile1 = $eg_uploaddir.rawurlencode($_FILES['photo']['name']);	
				// Create a unique filename for the uploaded file
				$eg_i = 1;
				while (file_exists($eg_uploadFile1))
				{
					$eg_separated_filename = explode(".",$eg_uploadFile1);
					if (substr($eg_separated_filename[0],-1) == $eg_i)
					{
						$eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1));
						$eg_i++;
					}
					$eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i";
					$eg_uploadFile1 = implode(".",$eg_separated_filename);
				}
							
				$eg_success_File1 = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadFile1);
			}
			
		}
		
	}

	// Run query
$sql = "INSERT INTO `plateau_pros`(`username`, `password`, `confirmPass`, `firstname`, `lastname`, `email`, `business`, `title`, `address`, `city`, `zip`, `phone`, `fax`, `mobile`, `category`, `comments`, `specialties`, `photo`) VALUES('".@$_POST['username']."', '".@$_POST['password']."', '".@$_POST['confirmPass']."', '".@$_POST['firstname']."', '".@$_POST['lastname']."', '".@$_POST['email']."', '".@$_POST['business']."', '".@$_POST['title']."', '".@$_POST['address']."', '".@$_POST['city']."', '".@$_POST['zip']."', '".@$_POST['phone']."', '".@$_POST['fax']."', '".@$_POST['mobile']."', '".@$_POST['category']."', '".@$_POST['comments']."', '".@$_POST['specialties']."', '".substr(strrchr($eg_uploadFile1, "/"), 1)."'), $eg_objConn1";
mysql_query($sql) or die(mysql_error());
$newid = mysql_insert_id();

$sql2 = "INSERT INTO members_cat (`memberid`, `categoryid`) VALUES ('$newid', '$catid')";
mysql_query($sql2) or die(mysql_error());

$sql3 = "INSERT INTO specialties (`memberid`, `specialties`) VALUES ('$newid' '$specialties')";
mysql_query($sql3) or die(mysql_error());

// set session ID and redirect to login page upon success
// Set Session Value
			$_SESSION['loggedin'] = @$_POST['username'];
			// Go to page
			header("Location: login.php");
			exit;
}
}
?>


#59 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 September 2006 - 07:08 PM

It is referring to line 1 of the SQL query string. As its all on one line it's not too meaningful.

I suspect it prob the first of the queries. in addition to showing mysql_error() also

echo $sql;
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#60 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 September 2006 - 07:12 PM

If it's a resource id, it means it' snot returning any information.
check to make sure your table names match your query, as well as your fields.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users