This code sucks?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

This code sucks?

Post by MKEMouse »

<?php
// process form

//*****add_expense.php*****
//*****Script v. 1.0*****
//This page displays and handles a form for inserting records into expenses table.

//Include the MySQL information
require_once ("balfdfafadf.php");


if($submit) {

$result = mysql_query("INSERT INTO member_table (`l_name`, `f_name`, `m_name`, `street`, `city`, `state`, `zip`, `phone`, `email`) VALUES
('$l_name','$f_name','$m_name','$street','$city','$state','$zip','$phone','$email')");

echo "The record has been added.\n";
} else {
?>
<form method="post" name="theform" action="simplesubmit.php">
<TABLE border=0 cellPadding=0 width="100%">
<TBODY>
<TR bgColor=#8fbc8f>
<TD colSpan=3 align=center><B class="tdw">&nbsp;WQI Online</B></TD>
</TR>
<TR bgColor=#0066cc>
<TD colSpan=3><B class="tdw">&nbsp;<font color="#FFFFFF"><span class="tdb">MEMBER
INFORMATION </span></font></B></TD>
</TR>
<TR>
<TD width="24%" class="td">First Name:</TD>
<TD width="34%"> <input type="Text" name="f_name">
<span class="td1r">* </span></TD>
<TD width="20%">&nbsp;</TD>
</TR>
<TR>
<TD width="24%" class="td">Middle Name:</TD>
<TD width="34%"> <INPUT type="Text" name="m_name"> </TD>
<TD width="20%">&nbsp;</TD>
</TR>
<TR>
<TD width="24%" class="td">Last Name:</TD>
<TD width="34%"> <INPUT type="Text" name="l_name"> <span class="td1r">*</span> </TD>
<TD width="20%">&nbsp;</TD>
</TR>
<TR>
<TD width="24%" class="td">Street Address:</TD>
<TD width="34%"> <input type="text" name="street">
<span class="td1r"> *</span> </TD>
</TR>
<TR>
<TD height=25 width="24%" class="td">City:</TD>
<TD height=25 width="34%"> <input type="text" name="city">
<span class="td1r">*</span> </TD>
</TR>
<TR>
<TD width="24%" class="td">State:</TD>
<TD width="34%"> <select name=states size=1>
<option selected value="">[Select one]</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="IN">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NY</option>
<option value="NM">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select> <span class="td1r">*</span> </TD>
<TD rowSpan=3 width="20%">&nbsp;</TD>
</TR>
<TR>
<TD height=35 width="24%" class="td">Zip Code:</TD>
<TD height=35 width="34%"> <INPUT maxLength=10 name="zip" size=8> <span class="td1r">*</span>
</TD>
<TD height=35 width="22%">&nbsp;</TD>
</TR>
<TR>
<TD height=24 width="24%" class="td">Tel:</TD>
<TD height=24 width="34%"> <input name="phone" type="Text" id="phone">
<span class="td1r">* </span></TD>
<TD height=24 width="22%">&nbsp;</TD>
</TR>
<TR>
<TD height=27 width="24%" class="td"> Email:</TD>
<TD height=27 width="34%"> <input type="text" name="email">
<span class="td1r">* </span></TD>
<TD height=27 width="20%">&nbsp;</TD>
</TR>
</TBODY>
</TABLE>
<TABLE width=70%>
<TBODY>
<TR align=center>
<TD align=middle colSpan=5 vAlign=top> <BR>
<DIV align=center>
<INPUT name="submit" type="submit" value="Submit">
</DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</form>
<?php
}
?>


WHY DOES IT SUCK? I can retrieve records from MySQL Server database table and spit them out to an HTML table BUT cannot INSERT any field stuff into the table from the php code I created above. Where is the submitted data going to when I press the "submit" button, Siberia?

8O Any one like Doraemon I will send you some collectible stickers if you can figure this one out.

Thank you for at least viewing this.

Sincerely,

Milwaukee Mouse
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

You're sending the POST to "simplesubmit.php" and not the page with the form ... also, when you are checking for a button click, check for $_POST['submit'] and not $submit
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

protokol -->

Thanks for responding. simplesubmit.php is the name of the page with the form and form elements. I thought any time I call local values in the form I am supposed to name the variable reference "$___ ."

"check for $_POST['submit'] and not $submit."


I guess the only good thing that happened this weekend was that I got a free meal in Chinatown for my Aunt's Birthday. And that you responded so quickly.

Thanks

MKEMouse :P
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

if your php version is greater than 4.2.x then have a read of this: viewtopic.php?t=511
goes along with what protokol said
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

Thanks guys,

I spoke to the ISP that hosts the site and installed PHP and he said it is
version 4.2. He said that wouldn't impact what I am trying to do.
I looked at the link you sent but it doesn't relate to what is happening to this code. Unless I am naive and not a veteran so I don't know what I am reading ?

I'm in trouble :?


MKEMouse
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: This code sucks?

Post by protokol »

Code: Select all

if($submit) &#123;
Change this to if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submit'])) {

Code: Select all

$result = mysql_query("INSERT INTO member_table (`l_name`, `f_name`, `m_name`, `street`, `city`, `state`, `zip`, `phone`, `email`) VALUES  
('$l_name','$f_name','$m_name','$street','$city','$state','$zip','$phone','$email')");
The variables $l_name, $f_name, $m_name, etc. need to be $_POST['l_name'], $_POST['f_name'], $_POST['m_name'], etc.

Also, you have $state in your query, but in your HTML code, you declare the select dropdown as $states ... so change this one to $_POST['states']
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

Hi protokol,

Thanks for your suggestions. I tried the following:


$result = mysql_query("INSERT INTO member_table (`l_name`, `f_name`, `m_name`, `street`, `city`, `state`, `zip`, `phone`, `email`) VALUES
($_POST['l_name'],$_POST['f_name'],$_POST['m_name'],$_POST['street'],$_POST['city'],$_POST['state'],$_POST['zip'],$_POST['phone'],$_POST['email']");


And got the error:


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/wqinc/wqionline-www/simplesubmit.php on line 39 :?:

Am I really this clueless what did I miss?

Thanks again


MKEMouse
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

Hi protokol,

Thanks for your suggestions. I tried the following:


$result = mysql_query("INSERT INTO member_table (`l_name`, `f_name`, `m_name`, `street`, `city`, `state`, `zip`, `phone`, `email`) VALUES
($_POST['l_name'],$_POST['f_name'],$_POST['m_name'],$_POST['street'],$_POST['city'],$_POST['state'],$_POST['zip'],$_POST['phone'],$_POST['email']");


And got the error:


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/xxxxxx/xxxxx/simplesubmit.php on line 39 :?:

Am I really this clueless what did I miss?

Thanks again


MKEMouse
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

use this syntax:


{$_POST['variable']} instead of $_POST['variable']
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

:P protokol,

Thanks for the code suggestion. I tried it, the php file executed but did not update my table in the MySQL database. ??? I appreciate all your efforts, I have no idea what to do right now. I'm lost. 8O

MKEMouse
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

:evil: I give up! I am going back to ASP.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just in case you are willing to still take suggestions - try putting

Code: Select all

echo '&lt;pre&gt;';
print_r($_POST);
echo '&lt;/pre&gt;';
before the if statement at the top of your script, this will tell you what's in the $_POST array.

Also in your SQL statement all non-numeric values have to be surrounded by single quotes so try:

Code: Select all

$sql = "INSERT INTO member_table (`l_name`, `f_name`, `m_name`, `street`, `city`, `state`, `zip`, `phone`, `email`)";
$sql .= "VALUES ('{$_POST&#1111;'l_name']}', '{$_POST&#1111;'f_name']}', '{$_POST&#1111;'m_name']}', '{$_POST&#1111;'street']}', '{$_POST&#1111;'city']}', '{$_POST&#1111;'state']}', '{$_POST&#1111;'zip']}', '{$_POST&#1111;'phone']}', '{$_POST&#1111;'email']}'";
$result = mysql_query($sql) or die(mysql_error().'&lt;p&gt;'.$sql.'&lt;/p&gt;');
Mac
MKEMouse
Forum Newbie
Posts: 19
Joined: Sun Oct 20, 2002 10:41 pm
Location: Milwaukee

Post by MKEMouse »

I figured it out. Thanks for all those that lent a brain.

$sql = "INSERT INTO member_table (l_name, f_name, m_name, street, city, state, zip, phone, email) VALUES
('$l_name','$f_name','$m_name','$street','$city','$state','$zip','$phone','$email')";

$result =@mysql_query($sql) or die(mysql_error());


I didn't want to go to ASP and thankfully I won't be using Bill Gates' schlock anytime soon.


Thanks again

MKEMouse :D
Post Reply