Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Php is changing values on it's own
New Posts  All Forums:Forum Nav:

Php is changing values on it's own

post #1 of 8
Thread Starter 
Ok I have this php code right here. Basically what it does is takes values from another page with text boxes using the post method. When it gets to this page (below) It does all kinds of checking such as making sure the fields are filled it, checking to see if the user name is already taking and etc.

But Im having a problem where when it comes to inserting a value into the database, the code then decides to insert a random number of it's own. My unique value for the database is the ssn. It takes ssn from the previous page which is an html text box. When it gets down to the adding the posted values in ssn will change from what ever i put in to a random value such as 5 even though what is passed is 123456789. I know it's doing that because the mysql_error pops saying there is a duplicate key for ssn '5' even though that is not what I typed in.

There is a 5 in the database because it switched on it's own the first time. The database is for the ssn field is not set to have a default value. Why is it not adding my values to the database correct and changing my unique value on it's own?
Code:



<?php

 // Connects to your Database 

 mysql_connect("localhost", "root", "") or die(mysql_error()); 

 mysql_select_db("project") or die(mysql_error());

 //This code runs if the form has been submitted

                      if (isset($_POST['add'])) 
            { 
   


//This makes sure they did not leave any fields blank

                      if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['ssn']| !$_POST['phone']| !$_POST['firstname']| !$_POST['lastname']| !$_POST['salary']| !$_POST['dept']| !$_POST['position']| !$_POST['supervisor']) {
         die('You did not complete all of the required fields. <a href = "javascript:history.back()">Back to previous page</a>');
                 }



 // checks if the username is in use

     if (!get_magic_quotes_gpc()) {

         $_POST['username'] = addslashes($_POST['username']);

     }

 $usercheck = $_POST['username'];

 $check = mysql_query("SELECT username FROM employee WHERE username = '$usercheck'") 

or die(mysql_error());

 $check2 = mysql_num_rows($check);



 //if the name exists it gives an error

 if ($check2 != 0) {

         die('Sorry, the username '.$_POST['username'].' is already in use. ');

                 }


 //this makes sure both passwords entered match

     if ($_POST['pass'] != $_POST['pass2']) {

         die('Your passwords did not match. ');

     }






     // here we encrypt the password and add slashes if needed
        

     $_POST['pass'] = md5($_POST['pass']);

     if (!get_magic_quotes_gpc()) {

         $_POST['pass'] = addslashes($_POST['pass']);

         $_POST['username'] = addslashes($_POST['username']);

             }





  // now we insert it into the database

     $insert = "INSERT INTO employee (username, password, ssn, salary, dept, firstname, lastname, phone, sex, position, supervisor)

             VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['ssn']."' ,'".$_POST['salary']."','".$_POST['dept']."','".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['phone']."','".$_POST['sex']."','".$_POST['position']."','".$_POST['supervisor']."')";

     $add_member = mysql_query($insert);

if(!$add_member){ die(mysql_error());}

     ?>



<?php header("Refresh: 3; url=\\"admin.php"");
 
echo "<h1>Employee Has Been Added</h1>";

echo '<p>Redirecting you back to admin page...<a href ="admin.php"> Click here</a> if you are not redirected automatically </a>.</p>';
?>
<?php 
 } 

 ?>
post #2 of 8
Thread Starter 
I also tried $ssn = $_POST['ssn']; along with changing the insert statement which is basically the same thing but instead I get 0 now as the value.
post #3 of 8
Are you sure the form field is named ssn in your HTML code?
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #4 of 8
Thread Starter 
Yep here is that portion of the HTML. It worked about 2 weeks ago with no issues. Now the only change I made was

$salary = str_replace(",", "", $_POST['salary']);

to get rid of commas in the salary entered and thats when ssn value started screwing up on me.

Code:
<h1>Add a new employee</h1>
<hr>
Use this form to add a new employee. All information is <B>REQUIRED</B>.<br />
This form checks to make sure all fields are populated.
<hr> 
 <form action="addemployee.php" method="post">

 <table border="0">
<tr><td>Username:</td><td>

 <input type="text" name="username" maxlength="60">

 </td></tr>

 <tr><td>Password:</td><td>

 <input type="password" name="pass" maxlength="10">

 </td></tr>

 <tr><td>Confirm Password:</td><td>

 <input type="password" name="pass2" maxlength="10">

 </td></tr>

 <tr><td>Social Security:</td><td>

 <input type="text" name="ssn" maxlength="60">

 </td></tr>

 <tr><td>Salary:</td><td>

 <input type="text" name="salary" maxlength="60">

 </td></tr>

 <tr><td>Dept:</td><td>

 <input type="text" name="dept" maxlength="60">

 </td></tr>

 <tr><td>First Name:</td><td>

 <input type="text" name="firstname" maxlength="60">

 </td></tr>

 <tr><td>Last Name:</td><td>

 <input type="text" name="lastname" maxlength="60">

 </td></tr>


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

 <input type="text" name="phone" maxlength="60">

 </td></tr>

 <tr><td>Sex:</td><td>

 <select name = "sex">
