Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File upload and data insertion problem 1

Status
Not open for further replies.

stmosaic

IS-IT--Management
Nov 10, 2004
10
US
I've been modifying a php script that inserted data into my database to include an image file upload. Previously, it inserted the data just fine. But now it goes through the motions, but doesn't put anything into the database. The new code I added for the file upload seems to be working fine, the image goes where it should and is the right size. It is connecting to the db, no error from my connection script. Triple checked form names, etc. I can't seem to see what I've done wrong and I'm sure it's something I'm missing.
Here's the offending code segment:
Code:
$msg = "";
$sname = "";
$pix = "";
$credentials = "";
$tparagraph = "";
$slogan = "";
$seminar = "";
$dorder = "";

if(isset($_POST['Submit']))
{

// This file upload section seems to work fine.
    if ($pix == 'none')
	{
	echo "You must select your picture file to upload.<br><br>Please click the back button to go back.";
	exit;
	}

$upload_path = "/sitepath/";
$newname = $_FILES['pix']['name'];
$tmpfile = $_FILES['pix']['tmp_name'];
$dest = "$upload_path" . "$newname";

	if (!copy ($tmpfile,$dest))
	{
	echo "Something went wrong with the upload.";
	}
//end of file upload section - start of insertion code

	$sname = $_POST['sname'];
	$pix = $newname;
	$credentials = $_POST['credentials'];
	$tparagraph = $_POST['tparagraph'];
	$slogan = $_POST['slogan'];
	$seminar = $_POST['seminar'];
	$dorder = $_POST['dorder'];

	if(!isset($_GET['ID']))
	{
	$result = mysql_query("Insert into msi(sname, pix, credentials, tparagraph, slogan, seminar, dorder) values('$sname','$pix','$credentials','$tparagraph','$slogan','$seminar','$dorder')");
		$msg = "New record is saved";
	}
//end of insertion code. else follows from there for updates.
Like I said, no errors are thrown, just goes through the motions like it worked, but when I look at the db through mysql, there are no records in the db. Any solutions? Thanks for your time!!



 
You need some error checking with your database code.
In the script it might fail without notice.
Append:
Code:
$result = mysql_query("Insert into msi(sname, pix, credentials, tparagraph, slogan, seminar, dorder) values('$sname','$pix','$credentials','$tparagraph','$slogan','$seminar','$dorder')") OR die("MySQL insertion failed: ".mysql_error());

Maybe that will reveal something.
 
DRJ478,
It revealed:
MySQL insertion failed: Table 'msi.msi' doesn't exist

How bizarre! There are no instances of my calling a table by that name. Though it does look like a pix.jpg construct. I've suspected the $pix = $newname; might be part of the problem. Just can't figure out how to deal with it.

 
I see no place in the code where a specific database is selected. Is there somwhere a mysql_select_db() statement?
Check out the connection code (mysql_connect()) and append a similar error checking construct with
Code:
mysql_whatever(...) [COLOR=red]OR die("Error: ".mysql_error());[/color]

Also, the MySQL syntax says database.tablename, so it tries to insert into a database by the name of msi into a table called msi.
 
The connection code has the "or die" in it, that was why I knew it was ok. However, your pointing out "the MySQL syntax says database.tablename" was right on the money!!

Mea culpa, I'm an idiot. DB name is msi, table name is speakers. Works like a charm when you give it the right instructions! Thank you so much for helping me figure out where I went astray. Your help was invaluable. I feel about 2 inches tall right now. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top