PDA

View Full Version : For insert from Tutorial


kona72
10-21-2008, 06:18 PM
Hi Guys/Gals,

I am using the tutorial method that DavidJ so graciously provided for us and I have run into a bit of a problem. I think i have followed it all correctly but it simply will not insert the data.

I am simply trying to modify the tutorial to suit my needs but cannot seem to find the issue at hand here.


This is the page right now....
http://www.atechaircovers.com/control/addproduct.php

This is the code for the page


<?php
include("../connection/atechaircovers.php");

/////////////////////////////////////////////////////////////////////////////////////////////////////
$rec_id = $_POST['rec_id'];
$favcategory = $_POST['favcategory'];
$favproduct = $_POST['favproduct'];
$favdesc = $_POST['favdesc'];
$favprice = $_POST['favprice'];
$active = $_POST['active'];
$userfile = $_POST['userfile'];
/////////////////////////////////////////////////////////////////////////////////////////////////////

if ($rec_id && $favcategory && $favproduct && $favdesc && $favprice && $active && $userfile){

/////////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("INSERT into products (rec_id, favcategory, favproduct, favdesc, favprice, active, userfile) values ('$rec_id', '$favcategory', '$favproduct', '$favdesc', '$favprice', '$active', '$userfile')");
mysql_query($query)or die(mysql_error());
/////////////////////////////////////////////////////////////////////////////////////////////////////

}elseif ($active){

echo "Empty Field Alert!";

}

/////////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM products");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////////////////////////////////



//
//{ if(is_uploaded_file($_FILES[userfile][tmp_name]))
// {
// $dt=date("YmdHis");
// $file1=$dt.$_FILES[userfile][name];
// copy($_FILES[userfile][tmp_name],"../images/$file1");
// }}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>A Touch of Elegance Administration Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../styleshop/mainstyle.css" rel="stylesheet" type="text/css">
<LINK REL="SHORTCUT ICON" HREF="favicon.ico">
</head>

<body><a name="top" id="top"></a>
<div id="container">
<div id="contactcms"></div>
<div id="header"></div>
<!--- Left Column --->
<div id="content">
<div id="leftcol">
<p align="center">Add Product</p>
<p align="center">&nbsp;</p>
<form method="POST" name="insertform" id="insertform" ENCTYPE="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>">

<p><input type="hidden" name="rec_id" id="rec_id"></p>

<p><label><select name="favcategory" id="favcategory"><option>Select the type of Wedding Favor</option></select></label></p>

<p><label><input type="text" name="favproduct" id="favproduct"></label>Product Name</p>

<p>Product Description<br>
<label><textarea name="favdesc" id="favdesc" cols="45" rows="5"></textarea></label>
</p>

<p><label><input type="text" name="favprice" id="favprice"></label>Price</p>

<p>Product Active?</p><p>
<label><input type="radio" name="active" value="yes" id="active_0">Yes</label>
<br />
<label><input type="radio" name="active" value="no" id="active_1">No</label>
<br /></p>

<p><input type="file" name="userfile">Width should be 200pixels wide. Resolution of 72ppi</p>
<p>
<label>
<input type="submit" name="Submit" id="Submit" value="Submit">
</label>
</p>
</form>

</div>
</div>

<div id="footer"></div>
</div>
</body>
</html>


and finally the sql