<option>Male</option>
<option>female</option>
</select>
 </td></tr>

 <tr><td>Position:</td><td>

 <input type="text" name="position" maxlength="60">
 </td></tr>

 <tr><td>Supervisor:</td><td>

 <input type="text" name="supervisor" maxlength="60">
 </td></tr>

 <tr><td><input type = "submit" name = "add" value = "Add employee"/></td></tr>
</table>
 </form>
I took out the string replace and it stays broken until I load a backup of that table and that php page.
post #5 of 8
If that's the case, then I suspect the problem is in a section of code you haven't posted. Can you post the whole thing?
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
Underground
(14 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 920 C0 ASUS P6T6 WS Revolution GTX 460 TR3X6G1600C8D 
Hard DriveOptical DriveCoolingOS
WD1001FALS SAMSUNG SH-S223F 22X DVD MULTI Corsair H50 Fedora 16 KDE x86_64 
MonitorKeyboardPowerCase
HP w19b Microsoft Comfort Curve Corsair CX600 Thermaltake Armor VA8003BWS 
MouseMouse Pad
Razer DeathAdder Black 
  hide details  
Reply
post #6 of 8
Please post your database table scheme.

......

I won't get into the addslashes() versus mysql_real_escape_string() debate since I know this is just a script you found on the internet and modified it for your purpose. Nothing wrong with that either.
post #7 of 8
Please post your SQL table it's most likely a problem in there. Is ssn a primary key in your table? What data type did you use for ssn in your table?
baby cakes
(13 items)
 
Macbook 4,1
(13 items)
 
 
CPUMotherboardGraphicsRAM
i7 930 [4.01Ghz vCore 1.28125 HT on] GA-X58A-UD3R ZOTAC GTX 460 1GB SLI [840/1680/1900 @stock volts] CORSAIR XMS3 6GB (3 x 2GB) 1600Mhz 
Hard DriveOptical DriveOSMonitor
C300 64GB SATA III, 2x F3 1TB 7200rpm RAID 0 Sony Optiarc 24X DVD/CD Ubuntu 14.04 22" Dell SP2009W 1680x1050 
PowerCase
CORSAIR HX 750W Cooler Master HAF 922 
CPUMotherboardGraphicsRAM
Core 2 Duo 2.4Ghz Penryn T8300 Some logical board GMA X3100 4GB (2x2GB) DDR2 667Mhz 
Hard DriveOptical DriveOSMonitor
Hitachi 160 GB 5400 RPM SATA II Combo drive OSX 10.6.4 (Snow leopard) 13.3" 1280x800 TFT 
PowerCase
Dying battery White macbook case 
  hide details  
Reply
baby cakes
(13 items)
 
Macbook 4,1
(13 items)
 
 
CPUMotherboardGraphicsRAM
i7 930 [4.01Ghz vCore 1.28125 HT on] GA-X58A-UD3R ZOTAC GTX 460 1GB SLI [840/1680/1900 @stock volts] CORSAIR XMS3 6GB (3 x 2GB) 1600Mhz 
Hard DriveOptical DriveOSMonitor
C300 64GB SATA III, 2x F3 1TB 7200rpm RAID 0 Sony Optiarc 24X DVD/CD Ubuntu 14.04 22" Dell SP2009W 1680x1050 
PowerCase
CORSAIR HX 750W Cooler Master HAF 922 
CPUMotherboardGraphicsRAM
Core 2 Duo 2.4Ghz Penryn T8300 Some logical board GMA X3100 4GB (2x2GB) DDR2 667Mhz 
Hard DriveOptical DriveOSMonitor
Hitachi 160 GB 5400 RPM SATA II Combo drive OSX 10.6.4 (Snow leopard) 13.3" 1280x800 TFT 
PowerCase
Dying battery White macbook case 
  hide details  
Reply
post #8 of 8
use Netbeans and XDEBUG to debug your SSN before inserting, or try printing your SSN to a page before inserting so you can see what the value is. if all is alright, post your database schema....
Outdated
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8200 2.6 Ghz Asrock g31M-S R2 ATi 3870 2GB Kingston Value RAM 
Hard DriveOptical DriveOSMonitor
2TB WD Green+ 640GB WD Blue None Win 7 Ultimate 21.5 Dell 
KeyboardPowerCaseMouse
HP Wireless Keyboard 550W Cooler Master None Logitech cheap mouse 
Mouse Pad
none 
  hide details  
Reply
Outdated
(13 items)
 
  
CPUMotherboardGraphicsRAM
E8200 2.6 Ghz Asrock g31M-S R2 ATi 3870 2GB Kingston Value RAM 
Hard DriveOptical DriveOSMonitor
2TB WD Green+ 640GB WD Blue None Win 7 Ultimate 21.5 Dell 
KeyboardPowerCaseMouse
HP Wireless Keyboard 550W Cooler Master None Logitech cheap mouse 
Mouse Pad
none 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Php is changing values on it's own