Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > database update question: capturing data from a form

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-14-07, 22:59
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
database update question: capturing data from a form

My apologies since this is not a strictly mysql question . . . more php.

I'm trying to update a database by building a form that links a city with surrounding towns. I want to enter the main town and the surrounding towns and then it updates all records that contain the main town. the code runs without errors, but the data i'm entering is not finding its way into the variables and doesn't update the database . . . all help appreciated.

<form id="form1" name="form1" method="get" action="demograph_updatep2.php">
<label><strong class="innerpageheading">Main town</strong>
<input name="maintown" type="text" id="maintown" />
</label>
<p><label><span class="results_13px">Nearby Town1</span>
<input name="nearbytown1" type="text" id="nearbytown1" />
</label>
</p>
<p>
<label><span class="results_13px">NearbyTown2</span>
<input name="nearbytown2" type="text" id="nearbytown2" />
</label>
</p>
<p>
<label><span class="results_13px">NearbyTown3</span>
<input name="nearbytown3" type="text" id="nearbytown3" />
</label>
</p>
<p>
<label><span class="results_13px">NearbyTown4</span>
<input name="nearbytown4" type="text" id="nearbytown4" />
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Update" />
</label>
</p>
</form>


<?php

// setting variables

$maincity = $_GET['maintown'];
$nearbycity1 = $_GET['nearbytown1'];
$nearbycity2 = $_GET['nearbytown2'];
$nearbycity3 = $_GET['nearbytown3'];
$nearbycity4 = $_GET['nearbytown4'];


