Falling to insert into a MySQL database

Hi i am a newbie trying to learn php programming. I am facing a challenge inserting data into a database . when i click on <input id ="" type="image" name="button4" src="img/Zoona.JPG" alt="Submit" style="width:25%" /> nothing happens. I have four submit images each attached to an event. Were could i be getting it wrong?.You help will be much appreciated.Here is my code below

<?php
session_start();

if(!isset($_SESSION['phonenumber']) || empty($_SESSION['phonenumber'])){
header("location: login.php");
exit;
}

$amount = "";
$amount_err = "";
$charge = "";
$airtelcode = "";
$airtel = "";
$mtnlcode = "";
$mtn = "";
$zamtellcode ="";
$zamtel ="";
$zoonacode = "";
$zoona = "";
$percent = 8;

if($_SERVER["REQUEST_METHOD"] == "button1"){ 

if(empty(trim($_POST["amount"]))){
$amount_err = "Please enter amount.";
} else {$_POST["charge"]=($percent/100) *$amount;
}

$amount = trim($_POST["amount"]);
$airtelcode = $_POST["airtelcode"];
$charge = $_POST["charge"];

if(empty($amount_err)){ 

$sql = "INSERT INTO transdetails ( amount,platform ,platformcode, charge, ) VALUES (?, ?, ?, ?)"; 
if($stmt = mysqli_prepare($link, $sql)){ 

mysqli_stmt_bind_param($stmt, "ssss", $param_amount, $param_platform, $param_platformcode, $param_charge);

$param_amount = $amount;
$param_platform = $airtel;
$param_platformcode = $airtelcode;
$param_charge = $charge; 

if(mysqli_stmt_execute($stmt)){

header("location: send.php");
} else{
echo "Something went wrong. Please try again later.";
}
}

}

} else if($_SERVER["REQUEST_METHOD"] == "button2"){ 

if(empty(trim($_POST["amount"]))){
$amount_err = "Please enter amount.";
} else {$_POST["charge"]=($percent/100) *$amount;
}

$amount = trim($_POST["amount"]);
$mtncode = $_POST["mtncode"];
$charge = $_POST["charge"];

if(empty($amount_err)){ 

$sql = "INSERT INTO transdetails ( amount,platform ,platformcode, charge, ) VALUES (?, ?, ?, ?)";	

if($stmt = mysqli_prepare($link, $sql)){ 

mysqli_stmt_bind_param($stmt, "ssss", $param_amount, $param_platform, $param_platformcode, $param_charge);

$param_amount = $amount;
$param_platform = $mtn;
$param_platformcode = $mtntelcode;
$param_charge = $charge; 

if(mysqli_stmt_execute($stmt)){

header("location: send.php");
} else{
echo "Something went wrong. Please try again later.";
}
} 
} 

}else if($_SERVER["REQUEST_METHOD"] == "button3"){ 

if(empty(trim($_POST["amount"]))){
$amount_err = "Please enter amount."; 
}else {$_POST["charge"]=($percent/100) *$amount;
} 

$amount = trim($_POST["amount"]);
$zamtelcode = $_POST["zamtelcode"];
$charge = $_POST["charge"];


if(empty($amount_err)){ 

$sql = "INSERT INTO transdetails ( amount,platform ,platformcode, charge, ) VALUES (?, ?, ?, ?)"; 
if($stmt = mysqli_prepare($link, $sql)){ 

mysqli_stmt_bind_param($stmt, "ssss", $param_amount, $param_platform, $param_platformcode, $param_charge);

$param_amount = $amount;
$param_platform= $zamtel;
$param_platformcode = $zamtellcode;
$param_charge = $charge; 

if(mysqli_stmt_execute($stmt)){

header("location: send.php");
} else{
echo "Something went wrong. Please try again later.";
}
}

}
} else if($_SERVER["REQUEST_METHOD"] == "booton4"){ 
if(empty(trim($_POST["amount"]))){
$amount_err = "Please enter amount."; 
}else {$_POST["charge"]=($percent/100) *$amount;
}

$amount = trim($_POST["amount"]);
$zoonacode = $_POST["zoonacode"];
$charge = $_POST["charge"];

if(empty($amount_err)){ 

$sql = "INSERT INTO transdetails(amount, platform ,platformcode, charge, ) VALUES (?, ?, ?, ?)";	

if($stmt = mysqli_prepare($link, $sql)){ 

mysqli_stmt_bind_param($stmt, "ssss", $param_amount, $param_platform, $param_platformcode, $param_charge);

$param_amount = $amount;
$param_platform = $zoona;
$param_platformcode = $zoonacode;
$param_charge = $charge; if(mysqli_stmt_execute($stmt)){ 
header("location: send.php");
} else{echo "Something went wrong. Please try again later.";
}
}

}

mysqli_close($link);
}
?> 

