How to insert value into MySQL table with PHP

29

I have a table [user_id, name] that is prepopulated with a list of user_IDs. I want to create a form to allow a user to register a name against their user_id.

Here's the basic HTML form:

  <form action="" id="contact-form" method="post">
     <label for="inputName">Enter Name</label>
     <input type="text" name="inputName" id="inputName">    
      <input type="hidden"  id="hidUid" name="hidUid" value="<?php echo $reg_user_id?>">
<input type="submit" id="sub_button" value="Save"/>
</form>

Note the $reg_user_id is the user_id variable set by a session.

Here is my start at the PHP:

if(isset($_POST['hidUid']) && isset($_POST['inputName'])){
  $inputName=$_POST['inputName'];
  $inputUid=$_POST['hidUid'];
  if ($stmtint = $mysqli->prepare("SELECT user_email FROM users WHERE user_ID=?")) {
   $stmtint->bind_param("s", $inputUid);
   $stmtint->execute();
   $stmtint->bind_result($user_email);
   $stmtint->fetch();
   $stmtint->close();
   if($user_email != "" && $inputName !=""){
    $user_email="";
    if ($stmtint = $mysqli->prepare("UPDATE users SET name=? where user_ID=?")) {
     $stmtint->bind_param("ss", $inputName,$inputUid);
     $stmtint->execute();
     $stmtint->close();        

      echo "User saved successfully";
      exit;
    }
  }
  else{
    echo "Please enter correct Name and password";
    exit;
  }

}
}

My problem is the form submits successfully, but the database doesn't update. I have no errors in either the PHP output or the console log. Any ideas?

975

Answer

Solution:

If you want to see the error try this.

if(isset($_POST['hidUid']) && isset($_POST['inputName'])){

 $inputName = $_POST['inputName'];
 $inputUid = $_POST['hidUid'];

 if ($stmtint = $mysqli->prepare("SELECT user_email FROM users WHERE user_ID = ?")) {

   $stmtint->bind_param("s", $inputUid);

   if ($stmtint->execute()) {
     $stmtint->bind_result($user_email);
     $stmtint->fetch();
     $stmtint->close();
   }else{
     die("Error Message:".$mysqli->error);
   }

   if ($user_email != "" && $inputName != "") {

     $user_email="";

     if ($stmtint = $mysqli->prepare("UPDATE users SET name = ? where user_ID = ?")) {

       $stmtint->bind_param("ss", $inputName,$inputUid);

       if ($stmtint->execute()) {
         $stmtint->close();
         echo "User saved successfully";           
       }else{
         die("Error Message:".$mysqli->error);
       }

       exit;
     }

   }else{
   echo "Please enter correct Name and password";
   exit;
   }
 }
}

People are also looking for solutions to the problem: php - Better way to do this subquery

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.