Jump to content

0s instead of blanks/nulls in input form


ericamcampbell

Recommended Posts

I have an input form in php that saves to mysql, when I leave blank fields they save as zeros... Anyway to go around that? Here is the code:

 

<?php

 

//start the session

session_start();

 

//Get variables from SESSION

$user = $_SESSION['username'];

$pass = $_SESSION['password'];

 

//Connect to MySQL

$db = mysql_connect('*', '*', '*') or die ("Error Connecting to MySQL: " . mysql_error());

 

//Connect to DB

mysql_select_db("transport", $db) or die ('<font color="red"><h1>Could not select the database: </h1></font>' . mysql_error());

 

//Check for connection

If ($db) {

echo 'Connected to Database: ';

echo $db . "<br />";

}

else {

echo'<font color="red"><h1>Cannot connect to MySQL Database.</h1></font>';

header("Refresh: 2; URL=index.shtml");

}

 

//Query database

$result = mysql_query("SELECT * FROM transport.users WHERE username='$user' AND password='$pass'", $db) or die ('<font color="red"><h1>Could not select the database: </h1></font>' . mysql_error()); 

 

//submit not clicked - show input form

if (!$_POST['submit']) {

 

//check for results

if (mysql_num_rows($result) == 1) {

//get the array of data

$data = mysql_fetch_array($result);

} else {

header ("Refresh: 2; URL=index.shtml");

echo ('There were no results from your username and password, relogin...');

exit();

}

 

//show form...

?>

 

<table border="1">

<form method="POST" action="">

<tr><td align="center" class="header" colspan="2">Edit  Information</td></tr>

<tr><td>Username:</td>

<td><input type="text" name="username" value="<?php echo $data['username'] ?>" disabled="disabled" size="25" maxlength="255"  /></td></tr>

<tr><td>Extension:</td>

<td><input type="text" name="extension" value="<?php echo $data['extension'] ?>" size="4" maxlength="4"/>

(####)</td>

</tr>

<tr><td>Cell Phone:</td>

<td><input type="text" name="cell_phone" value="<?php echo $data['cell_phone'] ?>" size="12" maxlength="12" />

(###-###-####)</td>

</tr>

<tr><td>Home Phone:</td>

  <td><input type="text" name="home_phone" value="<?php echo $data['home_phone'] ?>" size="12" maxlength="12" />

(###-###-####)</td>

</tr>

<tr><td>Fax:</td>

  <td><input type="text" name="fax" value="<?php echo $data['fax'] ?>" size="12"  maxlength="12"/>

(###-###-####)</td>

</tr>

<tr><td>Group Extension:</td>

<td><input type="text" name="group_extension" value="<?php echo $data['group_extension'] ?>" size="4" maxlength="4" />

(####)</td>

</tr>

<tr><td>After Hours:</td>

<td><input type="text" name="after_hours" value="<?php echo $data['after_hours'] ?>" size="2" maxlength="2" />

(#)</td>

</tr>

<tr><td>Title:</td>

<td><input type="text" name="title" value="<?php echo $data['title'] ?>" size="25" maxlength="255" /></td></tr>

<tr><td>Birthday:</td>

<td><input type="text" name="birthday" value="<?php echo $data['birthday'] ?>" size="10" maxlength="10" /> (MM/DD/YYYY)</td>

</tr>

<tr><td>Address:</td>

<td><input type="text" name="address" value="<?php echo $data['address'] ?>" size="25" maxlength="255" /></td></tr>

<tr><td>City:</td>

<td><input type="text" name="city" value="<?php echo $data['city'] ?>" size="25" maxlength="255" /></td></tr>

<tr><td>State:</td>

<td><input type="text" name="state" value="<?php echo $data['state'] ?>" size="2" maxlength="2" />

(AA)</td>

</tr>

<tr><td>Zip:</td>

<td><input type="text" name="zip" value="<?php echo $data['zip'] ?>" size="5" maxlength="5" />

(#####)</td>

</tr>

<tr><td colspan="2" align="center"><input type="submit" value="submit" name="submit" />

<input type="reset" value="clear" name="clear" /></td></tr>

</form>

</table>

<?php

} else {

//if submit clicked...

 

//get variables from form

$extension = $_POST['extension'];

$cell_phone = $_POST['cell_phone'];

$home_phone = $_POST['home_phone'];

$fax = $_POST['fax'];

$group_extension = $_POST['group_extension'];

$after_hours = $_POST['after_hours'];

$title = $_POST['title'];

$birthday = $_POST['birthday'];

$address = $_POST['address'];

$city = $_POST['city'];

$state = $_POST['state'];

$zip = $_POST['zip'];

 

 

//run update query

$update = mysql_query("UPDATE transport.users SET extension='$extension', cell_phone='$cell_phone', home_phone='$home_phone', fax='$fax', group_extension='$group_extension', after_hours='$after_hours', title='$title', birthday='$birthday', address='$address', city='$city', state='$state', zip='$zip' WHERE username='$user' AND password='$pass'", $db) or die ('<font color="red"><h1>Could not update the database: </h1></font>' . mysql_error());

 

if ($update) {

header ("Refresh: 2; URL=editinfo.php");

echo "Updated" . '<br />';

echo $update . '<br />';

echo $extension . '<br />';

echo $cell_phone . '<br />';

echo $home_phone . '<br />';

echo $fax . '<br />';

echo $group_extension . '<br />';

echo $after_hours . '<br />';

echo $title . '<br />';

echo $birthday . '<br />';

echo $address . '<br />';

echo $city . '<br />';

echo $state . '<br />';

echo $zip . '<br />';

}

else {

header ("Refresh: 2; URL=editinfo.php");

echo "Error Updating";

}

 

}

 

?>

Link to comment
Share on other sites

Hi. Did you look at your database table? Does it have a default of a bunch of zeros (like a timestamp that is set to not be empty by default under the NULL table header in PHPMyAdmin)?

Also, you could probably defeat this by checking isset() or empty() on your variable and writing the value of NULL in place of an empty string...? Assuming it is a field you want to allow to be NULL in your error checking.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.