Page 1 of 1

Submit form to PHP

Posted: Thu Feb 10, 2011 12:00 am
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);
?>

Re: Submit form to PHP

Posted: Thu Feb 10, 2011 1:56 am
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]'");
}


Re: Submit form to PHP

Posted: Thu Feb 10, 2011 1:56 am
by requinix
But what if you did want to clear it out? Or is that never, ever going to be the case?

Re: Submit form to PHP

Posted: Thu Feb 10, 2011 3:13 am
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

Re: Submit form to PHP

Posted: Thu Feb 10, 2011 4:31 pm
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]'");
}