Submit form to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rt133813
Forum Newbie
Posts: 2
Joined: Wed Feb 09, 2011 11:54 pm

Submit form to PHP

Post by rt133813 »

My senario, please help if you can.

I input two fields(zip and id) to check against a MySql database table and then update the areacode field in the database. The code below works perfect. The problem is if I want to leave the areacode field blank, but still neeed to submit the form. If i leave the areacode field blank, then I DO NOT want it to update the areacode field in the database with white space. I want it to keep its original value as though i was never trying to update in the first place. In other words " IF areacode is NULL or blank on submit, then don't update the MySql database table"

Boy, would I appreciate some help on this one. Thanks in advance to anyone who could help.


Update.html

Code: Select all

<html>
<body>

<form action="update1.php" method="post">
Zip: <input type="text" name="zip" />
Id: <input type="text" name="id" />


Areacode: <input type="text" name="areacode" />
<input type="submit" />
</form>

</body>
</html> 
Update1.php

Code: Select all

<?php
							
	if (isset($_POST['areacode'])) {		

echo $_POST['areacode'];

$con = mysql_connect("my_ip,"my_database","my_password");
if (!$con)
  {
  die('Could not connect to Database Name: ' . mysql_error());
  }

mysql_select_db("my_database", $con);



// My Table is ricks_inserts
$result = mysql_query("SELECT * FROM ricks_inserts WHERE zip='$_POST[zip]'");



mysql_query("UPDATE ricks_inserts

							
	 SET areacode  = '$_POST[areacode]' 
	 WHERE zip = '$_POST[zip]' AND id = '$_POST[id]'");
	
}

mysql_close($con);
?>
sirholiday
Forum Newbie
Posts: 7
Joined: Wed Aug 15, 2007 8:41 am

Re: Submit form to PHP

Post by sirholiday »

Code: Select all

if($_POST["areacode"]!="") {
	mysql_query("UPDATE ricks_inserts
	SET areacode  = '$_POST[areacode]' 
	WHERE zip = '$_POST[zip]' AND id = '$_POST[id]'");
}

Last edited by sirholiday on Thu Feb 10, 2011 2:07 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Submit form to PHP

Post by requinix »

But what if you did want to clear it out? Or is that never, ever going to be the case?
rt133813
Forum Newbie
Posts: 2
Joined: Wed Feb 09, 2011 11:54 pm

Re: Submit form to PHP

Post by rt133813 »

sirholiday wrote:

Code: Select all

if($_POST["areacode"]!="") {
	mysql_query("UPDATE ricks_inserts
	SET areacode  = '$_POST[areacode]' 
	WHERE zip = '$_POST[zip]' AND id = '$_POST[id]'");
}



Problem solved, except if the space bar is used to clear field and not corrected with the back space key. If any white space still in field it over writes the database field with O
sirholiday
Forum Newbie
Posts: 7
Joined: Wed Aug 15, 2007 8:41 am

Re: Submit form to PHP

Post by sirholiday »

Code: Select all


if($_POST["areacode"]!="") {
        $areacode = trim($_POST["areacode"] );
        mysql_query("UPDATE ricks_inserts
        SET areacode  = '$_POST[areacode]' 
        WHERE zip = '$_POST[zip]' AND id = '$_POST[id]'");
}

Post Reply