Page 1 of 1

Trouble editing member information w/o erasing pic in db

Posted: Mon Oct 24, 2005 11:51 pm
by Tehquickness
I am working on building a website for my fraternity. The Section I am working on now is going to be a memebrs section that will include all the members information and a picture of them. I will then store this info into a table. I have all of it figured out how to input the data, how to edit the data, and how to upload the pictures. When i go to edit the members section i have a form that has all the information in formation in it for the user to change if they want and at the bottom is an empty file input.
Now my idea was that when i went to process the form, i would just use check to see if the form returned an empty value and if it did, it would not need to register a new picture in the database.
Having said that I will post the relevent code.


This is the form that displays the member information for the user to edit.

Code: Select all

<?php

$name = $_POST[name];
$conn = include("library/open_conn_db.php");
$sql = "SELECT * FROM members_page WHERE name = '{$name}';";

$result = mysql_query($sql, $conn) or die(mysql_error());

if (!$result) {
	echo "could not do the search ($sql) from db: ".mysql_error();
	echo "<br> tell Rayn about this";
}
if (mysql_num_rows($result) == 0) {
	echo "No results for that name, try using the full name<br>";
}

print "<form enctype=multipart/form-data action=edit_member_submit.php method=post>";
while ($newArray = mysql_fetch_assoc($result)){
	$roll_number = $newArray['roll_number'];
	$name = $newArray['name'];
	$classification = $newArray['classification'];
	$major = $newArray['major'];
	$interests = $newArray['interests'];
	$birthday = $newArray['birthday'];
	$email = $newArray['email'];
	$position = $newArray['position'];
	$picture = $newArray['picture'];

	print "<b>Name:</b><input type=text name=name size=20 value='$name'> <br>";
	print "<b>Roll Number:</b> GT<input type=text name=roll_no size=5 value='$roll_number'> <br>";
	print "<b>Class: </b><input type=text name=classif value='$classification'><br>";
	print "<b>Major:</b><input type=text name=major value='$major'><br>";
	print "<b>Interests:</b><input type=text size=30 name=interests value='$interests'> <br>";
	print "<b>Email:</b><input type=text size=30 name=email value='$email'><br>";
	print "<b>Position:</b><input type=text name=position value='$position'><br><br>";
	print "Current picture:<br><img src=library/pictures/$picture><br><input type=hidden name=picture value='picture'>";
	print "<b>New Picture:</b><input type=file name=fileupload><br>";
	print "Leave blank to keep current picture.<br>";
	print "<input type=submit name=\"Submit\" value=\"Edit This Member\">";
	
}
print "</form>";
?>

This is the file that processes the said form and inputs it into the mysql database

Code: Select all

$rollnumber = $_POST[roll_no];
$name = $_POST[name];
$classification = $_POST[classif];
$major = $_POST[major];
$interests = $_POST[interests];
$birthday=  $_POST[birthday];
$email = $_POST[email];
$position = $_POST[position];
$picture = $_POST[picture];

$conn = include("library/open_conn_db.php");

$file_dir = "htdocs\library\pictures";
$uniq = uniqid("");

foreach($_FILES as $file_name => $file_array) {
	if (is_uploaded_file($file_array['tmp_name'])) {
		move_uploaded_file($file_array['tmp_name'], "$file_dir\\$uniq") or die("could not upload");
		print "file was uploaded<br><br>";
        }
}


if(empty($file_array['name'])){
    $sql = "replace into members_page values ('$name', '$rollnumber', '$classification', '$major',         '$interests', '$birthday', '$email', '$position', '$picture');";
    print "empty";
}else{
    $sql = "replace into members_page values ('$name', '$rollnumber', '$classification', '$major',  '$interests', '$birthday', '$email', '$position', '$uniq');";
}






//execute SQL statemnt;
$result = mysql_query($sql, $conn) or die(mysql_error());
//echo the result;
if ($result = 1) {
	print "Edit Successful <br>";
	print "<a href=index.html>Return to Administration Panel</a>";
}else{
	print " Error, Try and repeat problem and report to Ryan";
	print "<a href=index.html>Return to Administration Panel</a>";
}

?>

Currently the when the code is run, i can input a picture but when I try to change the data without changing the picture, it will put '' in the database wehre the uniqid used to be for the picture.
This is my first post so let me know if you need any more info.
Thanks in advance.

Posted: Tue Oct 25, 2005 1:24 am
by n00b Saibot
This

Code: Select all

print "Current picture:<br><img src=library/pictures/$picture><br><input type=hidden name=picture value='picture'>";
should be

Code: Select all

print "Current picture:<br><img src=library/pictures/$picture><br><input type=hidden name=picture value='$picture'>";
also, you should use quotes to access named indices in array i.e. $_POST[name] should be $_POST['name'].

Posted: Tue Oct 25, 2005 1:51 am
by Tehquickness
good deal. I thought i had thought it all the way through. That fixed the problem. So is that a good way to solve that problem or would there be a more efficient way to solve this? this is more of a conceptual question now. I would like to see how other programers would takle it.

Posted: Tue Oct 25, 2005 2:27 am
by Jenk

Code: Select all

print ("Current picture:<br /><img src=\"library/pictures/$picture\" /><br /><input type=\"hidden\" name=\"picture\" value=\"$picture\" />");
:)

Posted: Tue Oct 25, 2005 9:30 am
by n00b Saibot
Jenk, that is only changing to XHTMLand your img tag is missing title attribute ;)
In reply to poster, I would avoid printing HTML through PHP. all those prints should be removed and replaced by HTML.

Posted: Tue Oct 25, 2005 9:54 am
by John Cartwright
n00b Saibot wrote:Jenk, that is only changing to XHTMLand your img tag is missing title attribute ;)
In reply to poster, I would avoid printing HTML through PHP. all those prints should be removed and replaced by HTML.
Why is that?

Posted: Wed Oct 26, 2005 4:26 am
by n00b Saibot
because PHP will have to parse the string and then it will output it. that parsing time may mean small or negligible lag in cases where there is less to output, but in cases where there large amount of HTML embedding in PHP it does introduce a noticeable lag. Maybe some benchmarking will prove my point (or disprove it ;) )

Posted: Wed Oct 26, 2005 10:13 am
by Tehquickness
so instead of using

Code: Select all

print "<table<tr><td>text goes here with a $variable</td></tr></table> ";
it would just be faster to do this

Code: Select all

<table><tr><td>
text goes here with a <?php $variable ?>
</td></tr></table>

assuming you declares a value for the variable earlier in the page of course.

Posted: Thu Oct 27, 2005 12:56 am
by n00b Saibot
yeah, but <?php $variable ?> should be <?php echo $variable ?> ;)