Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a php function to update database on submit?

Tags:

function

php

I created this function to update a database when the submit button is clicked. But it doesn't seem to work. The script is meant to draw the values of the user from the database using the login detail of the user through id and populate the forms. The function is to help update the details of the user when their is a change in the form text field.

Please, I need help debugging it.

libraries.php
function db(){ //handles database connection

//connect to the database server or die and spit out connection error
$conn = mysql_connect('localhost','root', '') or die("Cannot connect to the database server now". mysql_error());
//select database table or die and spit out database selection error
mysql_select_db('newbishop',$conn) or die("Error in selecting database now ".mysql_errno());
  return $conn;  
}


personalsettings.php
<?php
include_once('libraries.php'); // contains the database function
session_checker();

db();
$categoryid = $_SESSION['id'];
$select =  "SELECT * FROM users WHERE categoryid ='$categoryid' LIMIT 1";
$row1 = dbprocess ($select);
$rows = mysql_fetch_assoc($row1);
$pname1 = $rows['pname'];
$email1 = $rows['email'];
$user1 = $rows['user'];
$pass1 = $rows['pass'];
$salt1 = $rows['salt'];
$phone1 = $rows['phone'];
$accesslevel = $rows['accesslevel'];
$position = $rows['position'];

function update(){
   db(); // database function
   $pname = $_POST['pname'];
   $categoryid = $_POST['categoryid'];
   $email = $_POST['email'];
   $phone = $_POST['phone'];
   $user = $_POST['users'];
   $pass = $_POST['pass'];

   function createSalt(){
     $string = md5(uniqid(rand(), true));
     return substr($string, 0, 3);
   };

   $salt = createSalt();
   $hash = hash('sha256', $salt . $pass);

   $sql = "UPDATE users SET user=?,pass=?,salt=?,pname=?,email=?,phone=? WHERE categoryid=?";
   $q = $conn->prepare($sql);
   $q->execute(array($user,$hash,$salt,$pname,$email,$phone,$categoryid));
}

?>

the form Edit personal Settings

<input name="users" type="text" id="users" class="users" autocomplete="off" value="<?php echo $user1;  ?>" />

<input type="text" autocomplete="off" name="pass" id="pass" placeholder="Create password" class="passwd" value="<?php echo $pass;  ?>"/>
    <input type="hidden" name="salt" id="salt" value="<?php echo $salt1;  ?>"/>


    <input name="pname" type="text" id="lname" placeholder="Name of Group" class="input-block-level" value="<?php echo $pname1;  ?>"/>

   <input type="hidden" id="categoryselect" name="categoryselect"/> 
   <input name="categoryid" type="text" id="resultselect"   readonly class="input-block-level" value="<?php echo $_SESSION['id'];  ?>"/>


    <input type="text" name="email" id="email" placeholder="Email Address" class="input-block-level" value="<?php echo $email1;  ?>"/>


    <input type="text" name="phone" id="phone" placeholder="Enter Phone Number" class="input-block-level" value="<?php echo $phone1;  ?>"/>


    <input type="text" name="accesslevel" id="accesslevel" class="input-block-level" value="<?php echo $accesslevel;  ?>" readonly/>



    <input type="text" name="position" id="position" class="input-block-level" value="<?php echo $position;  ?>" readonly/>


    <button type="submit" class="btn btn-small btn-primary" name="register" id="register" value="Register" onclick="update()">Submit</button>
  </form>
like image 817
Charles Okaformbah Avatar asked Oct 04 '22 07:10

Charles Okaformbah


1 Answers

db(); // database function

It seems as if you've had forgotten to assign the return value of the function to $conn.

Correct should be:

$conn = db();
like image 188
bwoebi Avatar answered Oct 10 '22 03:10

bwoebi