/    Sign up×
Community /Pin to ProfileBookmark

Help fixing query that posts from listbox

I have a page that updates my database from a web form here is my code for the page:

[CODE]
<?
session_start();
include_once”config.php”;
if(!isset($_SESSION[‘username’]) || !isset($_SESSION[‘password’])){
header(“Location: login.php”);
}
else{
$fetch_users_data = mysql_fetch_object(mysql_query(“SELECT * FROM `members` WHERE username='”.$_SESSION[‘username’].”‘”));
}
?>
<html>
<head>
<title>Edit Member Account</title>
</head>

<body>
<?php
$name = $_POST[‘Business_Name’];
$address = $_POST[‘Business_Address’];
$city= trim($_POST[‘city’]);
$state= trim($_GET[‘state’]);
$zip= trim($_POST[‘zip’]);
$phone = $_POST[‘Phone’];
$fax = $_POST[‘Fax’];
$website = $_POST[‘website’];
$keywords = $_POST[‘Keywords’];
$user = $_SESSION[‘username’];

//Udates Information in the Database
$rc = mysql_affected_rows();
if ($name > ” && $address > ” && $phone > ”){
mysql_query(“UPDATE Chamber_Members SET Business_Name = ‘$name’, Business_Address = ‘$address’, City = ‘$city’, State = ‘$state’, Zip = ‘$zip’, Business_Phone = ‘$phone’, Business_Fax = ‘$fax’, Website = ‘$website’, Key_Words = ‘$keywords’ WHERE Username = ‘$user'”);}
if (mysql_query==false){
echo “Your Account Has Been Updated”;
}
else
{
echo “Your Account was not updated!”;
}
//Fills the form from the database
$query = (“select * FROM Chamber_Members WHERE Username=’$user'”);
$result=mysql_query($query) or die (‘Error: ‘.mysql_error ());
while($row = mysql_fetch_array($result))
{
//Displays current Information
$output .= “<h1>” . $row[‘Business_Name’] . “</h1>” . “<br />”;
$output .= $row[‘Business_Address’] . “<br />”;
$output .= $row[‘City’] . “,” . $row[‘State’] . “<br />”;
$output .= $row[‘Zip’] . “<br>”;
$output .= “Phone: ” . $row[‘Business_Phone’] . “<br />”;
$output .= “Fax: ” . $row[‘Business_Fax’] . “<br />”;

echo $output;
echo “<a href=” . trim($row[‘Website’]) . “” . “> ” . $row[‘Website’] . “</a>”;
}
?>

<form name=”form1″ method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’];?>”>

<div align=”left”><a>Welcome, You are logged in as <? echo ” “.$fetch_users_data->username.” “;?>, Click here if this is not you or to </a><a href=”logout.php”>Logout</a></div>
<div align=”right”>
<? $result = mysql_query( “select * FROM Chamber_Members WHERE Username=’$user'” );
while( $row = mysql_fetch_array( $result ) ) {
?>
<label>
<div align=”justify”><br>
Business Name:
<input name=”Business_Name” type=”text” id=”Business_Name” value=”<?php echo $row[‘Business_Name’];?>” tabindex=”1″ size=”40″>
</div>
</label>
<p align=”justify”>
<label>Address:
<input type=”text” name=”Address” id=”Address” value=”<?php echo $row[‘Business_Address’];?>”>
</label>
</p>
<p align=”justify”>
<label>City:
<input type=”text” name=”City” id=”City” value=”<?php echo $row[‘City’];?>”>
</label>
<label>State:
<select name=”State” size=”1″ id=”state”>
<option selected> <?php echo $row[‘State’];?> </option>
<option value “AL” > AL</option>
<option value “AK” > AK</option>
<option value “AZ” > AZ</option>
<option value “AR” > AR</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 “GA” > GA</option>
<option value “HI” > HI</option>
<option value “ID” > ID</option>
<option value “IL” > IL</option>
<option value “IN” > IN</option>
<option value “IA” > 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” > 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 “TN” > TN</option>
<option value “TX” > TX</option>
<option value “UT” > UT</option>
<option value “VT” > VT</option>
<option value “VI” > VI</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>
</label>
</p>
<p align=”justify”>
<label>Zip: </label>
<input type=”text” name=”Zip” id=”Zip” value=”<?php echo $row[‘Zip’];?>”>
</p>
<p align=”justify”>
<label>Phone:
<input name=”Phone” type=”text” id=”Phone” value=”<?php echo $row[‘Business_Phone’];?>” tabindex=”3″ size=”13″ maxlength=”13″>
</label><label>Fax:
<input name=”Fax” type=”text” id=”Fax” value=”<?php echo $row[‘Business_Fax’];?>”tabindex=”4″ size=”13″ maxlength=”13″>
</label>
</p>
<p align=”justify”>
<label>Website:
<input name=”website” type=”text” id=”website” value=”<?php echo $row[‘Website’];?>”tabindex=”5″ size=”50″>
</label>
</p>
<p align=”justify”>
<label>Keywords:
<textarea name=”Keywords” id=”Keywords” cols=”45″ rows=”5″ tabindex=”6″ wrap=”physical”><?php echo $row[‘Key_Words’];?> </textarea>
</label>
</p>

<p align=”justify”>
<label>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>
<input type=”submit” name=”Submit” id=”Submit” value=”Submit” tabindex=”7″>
</p>
</blockquote>
</blockquote>
</blockquote>
</form>
<?php
}
?>

</body>

</html>
[/CODE]

The problem is when I added the city, state, and zip, it no longer will update my db my guess is I am not getting the state information from the listbox correctly. can anyone help me troubleshoot this?

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperMay 04.2010 — Whenever you come across an issue like this, add some debugging code at the top to look at the contents of $_POST and see how data was submitted:

[code=php]echo var_dump($_POST);[/code]

Then, if necessary, do the same for the query (concert it to a string) to see the formatted query before it's actually written. Helps me alot!

A quick look at your code reveals you're using $_GET for state instead of $_POST and also the label closing tag is out of position. And you should keep the key names and form id/value names the same case.

-jim
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — Thank you for your reply, I am getting better at php but still have so much to learn. I had $_POST but I was tring $_GET to see if that helped but it didn't. I will try your echo and see what it looks like.
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — output from echo is as follows:
[CODE]
array(10) { ["Business_Name"]=> string(19) "Sayers Ace Hardware" ["Address"]=> string(10) "314 N Penn" ["City"]=> string(12) "Independence" ["State"]=> string(2) "KS" ["Zip"]=> string(5) "67301" ["Phone"]=> string(13) "(620)331-1690" ["Fax"]=> string(0) "" ["website"]=> string(18) "http://www.ace.com" ["Keywords"]=> string(31) "pluming electrical bathroom ku " ["Submit"]=> string(6) "Submit" } Your Account was not updated!
[/CODE]
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — I could not get the echo to work on the sql query probly had the syntax wrong or something
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 04.2010 — [code=php]["City"]=> string(12) "Independence" ["State"]=> string(2) "KS" ["Zip"]=> string(5) "67301"[/code]

As your debugging shows, the city, state and zip is stored properly in $_POST using the key names "City", "State" and "Zip". As this data is not blank, you move on to the next part of your code which creates the local variables that you end up using in your query. As a novice, you might var_dump those variables as well and compare with the $_POST debugging. If nothing obvious there, your query comes into play next. Convert that to a string, say $sql="...your query here..." so you can var_dump it, and see if it changed there as well. If not, then the issue is likely with the database itself, i.e. wrong column name or something - and of course PHP provides many debugging methods for MySQL error trapping and reporting which you can research on your own.

Welcome to PHP programming and debugging 101!

-jim
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — I have come down to it must be in the query, becasue the echo reports all my data as being correct all my varibles are correct but bool is true when I echo the query. I cannot find anything wrong in the query it's making me crazy. I know the db table is ok casue I am pulling all the correct information from it with the while statement and that part works fine. It has me compleatly baffled!
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 04.2010 — Re-read my next to last sentence in my previous reply!

[code=php]mysql_query("UPDATE Chamber_Members SET Business_Name = '$name', Business_Address = '$address', City = '$city', State = '$state', Zip = '$zip', Business_Phone = '$phone', Business_Fax = '$fax', Website = '$website', Key_Words = '$keywords' WHERE Username = '$user'");}
if (mysql_query==false){
echo "Your Account Has Been Updated";
[/code]


That just aint gonna cut it! :p

Time for Debugging 201...

You could do something like this to help you get to the root of the query issue:

[code=php]$sql="UPDATE Chamber_Members SET Business_Name = '$name', Business_Address = '$address', City = '$city', State = '$state', Zip = '$zip', Business_Phone = '$phone', Business_Fax = '$fax', Website = '$website', Key_Words = '$keywords' WHERE Username = '$user'";

echo "Query is: $sql";

$result = mysql_query ($sql) or die("Error " . mysql_errno( ) . " : " . mysql_error( ));[/code]


Get the idea? Up to you to adapt the above for your purposes in your code, don't just copy/paste. Now if the query has a syntax issue you will hear from MySQL which loves to complain if something is wrong! You can also use the debugging output of $sql to see the query.

Class dismissed, you're on your own from here! ?

-jim
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — I did as I was asked and get:

Query is: 1Error 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

as far as I can tell error 1064 is due to using a reserved word, such as date time ect. i don't think that city, state, or zip are reserved words.:rolleyes:
Copy linkTweet thisAlerts:
@criterion9May 04.2010 — Use back ticks () to get around reserved keywords.

<CODE lang="php">[code=php]<i>
</i>$sql="UPDATE
Chamber_Members SET Business_Name = '$name', Business_Address = '$address', City = '$city', State = '$state', Zip = '$zip', Business_Phone = '$phone', Business_Fax = '$fax', Website = '$website', Key_Words = '$keywords' WHERE Username` = '$user'";
[/code]
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — yeah I went back and did that since I posted the first code but it only changed the error code , Now it is as follows:

Query is: Error 1065 : Query was empty

Here is the current code as it is after Jim's help!
[code=php]<?
session_start();
include_once"config.php";
if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){
header("Location: login.php");
}
else{
$fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM members WHERE username='".$_SESSION['username']."'"));
}
?>
<html>
<head>
<title>Edit Member Account</title>
</head>

<body>
<?php
$name = $_POST['Business_Name'];

$address = $_POST['Business_Address'];
$city= $_POST['City'];
$state= $_POST['State'];
$zip= $_POST['Zip'];

$phone = $_POST['Phone'];
$fax = $_POST['Fax'];

$website = $_POST['website'];

$keywords = $_POST['Keywords'];
$user = $_SESSION['username'];
echo var_dump($_POST);
echo var_dump($name, $city, $state, $zip, $user);
//Udates Information in the Database
$rc = mysql_affected_rows();
if ($name > '' && $address > '' && $phone > ''){
$uptquery = mysql_query("UPDATE Chamber_Members SET Business_Name = '$name', Business_Address = '$address', City = '$city', State = '$state', Zip = '$zip', Business_Phone = '$phone', Business_Fax = '$fax', Website = '$website', Key_Words = '$keywords' WHERE Username = '$user'");}

echo "Query is: $uptquery";
$result = mysql_query ($uptquery) or die("Error " . mysql_errno( ) . " : " . mysql_error( ));
if (mysql_query==false){
echo "Your Account Has Been Updated";
}
else
{
echo "Your Account was not updated!";
}
//Fills the form from the database
$query = ("select * FROM Chamber_Members WHERE Username='$user'");
$result=mysql_query($query) or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result))
{
//Displays current Information
$output .= "<h1>" . $row['Business_Name'] . "</h1>" . "<br />";
$output .= $row['Business_Address'] . "<br />";
$output .= $row['City'] . "," . $row['State'] . "<br />";
$output .= $row['Zip'] . "<br>";
$output .= "Phone: " . $row['Business_Phone'] . "<br />";
$output .= "Fax: " . $row['Business_Fax'] . "<br />";

echo $output;
echo "<a href=" . trim($row['Website']) . "" . "> " . $row['Website'] . "</a>";
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

<div align="left"><a>Welcome, You are logged in as <? echo " ".$fetch_users_data->username." ";?>, Click here if this is not you or to </a><a href="logout.php">Logout</a></div>
<div align="right">
<? $result = mysql_query( "select * FROM Chamber_Members WHERE Username='$user'" );
while( $row = mysql_fetch_array( $result ) ) {
?>

<label>
<div align="justify"><br>
Business Name:
<input name="Business_Name" type="text" id="Business_Name" value="<?php echo $row['Business_Name'];?>" tabindex="1" size="40">
</div>
</label>
<p align="justify">
<label>Address:
<input type="text" name="Address" id="Address" value="<?php echo $row['Business_Address'];?>" tabindex="2">
</label>
</p>
<p align="justify">
<label>City:
<input type="text" name="City" id="City" value="<?php echo $row['City'];?>" tabindex="3">
</label>
<label>State:
<select name="State" size="1" id="State" tabindex="4">
<option selected> <?php echo $row['State'];?> </option>
<option value "AL" > AL</option>
<option value "AK" > AK</option>
<option value "AZ" > AZ</option>
<option value "AR" > AR</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 "GA" > GA</option>
<option value "HI" > HI</option>
<option value "ID" > ID</option>
<option value "IL" > IL</option>
<option value "IN" > IN</option>
<option value "IA" > 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" > 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 "TN" > TN</option>
<option value "TX" > TX</option>
<option value "UT" > UT</option>
<option value "VT" > VT</option>
<option value "VI" > VI</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>
</label>
</p>
<p align="justify">
<label>Zip: </label>
<input type="text" name="Zip" id="Zip" value="<?php echo $row['Zip'];?>" tabindex="5">
</p>
<p align="justify">
<label>Phone:
<input name="Phone" type="text" id="Phone" value="<?php echo $row['Business_Phone'];?>" tabindex="6" size="13" maxlength="13">
</label><label>Fax:
<input name="Fax" type="text" id="Fax" value="<?php echo $row['Business_Fax'];?>"tabindex="7" size="13" maxlength="13">
</label>
</p>
<p align="justify">
<label>Website:
<input name="website" type="text" id="website" value="<?php echo $row['Website'];?>"tabindex="8" size="50">
</label>
</p>
<p align="justify">
<label>Keywords:
<textarea name="Keywords" id="Keywords" cols="45" rows="5" tabindex="9" wrap="physical"><?php echo $row['Key_Words'];?> </textarea>
</label>
</p>

<p align="justify">
<label>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>
<input type="submit" name="Submit" id="Submit" value="Submit" tabindex="10">
</p>
</blockquote>
</blockquote>
</blockquote>

</form>
<?php
}
?>

</body>

</html>
[/code]
Copy linkTweet thisAlerts:
@criterion9May 04.2010 — Try this instead:
[code=php]
$uptquery = "UPDATE Chamber_Members SET Business_Name = '$name', Business_Address = '$address', City = '$city', State = '$state', Zip = '$zip', Business_Phone = '$phone', Business_Fax = '$fax', Website = '$website', Key_Words = '$keywords' WHERE Username = '$user'";

echo "Query is: $uptquery";
$result = mysql_query ($uptquery) or die("Error " . mysql_errno( ) . " : " . mysql_error( ));
[/code]

You were trying to use a result set as your query in the second mysql_query call.
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — Try this instead:

[php

$uptquery = "UPDATE Chamber_Members SET Business_Name = '$name', Business_Address = '$address', City = '$city', State = '$state', Zip = '$zip', Business_Phone = '$phone', Business_Fax = '$fax', Website = '$website', Key_Words = '$keywords' WHERE Username = '$user'";

echo "Query is: $uptquery";

$result = mysql_query ($uptquery) or die("Error " . mysql_errno( ) . " : " . mysql_error( ));

[/code]

You were trying to use a result set as your query in the second mysql_query call.
[/QUOTE]


That did help but I still am not getting the table to update

here is the output from the page:

[CODE]array(10) { ["Business_Name"]=> string(19) "Sayers Ace Hardware" ["Address"]=> string(16) "314 N. Penn Ave." ["City"]=> string(12) "Independence" ["State"]=> string(2) "KS" ["Zip"]=> string(5) "67301" ["Phone"]=> string(13) "(620)331-1690" ["Fax"]=> string(0) "" ["website"]=> string(26) "http://www.acehardware.com" ["Keywords"]=> string(31) "electrical pluming hardware ku " ["Submit"]=> string(6) "Submit" } string(19) "Sayers Ace Hardware" string(12) "Independence" string(2) "KS" string(5) "67301" string(12) "ace_hardware" Your Account was not updated!
[/CODE]
Copy linkTweet thisAlerts:
@criterion9May 04.2010 — Change this:

if (mysql_query==false){
[/quote]

to:
[code=php]
if ($result==false){
[/code]
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — [code=php]if ($result==false){ [/code]is working it is executing the [code=php]echo "Your Account Has Been Updated"; [/code]but the update is still not taking place
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 04.2010 — Problem is resolved I found the issue in the address part of the query it is working now!!!

Thanks to all your help

Great forum!
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 04.2010 — The key point being now you know how to better debug such issues. Mistakes like this happen all the time, but what separates the pros from the joe's is how they can quickly identify and resolve the actual problem!

Glad you got it all figured out! Special thanks, of course, to criterion9.

-jim
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 20.2010 — This has been resolved thanks for the help!
Copy linkTweet thisAlerts:
@ZABIMay 20.2010 — hey man u have some mistakes in your code

change :
[CODE]<input type="text" name="Address" id="Address" value="<?php echo $row['Business_Address'];?>" tabindex="2">[/CODE]
to :
[CODE]<input type="text" name="Business_Address" id="Address" value="<?php echo $row['Business_Address'];?>" tabindex="2">[/CODE]
and change :
[code=php]if ($name > '' && $address > '' && $phone >'')[/code]
to :
[code=php]if ($name != '' && $address != '' && $phone != '')[/code]
Copy linkTweet thisAlerts:
@rebelweb2007May 24.2010 — When entering data from a form into a database use the add_slashes() function. It will automatically make escape sequences for quotes, for example which would mess up your sql statement. I had a similar problem before and i put my sql statement (SELECT * from table) whatever your doing into a variable ($sql) and once the data is inserted you can echo out the sql statement and see where the problem lies. Here is somewhere else to check.
×

Success!

Help @hotrodjeremy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...