Jump to content

Database is not updating with new info


Go to solution Solved by Thunder_Wolf,

Recommended Posts

I have created a form that i can see the form input info in to the form boxes but once submitted it is not updating or adding new input to the database. I am trying to keep it as simple as possible. I am also new at PHP.

Here is my code:

    <?php
    // define variables and set to empty values
    $amp_20_parts_idErr = $part_numberErr = $locationErr = $quantityErr = "";
    $amp_20_parts_id = $part_number = $discription = $location = $quantity = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["amp_20_parts_id"])) {
     $amp_20_parts_idErr = "ID is required.";
    } else {
     $amp_20_parts_id= test_input($_POST["amp_20_parts_id"]);
    }
 
    if (empty($_POST["part_number"])) {
     $part_numberErr = "Part number is required.";
    } else {
     $email = test_input($_POST["part_number"]);
    }
  
     if (empty($_POST["description"])) {
     $descriptionErr = "";
    } else {
     $description = test_input($_POST["description"]);
    }
  
    if (empty($_POST["location"])) {
     $locationErr = "A location is required.";
    } else {
     $location = test_input($_POST["location"]);
    }

    if (empty($_POST["quantity"])) {
      $quantityErr = "Quantity is required";
    } else {
      $quantity = test_input($_POST["quantity"]);
    }
    }

    function test_input($data) {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
    return $data;
    }
    ?>
  
 <div id="update_form">
  Please follow instructions for updating the data base.<br>instructional text goes here.<br/><br/>
  
    <table><tr><form  method="POST" action="new path">
      <td>ID: <input name="amp_20_parts_id" type="text"><span><?php echo
    $amp_20_parts_idErr; ?></span><br/><br/></td>
      <td>Part Number: <input name="part_number" type="text"><span><?php echo
     $part_numberErr; ?></span><br/><br/></td>
      <td><label>Discription: <textarea name="description" rows="3" col="20"></textarea> 
     <br/><br/></td>
      <td>Location: <input name="location" type="text"><span><?php echo $locationErr;?> 
      </span><br/><br/></td>
      <td>Quantity: <input name="quantity" type="text"><span><?php echo $quantityErr; ?>
      </span><br/><br/></td><br/>
      <td><input type="submit"></td>
      </form></tr></table>
 
 
      <?php
         $con=mysqli_connect("server","user","password","db");

 // Check connection
    if (mysqli_connect_errno()) {
    echo("Connect failed: %s\n", mysqli_connect_error());

 

 // escape variables for security
    $amp_20_parts_id = mysqli_real_escape_string($con, $_POST['amp_20_parts_id']);
    $part_number = mysqli_real_escape_string($con, $_POST['part_number']);
    $discription = mysqli_real_escape_string($con, $_POST['description']);
    $location = mysqli_real_escape_string($con, $_POST['location']);
    $quantity = mysqli_real_escape_string($con, $_POST['quantity']);

     $sql="INSERT INTO amp_20 (amp_20_parts_id, part_number, description, location,
     quantity)
     VALUES ('$amp_20_parts_id', '$part_number', '$description', '$location',
    '$quantity')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
     }
     echo "1 record added";
   
 mysqli_close($con);
    }
    ?>

Link to comment
https://forums.phpfreaks.com/topic/291490-database-is-not-updating-with-new-info/
Share on other sites

You seem to be rusty on html as well.  Your input tags have no value= clause.

 

PS - imho - shorter var names are better.  Who needs to be typing 10 or 20 char var names again and again?  Too prone to error.  Plus - as a newbie you would be smart to turn on php error checking - it will come in handy when you do misspell one of the var names. 

You have a typo

 

$amp_20_parts_id = $part_number = $discription = $location = $quantity = "";//<- daisy chaining all to blank huh

 

$discription = mysqli_real_escape_string($con, $_POST['description']);

 

$sql="INSERT INTO amp_20 (amp_20_parts_id, part_number, description, location,
     quantity)
     VALUES ('$amp_20_parts_id', '$part_number', '$description', '$location',
    '$quantity')";

Edited by QuickOldCar

I did catch the typo and have corrected it,  I was thinking about shortining the amp_20_ var in the script and the data base, which I have done now as well as shorting the part num var as well, will test and see if it changes the input to where it will add to the data base.

Edited by Thunder_Wolf

<?php
 error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');
// define variables and set to empty values

