Posted by phillipmulvaney (phillipmulvaney), 28 May 2004
Hi
I want to add items to a database by clicking a submit button
code:
<?
if($submit)
{
$sql = "INSERT INTO shoppinglist3 (productname,price,tablename) VALUES ('$productname','$price','$table')";
$result = mysql_query($sql);
}
?>
<form method="post" action="">
<input type="Submit" name="submit" value="Submit">
</form>
this is the code so far but when the submit button is clicked nothing works.
any ideas would be great.
cheers,
Posted by admin (Graham Ellis), 28 May 2004
You need input boxes on your form for the productname, price and table ... all you're doing at the moment is submitting an empty form

I would also suggest that you use superglobals such as $_REQUEST[submit] rather than $submit .... unless you're using PHP 4.0.6 or earlier. As from PHP 4.2, the form that you've used will only work if the configuration has been changed to set register_globals on.
It may be that neither of the above is the direct cause of your problem; if you could describe more full how it fails, I'll have a better chance of analysing what's happening.
Posted by phillipmulvaney (phillipmulvaney), 31 May 2004
cheers for the reply,
in the program we are retrieving the productname and price from one database and then sending them to another database.
the code so far can send the information to the new database but it does so all the time.
i need to get it to only send the information to the new database if the "Submit" button has been pressed.
to do this I think it needs to be in a "if" statement.
Code:<form method="post" action=" " name=""> <input type="Submit" name="submit" value="Add to List">
</form>
<?
// think need "if" statement here $sql = "INSERT INTO shoppinglist (productname,price,tablename) VALUES ('$productname','$price','$table')"; $result = mysql_query($sql); // end "if" statement
?>
the statement: $sql = "INSERT INTO shoppinglist (productname,price,tablename) VALUES ('$productname','$price','$table')"; $result = mysql_query($sql);
sends the information to the database but i need to only send this if the Submit button has been pressed. any ideas on how to achieve this would be greatly appreciated. thanks again, Phillip
|
|
Posted by admin (Graham Ellis), 31 May 2004
Phillip,
I still don't see how you're getting the information from the first database into the variables $productname, $price and $table. That's probably a piece of code that you haven't posted, so I can't comment on it, nor on whether or not you need an if statement; I think the probably do (you had one originally) but it strikes me that either I'm not getting something (quite possible) or you have something of an unusual or problamiatic design.
Please - how DO you get the information into those variables??
Graham
Posted by phillipmulvaney (phillipmulvaney), 1 June 2004
it is a search bar. where a user enters the desired item into the search bar. the program then searches all the specified databases for the information and if it finds a match it shows the user the productname and the price.
I have this all working so far.
then if the user decides to add the item to their list they click the "Submit" button this should store the "productname", "price" and "tablename" values in a different database called "shoppinglist".
the problem is i cant get the button to work.
the code for submitting to the new database works but i cant get it to work when the button is pressed.
Code:$table;
if(!$_POST[productname]) { echo "Please enter an item or "; }
$db = mysql_connect("localhost", "harro", "eMHiGiTU" );
mysql_select_db("harro",$db);
$result = mysql_query("SELECT * from a WHERE productname = '$_POST[productname]' ",$db);
$num = mysql_num_rows($result); if($num == 1) { ?> <img src="mapa.gif" alt="" usemap="#mapa" style="border-style:none" /> <? $table = 'A'; }
if($num == 0) { $result = mysql_query("SELECT * FROM b WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result); if($num == 1) { ?> <img src="mapb.gif" alt="" usemap="#mapb" style="border-style:none" /><? $table = 'B'; } } if($num == 0) { $result = mysql_query("SELECT * FROM c WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapc.gif" alt="" usemap="#mapc" style="border-style:none" />
<? $table = 'C'; } }
if($num == 0) { $result = mysql_query("SELECT * FROM d WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapd.gif" alt="" usemap="#mapd" style="border-style:none" /><? $table = 'D'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM e WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mape.gif" alt="" usemap="#mape" style="border-style:none" />
<? $table = 'E'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM f WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapf.gif" alt="" usemap="#mapf" style="border-style:none" /><? $table = 'F'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM g WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapg.gif" alt="" usemap="#mapg" style="border-style:none" />
<? $table = 'G'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM h WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="maph.gif" alt="" usemap="#maph" style="border-style:none" /><? $table = 'H'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM i WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapi.gif" alt="" usemap="#mapi" style="border-style:none" />
<? $table = 'I'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM j WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapj.gif" alt="" usemap="#mapj" style="border-style:none" /><? $table = 'J'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM k WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapk.gif" alt="" usemap="#mapk" style="border-style:none" />
<? $table = 'K'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM l WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapl.gif" alt="" usemap="#mapl" style="border-style:none" /><? $table = 'L'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM m WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapm.gif" alt="" usemap="#mapm" style="border-style:none" />
<? $table = 'M'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM n WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapn.gif" alt="" usemap="#mapn" style="border-style:none" /><? $table = 'N'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM o WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapo.gif" alt="" usemap="#mapo" style="border-style:none" />
<? $table = 'O'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM p WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapp.gif" alt="" usemap="#mapp" style="border-style:none" /><? $table = 'P'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM q WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapq.gif" alt="" usemap="#mapq" style="border-style:none" />
<? $table = 'Q'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM r WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapr.gif" alt="" usemap="#mapr" style="border-style:none" /><? $table = 'R'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM s WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="maps.gif" alt="" usemap="#maps" style="border-style:none" />
<? $table = 'S'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM t WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapt.gif" alt="" usemap="#mapt" style="border-style:none" /><? $table = 'T'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM u WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapu.gif" alt="" usemap="#mapu" style="border-style:none" />
<? $table = 'U'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM v WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapv.gif" alt="" usemap="#mapv" style="border-style:none" /><? $table = 'V'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM w WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapw.gif" alt="" usemap="#mapw" style="border-style:none" />
<? $table = 'W'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM x WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapx.gif" alt="" usemap="#mapx" style="border-style:none" /><? $table = 'X'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM y WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapy.gif" alt="" usemap="#mapy" style="border-style:none" />
<? $table = 'Y'; }
}
if($num == 0) { $result = mysql_query("SELECT * FROM z WHERE productname = '$_POST[productname]'",$db); $num = mysql_num_rows($result);
if($num == 1) { ?> <img src="mapz.gif" alt="" usemap="#mapz" style="border-style:none" /><? $table = 'Z'; }
} if($num == 0) { echo "we do not stock the item! Please try another search. ";
}
while($myrow = mysql_fetch_array($result)){
printf("<b>Product: "); printf($myrow["productname"]); printf("<br>"); printf(" <b>Price:</b> "); printf($myrow["price"]);
} ?>
<form method="post" action="<?php echo 'customer.html';?>" name=""> <input type="Submit" name="submit" value="Add to List">
</form>
<? // this works but needs to be included in a button!! $sql = "INSERT INTO shoppinglist (productname,price,tablename) VALUES ('$productname','$price','$table')"; $result = mysql_query($sql);
?> |
|
when the button is clicked it needs to do the follwing code:
Code:$sql = "INSERT INTO shoppinglist (productname,price,tablename) VALUES ('$productname','$price','$table')"; $result = mysql_query($sql); |
|
sorry for the messy code!
any help would once again be appreciated
cheers,
phillip
This page is a thread posted to the opentalk forum
at
www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow
this link.