-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 21, 2008 at 01:12 PM
-- Server version: 5.0.41
-- PHP Version: 5.2.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `atechaircove`
--

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE `products` (
`rec_id` int(11) NOT NULL auto_increment,
`favcategory` varchar(100) default NULL,
`favproduct` varchar(100) default NULL,
`favdesc` text,
`favprice` varchar(100) default NULL,
`active` varchar(10) default NULL,
`userfile` varchar(100) default NULL,
PRIMARY KEY (`rec_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `products`
--

davidj
10-21-2008, 07:04 PM
contact me through messenger

gavimobile
10-22-2008, 08:55 PM
you should always test your queries. if you have phpmyadmin, paste your insert query in there and see where the insert is failing.

i see another few problems which might help.
elseif ($active) should equal something something or it will always echo
echo "Empty Field Alert!";
do something like if ($active = 'no'){}

userfile you might want to change to varchar 255, to make sure the file name is long enough.

rec_id doesnt need to be in the query so you can remove
'$rec_id', and rec_id,because its being auto incremented.

your upload script i believe should have quotes $_FILES["userfile"]["tmp_name];

you really need to test your query with the sql tool in phpmyadmin, it will save you a whole lot of time. paste it just like this with the quotes
and each variable fill with something.

"INSERT into products (favcategory, favproduct, favdesc, favprice, active, userfile) values (cat1, fav1, desc1, price1, yes, /temp/file.ext)" let me know what it prints back

davidj
10-22-2008, 10:03 PM
i see another few problems which might help.
elseif ($active) should equal something something or it will always echo
echo "Empty Field Alert!";
do something like if ($active = 'no'){}


this is valid


elseif ($active)


as is this

$_FILES[userfile][tmp_name]

you can reference arrays without using quotes in the keys

gavimobile
10-23-2008, 07:09 AM
this is valid


elseif ($active)
as is this

$_FILES[userfile][tmp_name]you can reference arrays without using quotes in the keys

oops! , but wouldnt $active always echo something else?if ($active) means
that if $active = yes || $active = no, cause thoese are the values he has in his radio buttons.

davidj
10-23-2008, 07:22 AM
elseif($active)


this just means it contains something and is not null

gavimobile
10-23-2008, 11:53 AM
elseif($active)
this just means it contains something and is not null
but than it will always contain something, unless if $active = "";

gavimobile
10-23-2008, 01:16 PM
no.. makes absolutly no sence!!

this is exactly what his code is doing.

<?php
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];

if ($a && $b && $c) {
echo "query ";
echo $a . ' ' . $b . ' ' . $c ;
} elseif ($c) {
echo "error";
}
?>

<form action="" method="post">
A:<input type="text" name="a" /><br />
B:<input type="text" name="b" /><br />
C:<input type="text" name="c" /><br />
<input type="submit" name="submit" />
</form>
dj, if a & b are filled, and u leave c empty, it wont even print the error...

however if i change elseif ($c){} to elseif (empty($c)){} assuming this is inside a if (isset($submit)) the code will work just fine.

this is how his code should look,


<?php
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$submit = $_POST['submit'];
if (isset($submit)){
if ($a && $b && $c) {
echo "query ";
echo $a . ' ' . $b . ' ' . $c ;
} elseif (empty($c)) {
echo "error";
}
}
?>

<form action="" method="post">
A:<input type="text" name="a" /><br />
B:<input type="text" name="b" /><br />
C:<input type="text" name="c" /><br />
<input type="submit" name="submit" />
</form>


why am i wrong?

gavimobile
10-23-2008, 01:30 PM
kona72 does it work if u do this?


<?php
include("../connection/atechaircovers.php");

/////////////////////////////////////////////////////////////////////////////////////////////////////
$favcategory = $_POST['favcategory'];
$favproduct = $_POST['favproduct'];
$favdesc = $_POST['favdesc'];
$favprice = $_POST['favprice'];
$active = $_POST['active'];
$userfile = $_POST['userfile'];
$Submit = $_POST['Submit']
/////////////////////////////////////////////////////////////////////////////////////////////////////

if (isset($Submit)) {
if ($favcategory && $favproduct && $favdesc && $favprice && $active && $userfile){

/////////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("INSERT into products (favcategory, favproduct, favdesc, favprice, active, userfile) values ('$favcategory', '$favproduct', '$favdesc', '$favprice', '$active', '$userfile')");
mysql_query($query)or die(mysql_error());
/////////////////////////////////////////////////////////////////////////////////////////////////////

}elseif (empty($active)){

//$active field is empty and must be filled in
echo "Empty Field Alert!";

}

} //end of submit


/////////////////////////////////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM products");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////////////////////////////////



//
//{ if(is_uploaded_file($_FILES[userfile][tmp_name]))
// {
// $dt=date("YmdHis");
// $file1=$dt.$_FILES[userfile][name];
// copy($_FILES[userfile][tmp_name],"../images/$file1");
// }}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>A Touch of Elegance Administration Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../styleshop/mainstyle.css" rel="stylesheet" type="text/css">
<LINK REL="SHORTCUT ICON" HREF="favicon.ico">
</head>

<body><a name="top" id="top"></a>
<div id="container">
<div id="contactcms"></div>
<div id="header"></div>
<!--- Left Column --->
<div id="content">
<div id="leftcol">
<p align="center">Add Product</p>
<p align="center">&nbsp;</p>
<form method="POST" name="insertform" id="insertform" ENCTYPE="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>">

<p><input type="hidden" name="rec_id" id="rec_id"></p>

<p><label><select name="favcategory" id="favcategory"><option>Select the type of Wedding Favor</option></select></label></p>

<p><label><input type="text" name="favproduct" id="favproduct"></label>Product Name</p>

<p>Product Description<br>
<label><textarea name="favdesc" id="favdesc" cols="45" rows="5"></textarea></label>
</p>

<p><label><input type="text" name="favprice" id="favprice"></label>Price</p>

<p>Product Active?</p><p>
<label><input type="radio" name="active" value="yes" id="active_0">Yes</label>
<br />
<label><input type="radio" name="active" value="no" id="active_1">No</label>
<br /></p>

<p><input type="file" name="userfile">Width should be 200pixels wide. Resolution of 72ppi</p>
<p>
<label>
<input type="submit" name="Submit" id="Submit" value="Submit">
</label>
</p>
</form>

</div>
</div>

<div id="footer"></div>
</div>
</body>
</html>

kona72
10-23-2008, 02:33 PM
Hey Gavimobile,

Unfortunately it does not. I get this error

Parse error: syntax error, unexpected T_IF in C:\Users\Trafick\Documents\Wamp\ATE_newsite\contro l\addproduct.php on line 14

Thanks!

gavimobile
10-23-2008, 02:40 PM
kona72, i forgot to put a ; on end of line 11

should be

$Submit = $_POST['Submit'];

also change line 10 from this
$userfile = $_POST['userfile'];
to this

$userfile = $_FILES['userfile']['name'];

kona72
10-23-2008, 02:50 PM
Fixed that but still not adding to the DB

kona72
10-23-2008, 02:59 PM
Now I am getting this error...

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Users\Trafick\Documents\Wamp\ATE_newsite\connec tion\atechaircovers.php on line 9

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Users\Trafick\Documents\Wamp\ATE_newsite\contro l\addproduct.php on line 19

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Users\Trafick\Documents\Wamp\ATE_newsite\contro l\addproduct.php on line 19
Access denied for user 'ODBC'@'localhost' (using password: NO)

2bz2p
10-23-2008, 03:13 PM
Now I am getting this error...

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Users\Trafick\Documents\Wamp\ATE_newsite\connec tion\atechaircovers.php on line 9


Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Users\Trafick\Documents\Wamp\ATE_newsite\contro l\addproduct.php on line 19

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Users\Trafick\Documents\Wamp\ATE_newsite\contro l\addproduct.php on line 19
Access denied for user 'ODBC'@'localhost' (using password: NO)



You need to check your configuration file and make sure the user, password and database selection is correct.

include("../connection/atechaircovers.php");<-- this file here.

kona72
10-23-2008, 03:42 PM
oh... my...
Sorry 'bout that. I had changed it so i could post the site and did't even think about changing it back to the local user/pass.

Thanks for catching that!

kona72
10-23-2008, 03:43 PM
We have a winner!!

After 2bz2p catch, the script works now!!
If I only knew why though... :(

2bz2p
10-23-2008, 04:24 PM
We have a winner!!

After 2bz2p catch, the script works now!!
If I only knew why though... :(

no problem, though Gavi might now why the script didnt work originally I didnt load the script.

2b

gavimobile
10-23-2008, 09:04 PM
haha... yes the configuration file must be correct!!! but what im not sure about is why dj stated that the original code was good!

gavimobile
10-23-2008, 09:20 PM
We have a winner!!

After 2bz2p catch, the script works now!!
If I only knew why though... :(
kona72, there are 2 functions i used, which helped me out so much as a newb.

isset() and empty()

i did if (isset($submit)){
my code here
}

which means.. if the submit button has been pressed basicly..
than start with the code...

we took out the id, becuase its auto incrementing.

you have 2 radio button fields, 1 with the value of yes and 1 with the value of no

so if one of them is selected that means its as if you typed something into that field

the elseif(empty($active)) {
}

empty( ) says that the field is not filled in.. its a great tool with my validations..

again

isset() = filled in
empty() not filled in

i hope this was helpful.. let me know if u didnt understand something!

gavi

davidj
10-23-2008, 09:29 PM
elseif ($active) should equal something something or it will always echo
echo "Empty Field Alert!";
do something like if ($active = 'no'){}


i never read the script sorry. Just glanced at it being at work.

the syntax is correct is what i meant

you can have
elseif ($active)
or
elseif (!$active)
without the need to test for equality (just testing for not null and null)

your example is incorrect though as you forgot a =

in your example your testing for equality so you need ==

you used a single = which is assignment. This will make the var always be true and ironically it will always echo ....

echo "Empty Field Alert!";

gavimobile
10-23-2008, 09:46 PM
luckly i know the difference between = and ==, but when did i include either of them?

i did if (isset($submit))
which means if $_POST['Submit'] has been submitted!
i lost ya buddy!

davidj
10-23-2008, 09:53 PM
luckly i know the difference between = and ==, but when did i include either of them?


http://www.dreamweaverclub.com/forum/showpost.php?p=147342&postcount=3

line 6 mate

gavimobile
10-23-2008, 09:59 PM
http://www.dreamweaverclub.com/forum/showpost.php?p=147342&postcount=3

line 6 mate
naaaaaaaaaaa not that... that was a sample idea. i changed his script later in the thread more thurally to what it should be. that = was to show that $active must equal something! but i agree with you 100% that sample was not valid... when i throw something into [ p h p ] [ / p h p ] tags within the forum than its a different story ;-)

davidj
10-24-2008, 06:15 AM
best to be accurate as people use your examples literally and will post again when they don't work

gavimobile
10-24-2008, 06:47 AM
best to be accurate as people use your examples literally and will post again when they don't work
gotcha!!!!:-)

davidj
10-24-2008, 07:33 AM
nice one mate