$amp20ptidErr = $partnumErr = $locationErr = $quantityErr = "";
$amp20ptid = $partnum = $description = $location = $quantity = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["amp20ptid"])) {
     $amp20ptidErr = "ID is required.";
   } else {
     $amp20ptid= test_input($_POST["amp20ptid"]);
   }
 
   if (empty($_POST["partnum"])) {
     $partnumErr = "Part number is required.";
   } else {
     $partnum = test_input($_POST["partnum"]);
   }
   
     if (empty($_POST["description"])) {
     $descriptionErr = "";
   } else {
     $description = test_input($_POST["description"]);
   }
   
   if (empty($_POST["location"])) {
     $locationErr = "A location is required.";
   } else {
     $location = test_input($_POST["location"]);
   }

   if (empty($_POST["quantity"])) {
     $quantityErr = "Quantity is required";
   } else {
     $quantity = test_input($_POST["quantity"]);
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
        
    <div id="update_form">
        Please follow instructions for updating the data base.<br>
        1 - Please use the part number for the ID ie. part number is 20-1234, remove the hypen, so ID would be 201234,
        if it has been duplacted, message will show, <br>if this is the case add a 1 or next avalible number of greater value,
        ID can be no longer then 11 numbers.<br/>2 - Must put ONLY numbers in the Part ID section, please use numeric values for letters, IE. a=1, b=2, c=3, etc. No hyphens, commas or period can be used!<br/>
        3- All field MUST be filled out or you will get an error. The description can be lefts without any infomation in it but it is advised to enter a description before submiting.<br>
        
    <table><tr><form  method="POST" action="addnewpart.php">
      <td>ID: <input name="amp20ptid" type="text" value = ""><span><?php echo $amp20ptidErr; ?></span><br/><br/></td>
      <td>Part Number: <input name="partnum" type="text" value = ""><span><?php echo $partnumErr; ?></span><br/><br/></td>
      <td><label>Description: <textarea name="description" rows="1" col="1" ></textarea><br/><br/></td>
      <td>Location: <input name="location" type="text" value = ""><span><?php echo $locationErr; ?></span><br/><br/></td>
      <td>Quantity: <input name="quantity" type="text" value = ""><span><?php echo $quantityErr; ?></span><br/><br/></td><br/>
      <td><input type="submit" value="Submit form"></td>
</form></tr></table>
    
    
<?php
 error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');
         $con=mysqli_connect("localhost","scottk","ct@1446SA","part_inventory");
        
    // Check connection
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());


    // escape variables for security
$amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']);
$partnum = mysqli_real_escape_string($con, $_POST['partnum']);
$discription = mysqli_real_escape_string($con, $_POST['description']);
$location = mysqli_real_escape_string($con, $_POST['location']);
$quantity = mysqli_real_escape_string($con, $_POST['quantity']);

$sql = "INSERT INTO amp_20 (amp20ptid, partnum, description, location, quantity)
VALUES ('$amp20ptid', '$partnum', '$description', '$location', '$quantity')";
 
if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
            
    mysqli_close($con);
}
?>
Edited by mac_gyver
code tags around code please

Yes it is what i first typed, the description typo/misspelling was fixed but still not getting errors nor is it adding to the database like it should, i am feeling like its somethiung i am missing or something simple, but i will double check all the spelling and for typos

this is the only code i have to input the data to my DB and all the code i pasted is the html inputs and the PHP coding there is nothing more that handles the inputs, but here is just the php script :

<?php
 error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');
         $con=mysqli_connect("localhost","scottk","ct@1446SA","part_inventory");
        
    // Check connection
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());


    // escape variables for security
$amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']);
$partnum = mysqli_real_escape_string($con, $_POST['partnum']);
$discription = mysqli_real_escape_string($con, $_POST['description']);
$location = mysqli_real_escape_string($con, $_POST['location']);
$quantity = mysqli_real_escape_string($con, $_POST['quantity']);

$sql = "INSERT INTO amp_20 (amp20ptid, partnum, description, location, quantity)
VALUES ('$amp20ptid', '$partnum', '$description', '$location', '$quantity')";
 
if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
            
    mysqli_close($con);
}
?>

Edited by Thunder_Wolf

your form processing code has a misplaced } at the end that's part of the database connection error logic.

 

none of your database INSERT code is running, because it's inside the error handling conditional statement for your database connection. the INSERT code should be inside the form processing logic, where you have tested if the form was submitted and validated the data.

I do appalogize to all for wastinbg your time, with my poor exuse for scripting as i states i am new at php, but after seeing that I could add a hardcodrd values to my data base and my form inputs would not work no matter what I did, I have even tried a rescripted version still did not work, I do want to thank all that tried to help, wont be bothering you all with my scripts thank you

If you gave us the scripts that you actually run we would still be willing to help.  You just can't mislead us.  You have to realize we are trying to help but we are blind here if we can't see your code - good or bad.  Help us to help you by providing the MEANINGFUL code that RELATES to your problem next time.

the thing is we i have giving you the form code as well as the php code and there is nothing more I can give you but what I am running, I just dont know what else to give you, I have the php the pulls from my data base without issue working, I have even taken out the php code out of the main page just to see if that was causing my issues, and i was then starting to see error once that was cleaned up and it was saying "Entered data successfully" it stilll was not updating the database, I will put the php code in was I have now. but this will be what is showing "Entered data successfully". If you need my form code please let me know:

<?php
  $con = mysqli_connect("localhost","user","password","part_inventory");
    // Check connection
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} else {
    $result = mysqli_query($con, "SELECT * FROM amp20");
    
    // escape variables for security
$amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']);
$partnum = mysqli_real_escape_string($con, $_POST['partnum']);
$location = mysqli_real_escape_string($con, $_POST['location']);
$quantity = mysqli_real_escape_string($con, $_POST['quantity']);

$sql = "INSERT INTO 'part_inventory' . 'amp_20' ('amp20ptid', 'partnum', 'description', 'location', 'quantity')
VALUES('$amp20ptid', '$partnum', $_POST[description], '$location', '$quantity');";

if(! $result)
{
 die('Could not enter data: ' . mysql_error());    
}
 echo "Entered data successfully\n";
    mysqli_close($con);
}
?>

Edited by Thunder_Wolf
  • Solution

I just got it fixed:

Old code -  die('Could not enter data: ' . mysql_error());   
new code  - die('Could not enter data: ' . mysql_error($con));  

 

then change : old code - $sql = "INSERT INTO 'part_inventory' . 'amp_20' ('amp20ptid', 'partnum', 'description', 'location', 'quantity')

to new code - $sql = "INSERT INTO amp20 (amp20ptid, partnum, description, location, quantity)

i was able to put 2 new entries in to my data base.

Edited by Thunder_Wolf
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.