mysql_connect (localhost,"");
mysql_select_db("") or die( "Unable to select database");
mysql_query("UPDATE physician
SET

physician_City2 = $nearbycity1
WHERE physician_City1 IN ($maincity)

mysql_close();
?>

as you can see, i've left out some of the db details. i've tried this with GET and POST but no success. Suggestions please . . .
Reply With Quote
  #2 (permalink)  
Old 06-15-07, 06:07
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Is the whole script called demograph_updatep2.php, or is it split up into different script names?
Can you echo out the $_GET/$_POST variables and post what you're getting
e.g. insert the following into your code somewhere...
Code:
<?php print_r($_GET); ?>
Reply With Quote
  #3 (permalink)  
Old 06-15-07, 07:12
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
Hi,
its split into the first page demo_update.php, which contains the code you saw. and a second page, which just displays results. i went ahead and inserted your code at the end of the page along with

echo $nearbytown1;
echo $maintown

and got this:

Array ( )
Reply With Quote
  #4 (permalink)  
Old 06-15-07, 07:23
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
When you submit the form you're going to demograph_updatep2.php
So basically anything at the bottom of demo_update.php is not getting executed properly. Your update SQL and PHP code needs to be put into the demograph_updatep2.php becuase that's where all your data you put into the boxes is being sent...
Reply With Quote
  #5 (permalink)  
Old 06-15-07, 07:40
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
yes, i tried that but it still doesn't find its way into the database . . .
Reply With Quote
  #6 (permalink)  
Old 06-15-07, 10:18
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Are you connecting to the Db properly? are you getting an error?
Put the

<?php
print_r($_GET);
?>

code onto the page you're submitting the form to.

All the following code needs to be on the page the form is submitting to :

<?php

// setting variables

$maincity = $_GET['maintown'];
$nearbycity1 = $_GET['nearbytown1'];
$nearbycity2 = $_GET['nearbytown2'];
$nearbycity3 = $_GET['nearbytown3'];
$nearbycity4 = $_GET['nearbytown4'];


mysql_connect (localhost,"");
mysql_select_db("") or die( "Unable to select database");
mysql_query("UPDATE physician
SET

physician_City2 = $nearbycity1
WHERE physician_City1 IN ($maincity)

mysql_close();
?>

How about outputting the contents of
mysql_error() ....
Reply With Quote
  #7 (permalink)  
Old 06-15-07, 16:14
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
Its definitely doing the GET to the second page. With the echo I can see the entered variable values across the top of the page.

mysql_error();

does not display anything.

Think I'm missing something basic here . . .
Reply With Quote
  #8 (permalink)  
Old 06-15-07, 16:15
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
also, i moved all that code to the page the GET is outputting to.
Reply With Quote
  #9 (permalink)  
Old 06-18-07, 05:05
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Ok, let's do a nice simple example :

page1.php :
Code:
<html> <body> <form name="myform" method="POST" action="page2.php"> Main Town : <input name="maintown" type="text" /> Town 1 : <input name="nearbytown1" type="text" /> Town 2 : <input name="nearbytown2" type="text" /> Town 3 : <input name="nearbytown3" type="text" /> Town 4 : <input name="nearbytown4" type="text" /> <input type="submit" name="Submit" value="Onward!" /> </form> </body> </html>

page2.php
Code:
<html> <body> I have reached this page and the output is <pre> <?php print_r($_POST); ?> <pre> </body> </html>

What does that give you?
Reply With Quote
  #10 (permalink)  
Old 06-18-07, 08:35
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
I have reached this page and the output is

Array
(
[maintown] => chicago
[nearbytown1] => naper
[nearbytown2] =>
[nearbytown3] =>
[nearbytown4] =>
[Submit] => Onward!
)
Reply With Quote
  #11 (permalink)  
Old 06-18-07, 09:56
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Ok, that's lovely.
So you can now see that the form you filled in is passing those fields through from page1.php to page2.php and it's writing out the results so you can see them.

Now, here's a test :
Can you tell me where you should be putting your SQL statement (+ db connection + query)?
i.e. on which page?
Reply With Quote
  #12 (permalink)  
Old 06-18-07, 15:25
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
the database connection I placed on p2, after the information is posted.
Reply With Quote
  #13 (permalink)  
Old 06-19-07, 05:50
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Ok so you're saying that this code :
Code:
<?php // setting variables $maincity = $_GET['maintown']; $nearbycity1 = $_GET['nearbytown1']; $nearbycity2 = $_GET['nearbytown2']; $nearbycity3 = $_GET['nearbytown3']; $nearbycity4 = $_GET['nearbytown4']; mysql_connect (localhost,""); mysql_select_db("") or die( "Unable to select database"); mysql_query("UPDATE physician SET physician_City2 = $nearbycity1 WHERE physician_City1 IN ($maincity) mysql_close(); ?>

goes on page2.php ?

Have you see the SQL you have put there? What happens if the value for $maincity doesn't exist in the physician_City1 column?
Reply With Quote
  #14 (permalink)  
Old 06-19-07, 07:41
hconnor hconnor is offline
Registered User
 
Join Date: Feb 2007
Posts: 34
the sql doesn't work, at least it doesn't update the db. but doesn't throw an error message as written. it looks like this:

mysql_connect (localhost,"","");
mysql_select_db("") or die( "Unable to select database");
mysql_query("UPDATE physician




SET
Physician_City2=$maintown
WHERE Physician_City1 = $nearbytown1


");
mysql_error();
mysql_close();
?>

for some reason the variable is not being passed into the sql. could it have to do with the way the sql statement is written?

Last edited by hconnor : 06-19-07 at 07:49.
Reply With Quote
  #15 (permalink)  
Old 06-19-07, 08:52
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
I think you need to echo mysql_error();
Nevertheless, the SQL is not necessarily failing. Think it through, you are updating some columns where one of those columns equals a particular value,
e.g. update <tablename> SET column2='somenewvalue',column3='somenewvalue' WHERE column1 IN ('somevalue')

(as a side note there is nothing wrong with using IN but I would use = , because what is inside the bracket is constant)

So what does this do? Well, it updates column2 & 3 where column1 = 'somevalue'.

[I'm MySQL]
Ok, i'm MySQL, i'm searching through all the rows in my table, and I find three rows that have column1='somevalue'. Ok great, i'll update column2 and 3 for those rows then. Done.

Different scenario
[I'm MySQL]
I'm searching through all the rows in my table, and i find zero (0) rows that have column1='somevalue'. What do I have to update? Nothing...! So I don't update anything. Done.

Can you see something here?
There is no error. MySQL has searched through our results and hasn't found any to update so it doesn't update any. That's a perfectly valid result.

Your problem is not that your SQL is wrong, it's that you don't have anything in the table to update...
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On