7 Replies - 517 Views - Last Post: 17 April 2020 - 03:29 AM Rate Topic: -----

#1 itechwebnig   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 20

difficult adding more functions to php code

Posted 11 April 2020 - 10:30 AM

Please you guys should help me out for the sake of God.
I'm seriously having a difficult time right now to solve this by myself but I couldn't solve it.

am trying to add this line of code to my PHP functions.php
but it refused to send the data to the database

this is the PHP functions code
//Add Session
    	public function insert_sessions($session){
    		global $conn;
    		$qry = "select * from sessions where session='$session' ";
    		$query = $conn->query($qry);
    		$count = $query->num_rows;
    		if($count == 0){
    			return false;
    		}else{
    		$sql = "insert into sessions(session) values('$session')";
    		$result = $conn->query($sql);
    		return $result;
    		}
    	}


And here is the HTML page code
 <div class="row">
             <div class="col-md-12">
                <div class="row"><div class="col-md-12"></div></div>            </div>
        </div>
<?php
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				$session = $_POST['session'];
				$res = $user->insert_sessions($session);
				if($res){
					echo "<h3 style='color:green;margin:0;padding:0;text-align:center'>Inserted successfully inserted!</h3>";
				}else{
					echo  "<p style='color:red;text-align:center'>Failed to insert data</p>";
				}
			}
	
		?>
				
		
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    
                    <!-- form start -->
                                        <form role="form" id="" class="form-horizontal" action="" method="post">
                        <div class="box-body">
                            <div class="row">
                                <div class="col-md-6"> <div class="form-group"><label for="field_id_department_name" class=" col-sm-4 control-label">Sessions:</label>
								<div class="col-sm-8"><input type="text" name="session" class="form-control required" value="" id="field_id_department_name">
								</div></div></div>
                            </div>
                        </div>
    
                        <div class="box-footer">
                            <div class="row">
                                <div class="col-md-6 ">
                                    <div class="form-group">
                                        <div class="col-sm-4 control-label"></div>
                                        <div class="col-sm-8">
                        	                <input type="hidden" value="" name="id">
                                            <input type="submit" name="sub" class="btn btn-primary" value="Submit" /> 
                                            <a class="btn  btn-default" href="sessions.php" title="Cancel"> Cancel </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>


Is This A Good Question/Topic? 0
  • +

Replies To: difficult adding more functions to php code

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15724
  • View blog
  • Posts: 62,994
  • Joined: 12-June 08

Re: difficult adding more functions to php code

Posted 11 April 2020 - 10:33 AM

07	            if($count == 0){
08	                return false;
09	            }else{


So if there is no record of that value in your 'sessions' table, you want to return? I would think you would want to do the INSERT then.
Was This Post Helpful? 0
  • +
  • -

#3 itechwebnig   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 20

Re: difficult adding more functions to php code

Posted 11 April 2020 - 11:30 AM

View Postmodi123_1, on 11 April 2020 - 10:33 AM, said:

07	            if($count == 0){
08	                return false;
09	            }else{


So if there is no record of that value in your 'sessions' table, you want to return? I would think you would want to do the INSERT then.


Please I want to do the inserting
of years like 2019/2020
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15724
  • View blog
  • Posts: 62,994
  • Joined: 12-June 08

Re: difficult adding more functions to php code

Posted 11 April 2020 - 11:43 AM

I do not know how that responds to my comment.

Again I am pointing out that in typical workflow if you check a database for a COUNT on some condition, and there are none, one would do the INSERT. Right now the code shows inserting if there ARE records.
Was This Post Helpful? 0
  • +
  • -

#5 benanamen   User is online

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 248
  • Joined: 28-March 15

Re: difficult adding more functions to php code

Posted 12 April 2020 - 10:19 AM

Your logic is unnecessarily complicated.

Set a unique constraint on session, attempt the insert and catch the duplicate error if any.

Also, get rid of the global connection. Pass it to the function as a parameter.

This post has been edited by benanamen: 12 April 2020 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6898
  • View blog
  • Posts: 28,550
  • Joined: 12-December 12

Re: difficult adding more functions to php code

Posted 12 April 2020 - 11:22 AM

You must not directly inject user-submitted data into SQL query strings, use parameterized queries. Your current code is wide open to SQL injection.

PS It might not help that session is a reserved word in MySQL.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2714
  • View blog
  • Posts: 7,997
  • Joined: 15-January 14

Re: difficult adding more functions to php code

Posted 12 April 2020 - 01:39 PM

Quote

Set a unique constraint on session, attempt the insert and catch the duplicate error if any.

Or use INSERT IGNORE, or ON DUPLICATE KEY UPDATE.
Was This Post Helpful? 0
  • +
  • -

#8 itechwebnig   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 20

Re: difficult adding more functions to php code

Posted 17 April 2020 - 03:29 AM

View Postbenanamen, on 12 April 2020 - 10:19 AM, said:

Your logic is unnecessarily complicated.

Set a unique constraint on session, attempt the insert and catch the duplicate error if any.

Also, get rid of the global connection. Pass it to the function as a parameter.


Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1