Try

else if($_SERVER["REQUEST_METHOD"] == "post")

(I’ve not looked at the rest of the code.)

I have 4 buttons using post will mean nether one of the buttons will be recognized. and if u carefully look at my code u will notice the syntax with this line of code below is on point thanks…
else if($_SERVER["REQUEST_METHOD"] == "button3")

The request method is either post or get, not button1 or button3.

Since i have four buttons how can i make sure when a button is clicked the right data gets inserted in the database?

You need to check the request method to see if a button has been pressed, and then if it has, you can check which button, but since you don’t include your form I can’t tell you how you check which button.

" method="post" accept-charset="UTF-8" >
 <div class="opt">
				
	<div class="wrap-input100 validate-input m-b-26" data-validate="Amount is required">
		<span class="label-input100" font size="16"><h2>ZMK:</h2></span>
				
		<input class="input100" type="number"  style="height:37px;font-size:35pt;text-align:center" name="amount" class="form-control"size="10"maxlength="10" required="required" Placeholder = "000"/>
					
		<span class="focus-input100"></span>
					
		 <input type="hidden"	name="airtelcode"	Value ="0026097">
		<input type="hidden"	name="airtel"	Value ="Airtel">
		<input type="hidden"	name="mtncode"	Value ="0026096">
	        <input type="hidden"	name="mtn"	Value ="Mtn">
		<input type="hidden"	name="zamtelcode"	Value ="0026095">
		<input type="hidden"	name="zamtel"	Value ="Zamtel">
		<input type="hidden"	name="zoonacode"	Value ="00260">
	        <input type="hidden"	name="zoona"	Value ="Zoona">
	</div>
				
				
		
<div class="section-title">	
  <h3>Enter Amount you want to send and Click on Network:</h3>	  
  <hr>
</div>		
  <div class="col-md-3 col-sm-6 team">	 
    <div class="thumbnail">		 
	<input type="image" src="img/airtel.PNG"  name="button1"  alt="Submit" width="25%"   >		
      <div class="caption">
        <h3>Airtel</h3>
        <p>Airtel Mobile Money</p>
      </div>
    </div>
  </div>
  
 
  
  <div class="col-md-3 col-sm-6 team"    >
    <div class="thumbnail"> 
	
	
	<input type="image" name="button2" src="img/MTN.PNG" alt="Submit" style="width:25%"/>	       
      <div class="caption">
        <h3>Mtn</h3>
        <p>Mobile Mobile</p>
      </div>
    </div>
  </div>
  
  <div class="col-md-3 col-sm-6 team">
    <div class="thumbnail"> 
	<input id="" type="image" name="button3" src="img/Zamtel.PNG"  alt="Submit" style="width:25%"   />		    

      <div class="caption">
        <h3>Zamtel</h3>
        <p>Mobile Money</p>
      </div>
    </div>
  </div>
  
  
  <div class="col-md-3 col-sm-6 team">
    <div class="thumbnail">
      <input id ="" type="image" name="button4" src="img/Zoona.JPG"  alt="Submit" style="width:25%"   />
	 
      <div class="caption">
        <h3>Zoona</h3>
        <p>E-wallet</p>
      </div>
    </div>
  </div>
  
  
  
  
</div>	
				
		
		</form>

Please look at my form and help…

See the form tag’s “method” attribute value, it’s “post”.

That means when the form is submitted inputs will send their values in the POST array. eg.

<input name="something" value="foo" ... 
...
$bar = $_POST['something'] 
// the $bar variable is assigned the value "foo" 

Thanks for the help guys . The besides the errors you showed me the amin error was coming from a java srcipt i wrote.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.